You are on page 1of 37

WEB

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.

DATABASE MANAGEMENT SYSTEM


The software which is used to manage database is called Database Management
System (DBMS). For Example, MySQL, Oracle etc. are popular commercial
DBMS used in different applications. DBMS allows users the following tasks:
Data Definition: It helps in creation, modification and removal of definitions
that define the organization of data in database.
Data Updation: It helps in insertion, modification and deletion of the actual
data in the database.
Data Retrieval: It helps in retrieval of data from the database which can be
used by applications for various purposes.
User Administration: It helps in registering and monitoring users, enforcing
data security, monitoring performance, maintaining data integrity, dealing with
concurrency control and recovering information corrupted by unexpected
failure.

STANDARD QUERY LANGUAGE (SQL)


SQL stands for Structured Query Language. SQL is a standard programming
language specifically designed for storing, retrieving, managing or manipulating
the data inside a relational database management system (RDBMS). SQL
became an ISO standard in 1987.
SQL is the most widely-implemented database language and supported by the
popular relational database systems, like MySQL, SQL Server, and Oracle.
www.teknowize.com MySQL Page 3 of 36

However, some features of the SQL standard are implemented differently in


different database systems.
SQL was originally developed at IBM in the early 1970s. Initially it was called
SEQUEL (Structured English Query Language) which was later changed to
SQL (pronounced as S-Q-L).

You Can Do many data base related task with SQL


There are lot more things you can do with SQL:
• We can create a database.
• We can create tables in a database.
• we can query or request information from a database.
• we can insert records in a database.
• we can update or modify records in a database.
• we can delete records from the database.
• we can set permissions or access control within the database for data security.
• we can create views to avoid typing frequently used complex queries.

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 --

MySQL String Data Types

CHAR(Size) It is used to specify a fixed length string that can contain


numbers, letters, and special characters. Its size can be 0 to
255 characters. Default is 1.

VARCHAR(Size) It is used to specify a variable length string that can contain


numbers, letters, and special characters. Its size can be from
0 to 65535 characters.

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.

VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its


size parameter specifies the maximum column length in
bytes.

TEXT(Size) It holds a string that can contain a maximum length of 255


characters.

TINYTEXT It holds a string with a maximum length of 255 characters.

MEDIUMTEXT It holds a string with a maximum length of 16,777,215.

LONGTEXT It holds a string with a maximum length of 4,294,967,295


characters.

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.

MySQL Numeric Data Types

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.

INTEGER(size) It is equal to INT(size).

FLOAT(size, d) It is used to specify a floatingpoint number. Its size parameter


specifies the total number of digits. The number of digits after the
decimal point is specified by d parameter.

FLOAT(p) It is used to specify a floating point number. MySQL used p parameter


to determine whether to use FLOAT or DOUBLE. If p is between 0
to24, the data type becomes FLOAT (). If p is from 25 to 53, the data
type becomes DOUBLE().

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

DEC(size, d) It is equal to DECIMAL(size, d).

BOOL It is used to specify Boolean values true and false. Zero is considered
as false, and nonzero values are considered as true.

MySQL Date and Time Data Types

DATE It is used to specify date format YYYY-MM-DD. Its supported range is


from '1000-01-01' to '9999-12-31'.

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'

YEAR It is used to specify a year in four-digit format. Values allowed in


four digit format from 1901 to 2155, and 0000.

CODE FOR STANDARD QUERY LANGUAGE


SHOW ALL SQL DATA BASES

To show all the data bases created in database management system.

SYNTAX
Show databases;
www.teknowize.com MySQL Page 7 of 36

CREATING A DATABASE

To create a database in which all related data will be stored.

SYNTAX
Create database database_name ;

DELETE A DATABASE

To delete a database for many created databases.

SYNTAX
Drop database database_name ;
www.teknowize.com MySQL Page 8 of 36

USE A DATABASE

To use a database which we need to create table or modify table.

SYNTAX
Use database_name ;
www.teknowize.com MySQL Page 9 of 36

Database changed to technical_sumit

CHECK CURRENT DATABASE

To check current data base in which we are currently working.

SYNTAX
Select database();

CREATE A TABLE

To create a table in selected database.

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;

DESCRIBE TABLE NAME

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

To delete a table in selected database.

SYNTAX
Drop table table_name ;

INSERT DATA INTO TABLES


To insert data in a created table in any databases.

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

We can also insert value by this code

SYNTAX
Insert into table_name;

Values ( coloumn1_value, coloumn2_value, coloumn3_value, ……….);


www.teknowize.com MySQL Page 12 of 36

SHOW DATA OF TABLE

