You are on page 1of 41

ll

ter 13
Structured
Q r
L

l XII p SE o d
Q
i i
l l i
i yi rel i l e
i i

l
i
i r erver
Q

A v t ge i g Q
I ter ctive g ge i l g ge c e e r
c mm ic ti g wit t e t e receive wer t t e
c m le e ti i ec
M lti le t view e er c m ke i ere t view
database structure and databases for the different users
P rt ility Q c e e i t e r gr m i P , erver ,
l t , eve me t e m ile e eve
i ere t m tw re
 No coding needed-It is very easy to manage the database
y tem wit t y ee t write t e t ti l m t
c e y i gt e t r Q
Q

i l l
e re i li er, l i re e
i i ll
l
Q

My QL eatures
Open Source & Free of Cost:
It is Open Source and available at free of cost
 Portability:
Small enough in size to install and run it on any types of Hardware
and OS like Linux, MS Windows or Mac etc
 Security :
Its Databases are secured & protected with password
 Connectivity
Various APIs are developed to connect it with many programming
languages
 Query Language
It supports SQL (Structured Query Language) for handling database
Q

t e i iti g ge
cre te t e t le tr ct re c mm
like R A , A R , R P etc
M t M i l ti g ge
Rec r r w rel te er ti c mm like
, IN R , , UP A etc
t tr l g ge
e t m i l te ermi i r cce rig t t t e
t le c mm like GRAN , R K etc
r cti l c tr l g ge
U e t c tr l t e tr cti c mm like
MMI , R BA K, A P IN etc
Q
ata ty i My

 INTEGER or INT – up to 11 digit number without decimal.
 SMALLINT – up to 5 digit number without decimal.
 FLOAT (M,D) or DECIMAL(M,D) or NUMERIC(M,D)
Stores Real numbers upto M digit length (including .) with D
decimal places.
e.g. Float (10,2) can store 1234567.89

 DATE - Stores date in YYYY-MM-DD format.
 TIME - Stores time in HH:MM:SS format.
 t t t
 CHAR(Size)
A fixed length string up to 255 characters. (default is 1)
 VARCHAR(Size)
A variable length string up to 255 characters.
Char, Varchar, Date and Time values should be enclosed with single („ „) or double (
“”) quotes in MySQL. varchar is used in MySQL and varchar2 is used in Oracle.
Q
t e mm i My l
i li i il l
l
i
l l
ele i e y l P e e e ;
l l l l
i i
l l
li le i e e e
l
i l i i i i l
l l ,i , i
, l
i i l
my l ;
Q

Database Commands in My ql
i ert ew r w i t e i ti g t le e t e IN R c mm :
mysql>INSERT INTO student values(‘dwivedi’,’freya’,’Udaipur’,’4’);
We c i ert rec r wit eci ic c l m ly
mysql>IN R IN t e t l t me, ir t me,city v l e ‘ wive i’,’M k’,’U i r’, ;
Wit t e c mm we c retrieve revi ly i erte r w :
A ge er l rm i:
w t t elect iel me R M t le
WHERE condition that the data must satisfy;
m ri er t r re: ; ; ; ! r ; ;
gic l er t r re: AN ; R ; N
m ri er t r r eci l v l e NU : I
mysql> ELECT * ROM student;
Q

Database Commands in My ql
electi g r w y i g t e W R cl e i t e c mm
my l R M t e t W R cl 4";
electi g eci ic c l m Pr ecti y li ti g t eir me
mysql> ELECT first_name, class ROM student;
electi g r w wit ll v l e i eci ic c l m
my l R M t e t W R ity I NU ;
B W N t cce t i eci ie r ge
mysql> SELECT * FROM Student WHERE class between 4 and 6;
IN operator allows us to easily test if the expression in the list of values.
l SELECT * FROM Student WHERE class in (4,5,6);
Q
t e mm i My l
 Pattern Matching – LIKE Operator
A string pattern can be used in SQL using the following wild card
 % Represents a substring in any length
 _ Represents a single character
Example:
‘A%’ represents any string starting with ‘A’ character
‘_ _A’ represents any 3 character string ending with ‘A’
‘_B%’ represents any string having second character ‘B’
‘_ _ _’ represents any 3 letter string
A pattern is case sensitive and can be used with LIKE operator
mysql> SELECT * FROM Student WHERE Name LIKE ‘A%’;
mysql> ’ i ’
mysql> SELECT Name, City FROM Student WHERE Class>=8 AND Name LIKE
‘%Kumar%’ ;
Q

t e mm i My l
my l R M t e t R R B cl ;
get e ce i g r er e key w r
my l R M t e t R R B cl ;

i l l li l ii l
my l elect i ti ct cl r m t e t;

eleti g electe r w r m t le i g t e c mm
my l R M t e t W R ir t me m r";

To modify or update entries in the table use the UPDATE command


my l UP A t e t cl "W R ir t me rey ";
Q

Database Commands in My ql
i l i i
ll i i l i
l l
ll l i l i
i i i l i ii
i l
i l i ii i l
ll l i l i i ii
ii
l l l l i ii
i i
i l
Q

t e mm i My l
i l i i
mysql> CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘Jaipur',
CONSTRAINT CHK_Person CHECK (Age>=18)
);
mysql> CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(ID) );
Q

t e mm i My l
l i l
i , l i
l i i i l l l
i i i i l
y
i l i
i i l i ll
l l
e i y P Ni
i i l i ll
l l
i
l i le i ll
l l
Q

