Basic To Advance Tutorial of SQL
Basic To Advance Tutorial of SQL
DESIGNING
My s
SQL
DATABASE
Learn coding fast
Written by
SUMIT SARKAR
www.teknowize.com
www.teknowize.com MySQL Page 1 of 36
Table of contents
TOPIC PAGE NO
Introduction to MySQL 1 - 6
Show all sql databases 6 - 7
Creating database 7
Delete a database 7 - 8
Use a database 8 - 9
Check current database 9
Create database 9
Show table 10
Describe table name 10
Delete a table 11
Insert data into table 11
Show data of table 12
Show warnings 12
Null vs not null 13
Default value 14
Add column in a table 14
Delete a table 15
Primary key 15
Auto increment 15 - 16
CRUD operation 16 - 19
Select distinct statement 20
Order by keyword 20 - 21
Limit keyword 21
Like operator 22
Aggregate function 22 - 23
Logical operation 23 - 28
Concat function 28
Reverse function 29
Character length function 29 - 30
Uppercase function 30
Lowercase function 30 - 31
Date time data type 31 - 32
Foreign key 32 - 34
Inner join 34 - 35
Left join 35 - 36
Right join 36
Outer join 36
www.teknowize.com MySQL Page 2 of 36
DATABASE
Database is a collection of related data (or information) stored in a format that
can easily be accessed.
RELATION DATABASE
A relational database is a database divided into logical units called tables, where
tables are related to one another within the database. Relational database allows
data to be broken down into logical, smaller, and manageable units for easier
maintenance and better performance.
Tables are related to one another through common keys or fields in a relational
database system, that's why even though the desired data may exist in more than
one table, you can easily join multiple tables together to get combined data set
using a single query.
The list does not end here, you can perform many other database-related tasks
with SQL.
DATA TYPE
Data type are use to enter value in the table. In sql data type are divided into the
categories.
NUMERIC TYPE STRING TYPE DATE TYPE
Int Char Date
Smallint Varchar Datetime
Tinying Binary Timestamp
Mediumint Varbinary Time
Bigint Blob year
Decimal Tinyblob --
Numeric Mediumblob --
Float Longblob --
www.teknowize.com MySQL Page 4 of 36
Double Text --
bit Tinytext --
-- Mediumtext --
-- Longtext --
-- Enum --
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size
parameter specifies the column length in the bytes. Default
is 1.
ENUM(val1, val2, It is used when a string object having only one value, chosen
val3,...) from a list of possible values. It contains 65535 values in an
ENUM list. If you insert a value that is not in the list, a blank
value will be inserted.
www.teknowize.com MySQL Page 5 of 36
SET( val1,val2,val3,....) It is used to specify a string that can have 0 or more values,
chosen from a list of possible values. You can list up to 64
values at one time in a SET list.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to 65,535
bytes.
BIT(Size) It is used for a bit-value type. The number of bits per value is
specified in size. Its size can be 1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -
2147483648 to 2147483647 and unsigned range varies from 0 to
4294967295. The size parameter specifies the max display width that
is 255.
DOUBLE(size, d) It is a normal size floating point number. Its size parameter specifies
the total number of digits. The number of digits after the decimal is
specified by d parameter.
DECIMAL(size, d) It is used to specify a fixed point number. Its size parameter specifies
the total number of digits. The number of digits after the decimal
parameter is specified by d parameter. The maximum value for the
size is 65, and the default value is 10. The maximum value for d is
30, and the default value is 0.
www.teknowize.com MySQL Page 6 of 36
BOOL It is used to specify Boolean values true and false. Zero is considered
as false, and nonzero values are considered as true.
DATETIME(fsp) It is used to specify date and time combination. Its format is YYYY-
MM-DD hh:mm:ss. Its supported range is from '1000-01-01
00:00:00' to 9999-12-31 23:59:59'.
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is stored as the number
of seconds since the Unix epoch('1970-01-01 00:00:00' UTC). Its
format is YYYY-MM-DD hh:mm:ss. Its supported range is from
'1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss. Its
supported range is from '-838:59:59' to '838:59:59'
SYNTAX
Show databases;
www.teknowize.com MySQL Page 7 of 36
CREATING A DATABASE
SYNTAX
Create database database_name ;
DELETE A DATABASE
SYNTAX
Drop database database_name ;
www.teknowize.com MySQL Page 8 of 36
USE A DATABASE
SYNTAX
Use database_name ;
www.teknowize.com MySQL Page 9 of 36
SYNTAX
Select database();
CREATE A TABLE
SYNTAX
Create table table_name ( coloumn1_name data_type ( data type range
according to need ), coloumn2_name data_type ( data type range according
to need), ……………);
www.teknowize.com MySQL Page 10 of 36
SHOW TABLES
To show all tables in selected database.
SYNTAX
Show tables;
To show detail about any table in selected database like , number of field, key,
extra (auto increment), data type, null etc.
SYNTAX
Desc table_name ;
www.teknowize.com MySQL Page 11 of 36
DELETE A TABLE
SYNTAX
Drop table table_name ;
SYNTAX
Insert into table_name ( coloumn1_name, coloumn2_name, coloumn3_name,
………….. ) ;
Values ( coloumn1_value, coloumn2_value, coloumn3_value, ……….);
Note:- varchar value must be in the ‘…..’ or “…..” when insert data.
OR
SYNTAX
Insert into table_name;
SYNTAX
Select * from table_name ;
SHOW WARNINGS
When we enter any wrong value and then it will run, then it will show warning.
By using this tag we can show warning detail.
for example- when we enter character value in the place of integer value then it
will run but show warning. then we will check why warning showed by this tag.
SYNTAX
Show warnings ;
www.teknowize.com MySQL Page 13 of 36
Null means when we does not enter any value in any column then in this place
of value it show NULL.
AND
NOT NULL means when we does not enter any value in any coloumn then
there automatically inserted zero.
SYNTAX
Create table table_name ( coloumn1_name data_type (data type range
according to need) not null, coloumn2_name data_type (data type range
according to need), ……………not null );
www.teknowize.com MySQL Page 14 of 36
DEFAULT VALUE
Default value means when we doesnot enter any value in any coloum then there
we can use a custom default value.
SYNTAX
Create table table_name ( coloumn1_name data_type ( data type range
according to need) default value, coloumn2_name data_type ( data type range
according to need ));
SYNTAX
Alter table table_name add column_name_ data_type( data type range
according to need ) ;
www.teknowize.com MySQL Page 15 of 36
SYNTAX
Alter table table_name drop column column_name;
PRIMARY KEY
Primary key must contain unique value, and cannot contain null value.
SYNTAX
Create table table_name (column_name1 data_type ( data type range
according to need) not null (if use as a primary key) , column_name2
data_type ( data type range according to need ) not null (if use as a primary
key) ……….. , primary key( field name which need to make it unique ) );
AUTO INCREMENT
Auto increment use to generate a unique number to be generated automatically
when a new record insert into a table.
www.teknowize.com MySQL Page 16 of 36
SYNTAX
Create table table_name ( column_name1 data_type ( data type range
according to need ) not null (if coloumn use as a primary key) auto_increment
(if need to auto increment), column_name2 data_type ( data type range
according to need ) not null (if coloumn use as a primary key) auto_increment
(if need to auto increment) ……….. , primary key( field name which need to
make it unique ) );
CRUD OPERATION
1) Create operation
In this we can create table by same method which we discuss above.
www.teknowize.com MySQL Page 17 of 36
2) Read operation
In this we can read data inserted in a table .this process is same as select
statement.
SYNTAX
Select * from table name where column_name = detail need to search from
selected column ;
www.teknowize.com MySQL Page 18 of 36
3) update operation
the update statement use to modify the existing record in a table.
SYNTAX
Updated table_name set coloumn_name (data need to update from which
column) = insert data (insert data value need to update) where
coloumn_name (name of which column related to updated row) = data ( data
which already inserted ,it basically use to relate data) ;
www.teknowize.com MySQL Page 19 of 36
4) Delete operation
the delete statement is use to delete existing records in a table.
• Delete a existing record
This is use to delete a existing record from a table.
SYNTAX
Delete from table_name where column_name (data need to delete from which
column) = data ( all data need to delete from same row) ;
The select distinct statement use to search a unique data from a table. That
means if two data are same then it show one data.
SYNTAX
Select distinct column_name from table_name ;
Note- this statement must have one field in place of column_name because
it show distinct (unique) value. If we give two field then it not give unique
value as shown below.
ORDER BY KEYWORD
The order by keyword is used to sort the result in ascending and descending
order
SYNTAX
www.teknowize.com MySQL Page 21 of 36
note - if we write nothing like asc or desc then it show ascending order by
default.
LIMIT KEYWORD
The limit keyword use to search the first 1,2,3,4,…… values from a column
SYNTAX
Select column_name from table_name order by column_name(selected first)
desc/ASC limit 2 ;
www.teknowize.com MySQL Page 22 of 36
LIKE OPERATOR
The like operator used in a WHERE clause to search for a specified pattern in a
column.( suppose we know any 2 or 3 letter of anyone name then we use like
operator in a WHERE CLAUSE.
SYNTAX
Select column_name from table_name where column _name( we selected first
in column name) like ‘ % letter we remembered % ’ ;
AGGREGATE FUNCTION
The COUNT ( ) function return the number of row that matches a specified
criteria.
SYNTAX
Select aggregate_function( any function like max min count avg……) (
coloumn_name ) from table_name ;
www.teknowize.com MySQL Page 23 of 36
LOGICAL OPERATION
The logical operators are use to find smaller , grater ,equal to values from same
column.
1) EQUAL T0
SYNTAX
2) NOT EQUAL T0
SYNTAX
7) DATA OF ANYONE
SYNTAX
Select * from table_name where column_name in ( ‘name1’, ‘name
2’,………. );
OR
Select * from table_name where column_name = ‘name1’ or column_name
= ‘name 2’ or ……….);
www.teknowize.com MySQL Page 28 of 36
CONCAT( ) FUNCTION
SYNTAX
Select Concat (expression1,expression2,expression3,…………);
SYNTAX
select concat ( column_name1, ' ', column_name2, ' ' ……….. ) from
table_name as fullname;
www.teknowize.com MySQL Page 29 of 36
REVERSE ( ) FUNCTION
SYNTAX
select reverse ( ‘need to reverse’ );
or
select reverse ( ‘need to reverse’ ) as reverse ;
SYNTAX
www.teknowize.com MySQL Page 30 of 36
SYNTAX
Select upper ( character need to covert lower to upper word );
SYNTAX
Select lower ( character need to covert upper to lower word );
www.teknowize.com MySQL Page 31 of 36
My sql come with the following data types for storing a date or a date/time
value in the data base
• DATE (this use when create table) : YYYY-MM-DD (this formate use to
. insert data in table)
• TIME (this use when create table) : HH:MI:SS (this formate use to insert
. data in table)
• DATETIME (this use when create table) : YYYY-MM-DD HH:MML:SS
. (this formate use to insert data in
. table)
SYNTAX
Syntax is same as creating table but where we declare date & time types like
varchar,int etc there we need to write above data type.
www.teknowize.com MySQL Page 32 of 36
When we insert data into this data type we must follow the above format of
date,time and datetime.
Note-inserted data into this data types must in ( ‘ ’ ).
FOREIGN KEY
A foreign key is a key used to link two table together.
A foreign key is a field ( or collection of field ) in one table that refer to the
primary key in another table .
www.teknowize.com MySQL Page 33 of 36
Example:-
TABLE 1
Customer_id Customer_name email
1 Nitu [email protected]
2 Sumit [email protected]
3 hariom [email protected]
TABLE 2
ORDER_ID ORDER_DATE AMOUNT CUSTOMER_ID
1 ‘2020/5/6’ 55 1
2 ‘2020/06/06 100 1
3 ‘2020/03/12 203 2
4 ‘2019/05/13 76 3
TABLE 1
TABLE 2
INNER JOIN
The inner join key word select the record that have matching value in the both
table.
www.teknowize.com MySQL Page 35 of 36
SYNTAX
Select * from table1_name inner join table2_name on
table1_name.table1_column name// which is primary key inn table 1 =
table2.column_name // which is foreign key in table 2 ;
LEFT JOIN
The left join keyword return all record from the left table (table 1) and the
match record from the right table (table 2) . the result is null from the right side
, if the is no match.
SYNTAX
www.teknowize.com MySQL Page 36 of 36
RIGHT JOIN
The right join keyword return all record from the right table (table 2) and the
match record from the left table (table 1) . the result is null from the left side ,
if the is no match.
SYNTAX
Select * from table1_name right join table2_name on
table1_name.table1_column name// which is primary key inn table 1 =
table2.column_name // which is foreign key in table 2 ;