To show inserted data in table which we created in database.

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 Vs NOT NULL

Null means when we does not enter any value in any column then in this place
of value it show NULL.

It does not need any tag (it automatically entered)

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 ));

ADD COLUMN IN A TABLE

In this we can add a column in any table .

SYNTAX
Alter table table_name add column_name_ data_type( data type range
according to need ) ;
www.teknowize.com MySQL Page 15 of 36

DELETE A COLUMN FROM TABLE

Using this we can delete any column from a table.

SYNTAX
Alter table table_name drop column column_name;

PRIMARY KEY

The primary key constraint uniquely identifies each record in a table.

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

CRUD operation means create read update delete operation in a table.

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.

For read Where clause


The where clause is use to filter records. It means that suppose we need to
search any detail from table then we use this .

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) ;

• Detele all data from a table


This is use to delete entire data from a table.
SYNTAX

Delete from table_name ;


www.teknowize.com MySQL Page 20 of 36

SELECT DISTINCT STATEMENT

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

Select column_name from table_name order by column_name(selected first)


desc/ASC ;

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

COUNT( ),MAX ( ), MIN ( ), AVG ( ) AND SUM ( ) FUNCTION

The COUNT ( ) function return the number of row that matches a specified
criteria.

The MIN ( ) function use to specified minimum value in numeric column.

The MAX ( ) function use to specified maximum value in numeric column.

The AVG ( ) function return the average value of a numeric column.

The SUM ( ) function return the total sum of a numeric column.

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

Select * from table_name where column_name = data (what need to find)


www.teknowize.com MySQL Page 24 of 36

2) NOT EQUAL T0
SYNTAX

Select * from table_name where column_name != data (what need to find) ;

3) GREATER THEN AND LESS THEN


SYNTAX
• Select * from table_name where column_name > data (what need to find);
• Select * from table_name where column_name < data (what need to find);
www.teknowize.com MySQL Page 25 of 36

4) GREATER THEN EQUAL TO AND LESS THEN EQUAL TO


SYNTAX
• Select * from table_name where column_name > = data (what need to
find) ;
• Select * from table_name where column_name < = data (what need to
find) ;

5) DATA BETWEEN TWO DATA


SYNTAX

• Select * from table_name where column_name between data 1 and data


2;
OR
SYNTAX
• Select * from table_name where column_name <= data (highest data) &&
data >= data (lowest data) ;
www.teknowize.com MySQL Page 26 of 36

6) DATA NOT BETWEEN TWO DATA


SYNTAX
Select * from table_name where column_name NOT between data 1 and
data 2 ;
www.teknowize.com MySQL Page 27 of 36

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

The concat( ) function add two or more expression together .

For add two or more expression from column

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

Reverse function use to write any thing reversely .

SYNTAX
select reverse ( ‘need to reverse’ );
or
select reverse ( ‘need to reverse’ ) as reverse ;

CHARACTER LENGTH FUNCTION

This is use for count the length of words , numbers etc.

SYNTAX
www.teknowize.com MySQL Page 30 of 36

Select char_length (‘ character length need to count’);

UPPER CASE FUNCTION

This is use for write lower word in upper word.

SYNTAX
Select upper ( character need to covert lower to upper word );

LOWER CASE FUNCTION


This is use for write lower word in upper word.

SYNTAX
Select lower ( character need to covert upper to lower word );
www.teknowize.com MySQL Page 31 of 36

DATE TIME DATA TYPE

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)

DATE TIME DATA TYPE WITH PRE-DEFINE FUNCTIONS

• CURDATE ( ) : GIVE CURRENT TIME AND DATE ( YYYY-MM-


. DD)
• CURTIME ( ) : GIVE CURRENT TIME (HH:MI:SS)
• NOW ( ) : GIVE BOTH CUR DATE & TIME (YYYY-MM-DD ..
. HH:MML:SS)
NOTE – This all above data types use 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 ( ‘ ’ ).

using current date ,time and date time

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 Deynitu1998@gmail.com

2 Sumit 10sumitsarkar@gmail.com

3 hariom Hariomprajapati9097@gmail.com

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

SYNTAX FOR TABLE 1


Create table table_name ( column_name not null auto increment primary
key, column_name datatype (range) , …………………);

SYNTAX FOR TABLE 2


Create table table_name ( column_name data_type not null auto
increment primary key / according to need , column_name datatype
(range), …………………, column_name data type, foreign key (
column_name // column name need to same as table 1 primary key use
for foreign key) references table_2_name (column name // table 1 column
name which is primary key);
www.teknowize.com MySQL Page 34 of 36

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

Select * from table1_name left 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 ;

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 ;

You might also like