t e mm i My l
l i l
i ll

i r l i ll
Q

t e mm i My l
l eri le

l
Q

MySQL Order By clause is used to sort the table data in


either Ascending order or Descending order By default,
data is not inserted into Tables in any order unless we have
an index
So, If we want to retrieve the data in any particular order,
we have to sort it by using MySQL Order By statement
Syntax:-SELECT Column_Names
FROM Table_Name
ORDER BY {Column1}[ASC | DESC] {Column2}[ASC | DESC]
Q

MySQL Order by– e.g.


Suppose we are having student table with following data.

Now we write the query – select * from student order by class;

Query result will be in ascending order of class.If we not specify asc/desc in


query then ascending clause is applied by default
Q

My Q r er y e g
e we re vi g t e t t le wit ll wi g t

N w we write t e ery elect r m t e t r er y cl e c;

ery re l ill e i e e i g r er l
Q

i l i ll i

i l l , ;

Q ery re lt will e ce i g r er cl i
me cl e it
t e r eri g will e m rk c l m ce i g r er
Q

i l i ll i

i l l , ;

Q ery re lt will e ce i g r er cl i me cl e it
t e r eri g will e m rk c l m e ce i g r er
Q

A ggreg te cti er rm c lc l ti m lti le v l e


and returns a single value or example, you can use the AVG()
ggreg te cti t t t ke m lti le m er ret r t e
ver ge v l e t e m er ll wi g i t e li t ggreg te
cti rte y my l

Name Purpose
SUM() Returns the sum of given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
Q
Aggreg te cti NU
i er t le m vi g ll wi g rec r
Null values are excluded while (avg)aggregate function is used

Q Q erie Re lt ery
my l elect m l r m MP; 12000
mysql> elect Min( al) from EMP; 3500
my l elect M l r m MP; 4500
my l elect t l r m MP; 3
my l elect Avg l r m MP; 4000
mysql> elect Count(*) from EMP; 5
Q

The GROUP BY clause groups a set of rows/records into a


set of summary rows/records by values of columns or
expressions It returns one row for each group
We often use the GROUP BY clause with aggregate
functions such as SUM, AVG, MAX, MIN, and COUNT The
aggregate function that appears in the SELECT clause
provides information about each group
The GROUP BY clause is an optional clause of the SELECT
statement
y
, , , , i i
l ii , , ,
, , i, l
Q

My Q gr y eg
uppose we are having student table with following data

N w we write ery elect cl r m t e t gr y cl ;

Q ery re lt will e i e cc rre ce cl v l e , t imil r


to use distinct clause like(select distinct class from student)
Q

i i
e ggreg te cti ll w t er rm t e c lc l ti et r w ret r
i gle v l e e GR UP B cl e i te e wit ggreg te cti t er rm
c lc l ti ret r i gle v l e r e c gr
r e m le, i we w t t k w t e m er t e t i e c cl ,y c et e
UN cti wit t e GR UP B cl e ll w : e we re vi g t e t t le
wit ll wi g t

i l l l

l ill i l l l i i
l
Q

i i
we are having student table with following data

i l l , l ;

l ill i l l l i
rk e l r
Q

MySQL GROUP BY with aggregate functions (with where and order by clause)
we are having student table with following data.

Now we write query–select class,avg(marks) from student where class<10 group


by class order by marks desc;

Query result will be unique occurrences of class values where class<10 along with
average marks of each class(sub group) and descending ofer of marks.
Q

e A N l e i e i e
i il ii

l e i e e i e UP
l il ii
ii il
UP l e, e e A N l e
i li ,
i li il
Q

i i i l
we are having student table with following data

i l l , l
i ;

l ill i l l l i
rk e l r e l vi ver e rk
Q

i i i l
we are having student table with following data

i l l , l
i ;

l ill i l l l i
rk e l r e l vi le r
Q

rei r r i
i i l
l i
i
l i l i l
ir rel i i e e le
l i
Q

Cartesian product (X) example Mysql query –


le le w elect r m , ;
el w elect r m cr i ;

Degree of cartesion product is 3 and cardinality is 4=(2 rows of a X 2 rows of b)


Q

Join – Join is used to fetch data from two or more tables,


which is joined to appear as single set of data It is used for
combining column from two or more tables by using values
common to both tables

Types of JOIN
Following are the types of JOIN that we can use in SQL:
• Inner
• Outer
• Left
• Right
Q

NN J i r U J i ⋈
i i i l i i l i
li
ii ii i
Q

INNER Join or EQUI Join example Mysql query –


Table a and Table b as shown below Select course.student_name from
couse , student where
course.student_name=student.stude
nt_name;

Select a.name from a inner join b


where a.name=b.name;
Q

N r lJ N ⋈
l i i i i i
l i
i e le e i e g
l l i ;
Q

i
The left outer join returns a resultset table with the matched data from the
two tables and then the remaining rows of the left table and null from the
right table's columns. E.g.
My l ery
Select * from a left outer join b on
(a.name=b.name);
Q

RIGHT Outer Join


The right outer join returns a resultset table with the matched data from the
two tables being joined, then the remaining rows of the right table and null
for the remaining left table's columns.E.g.
My l ery
Select * from a right outer join b on
(a.name=b.name);
Q

Full Outer Join


The full outer join returns a resultset table with the matched data of two
table then remaining rows of both left table and then the right table.E.g.

My l ery
Select * from a left outer join b on
(a.name=b.name) union Select * from
a right outer join b on
(a.name=b.name);

You might also like