Click here to load reader
View
15
Download
0
Embed Size (px)
04.11.2015
1
STRUCTURED
QUERY LANGUAGE
Database systems
Query defines price. Russian proverb
Outline
Database systems
Historical note and structure
Data manipulation with SQL
Data definition with SQL
2
© M. Zymbler
SQL
© M. ZymblerDatabase systems
SQL (Structured Query Language) is a standard
language of relational databases.
SQL is descriptive, not programming language.
SQL describes only the resulting relation that DBMS should
output
SQL does not describe how DBMS should obtain the
resulting relation
3
04.11.2015
2
SQL historical note
© M. ZymblerDatabase systems
Year Name Features
1970 SEQUEL Language of IBM System R DBMS (SEQUEL – Structured
English QUEry Language).
1986 SQL-86 Accepted by ANSI in 1986 and approved by ISO in 1987.
1989 SQL-89 Improvement of previous version.
1992 SQL-92 Significant improvement of previous version.
1999 SQL:1999
(or SQL-3)
Added: regular expressions, recursive queries, triggers, non-scalar
data types, some OO-features.
2003 SQL:2003 Added: XML data type, OLAP functions, sequences.
2006 SQL:2006 Improved: working with XML data. Added: XQuery in SQL
queries.
2008 SQL:2008 Improved: OLAP functions, SQL:2003 errors fixed.
4
SQL structure
© M. ZymblerDatabase systems
SQL consists of the following parts: Data Definition Language, DDL
Data Manipulation Language, DML
Data Control Language, DCL
Transaction Control Language, TCL
SQL/PSM (Persistent Stored Modules), procedural extension of SQL.
DDL create, drop, alter tables
DML select rows from table(s)
insert rows into table
update rows in table
delete rows from table
5
Database structure for examples
© M. ZymblerDatabase systems
S – Suppliers
P – Parts
SP – Supplies
6
04.11.2015
3
Selection of rows
© M. ZymblerDatabase systems
select [distinct] list of selected columns
from list of tables
[where condition for rows]
[order by list of columns to be ordered]
[group by list of grouped columns]
[having condition of groups];
7
Selection with many conditions
© M. ZymblerDatabase systems
Get list of parts' names and
prices where parts are not
from Paris and have weight
more than 10.
select P.Name, P.Price
from P
where P.City'Paris' and
P.Weight>10;
PID Name City Weight Price
P1 Bolt Paris 15 40
P34 Screw Moscow 29 33
P22 Screw-nut Chelyabinsk 20 24
P45 Screw Odessa 14 33
P
Name Price
Screw 33
Screw-nut 24
Screw 33
Result
8
Selection without duplicates
© M. ZymblerDatabase systems
Get list of parts' names and
prices without repetition where
parts are not from Paris and
have weight more than 10, and
sort resulted list by part name.
select distinct P.Name, P.Price
from P
where P.City'Paris' and
P.Weight>10
order by P.Name;
PID Name City Weight Price
P1 Bolt Paris 15 40
P34 Screw Moscow 29 33
P22 Screw-nut Chelyabinsk 20 24
P45 Screw Odessa 14 33
P
Name Price
Screw 33
Screw-nut 24
Result
9
04.11.2015
4
Selection with ordering
© M. ZymblerDatabase systems
Get list of parts' names and
prices without repetition where
parts are not from Paris and
have weight more than 10, and
sort resulted list descending by
price.
select distinct P.Name, P.Price
from P
where P.City'Paris' and
P.Weight>10
order by P.Price desc;
PID Name City Weight Price
P1 Bolt Paris 15 40
P34 Screw Moscow 29 33
P22 Screw-nut Chelyabinsk 20 24
P45 Screw Odessa 14 33
P
Name Price
Screw 33
Screw-nut 24
Result
10
Selection with calculations
© M. ZymblerDatabase systems
Get part names and prices in €.
select distinct P.Name,
P.Price/40.00 as Price_EUR
from P;
PID Name City Weight Price
P1 Bolt Paris 15 80
P34 Screw Moscow 29 120
P22 Screw-nut Chelyabinsk 20 20
P45 Screw Odessa 14 120
P
Result
Name Price_EUR
Bolt 2
Screw 3
Screw-nut 0.5
11
Selection from joining tables
© M. ZymblerDatabase systems
Get list of all the pairs of suppliers and parts from the same city.
select S.SID, S.SName, S.City, P.PID, P.PName from P, S where S.City=P.City;
select S.SID, S.SName, S.City, P.PID, P.PName from S join P using City;
select S.SID, S.SName, S.City, P.PID, P.PName from S natural join P;
PID PName City Weight Price
P1 Bolt Paris 15 72
P34 Screw Chelyabinsk 29 108
P22 Screw-nut Chelyabinsk 20 18
P45 Screw Odessa 14 108
P
Result
SID SName City PID PName
S2 KGB Ltd Chelyabinsk P34 Screw
S2 KGB Ltd Chelyabinsk P22 Screw-nut
SID SName City Rating
S1 Bender Chernomorsk 15
S34 IT-monster Moscow 29
S2 KGB Ltd Chelyabinsk 20
S
12
04.11.2015
5
Selection of all the columns
© M. ZymblerDatabase systems
Get full info (all the columns)
about parts.
select P.*
from P;
select *
from P;
PID Name City Weight Price
P1 Bolt Paris 15 72
P34 Screw Moscow 29 108
P22 Screw-nut Chelyabinsk 20 18
P45 Screw Odessa 14 108
P
Result
PID Name City Weight Price
P1 Bolt Paris 15 72
P34 Screw Moscow 29 108
P22 Screw-nut Chelyabinsk 20 18
P45 Screw Odessa 14 108
13
Selection from self-joining table
© M. ZymblerDatabase systems
Get all the pairs of suppliers'
IDs where first and second
supplier are from the same
city.
select First.SID as SA,
Second.SID as SB
from S as First, S as Second
where First.City=Second.City
and SASB;
SID Name City Rating
S1 Bender Chernomorsk 15
S3 IT-monster Moscow 29
S2 KGB Ltd Chelyabinsk 20
S7 AnyImport Moscow 31
S5 Suspense+ Chelyabinsk 2
S4 Horns&Hoofs Chernomorsk 15
S
SA SB
S1 S4
S4 S1
S3 S7
S7 S3
S2 S5
S5 S2
Result
14
Selection of aggregations
© M. ZymblerDatabase systems
Get quantity of suppliers.
select 'Quantity' as Text, count(*)
as CountS
from S;
SID Name City Rating
S1 Bender Chernomorsk 15
S3 IT-monster Moscow 29
S2 KGB Ltd Chelyabinsk 20
S7 AnyImport Moscow 31
S5 Suspense+ Chelyabinsk 2
S4 Horns&Hoofs Chernomorsk 15
S
Text CountS
Quantity 6
Result
15
04.11.2015
6
Selection of aggregations
© M. ZymblerDatabase systems
Get min, max and average
quantity of supplements of
part with P1 ID.
select max(SP.Qty) as MaxQ,
min(SP.Qty) as MinQ,
avg(SP.Qty) as AvgQ
from SP
where SP.PID='P1';
SID PID Qty
S1 P1 10
S3 P2 20
S2 P1 30
S7 P1 40
S5 P3 50
S4 P1 80
SP
MaxQ MinQ AvgQ
80 10 40
Result
16
Selection and grouping rows
© M. ZymblerDatabase systems
Get an ID and quantity of
supplements for every part.
select SP.PID, sum(SP.Qty) as Total
from SP
group by SP.PID;
select P.PID,
(select sum(SP.Qty)
from SP
where SP.PID=P.PID) as Total
from P;
SID PID Qty
S1 P5 10
S3 P2 20
S2 P4 30
S7 P1 40
S5 P5 20
S5 P3 50
S8 P2 30
S4 P1 80
SP
PID Total
P5 30
P2 50
P4 30
P1 120
P3 50
Result
17
Selection and grouping rows
© M. ZymblerDatabase systems
Get part IDs that are supplied by more than
one supplier.
select SP.PID
from SP
group by SP.PID
having count(SP.SID)>1;
SID PID Qty
S1 P5 10
S3 P2 20
S2 P4 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
PID
P2
P4
Result
18
04.11.2015
7
Selection and grouping with conditions
© M. ZymblerDatabase systems
Get part names that are supplied
by more than one supplier.
select distinct P.Name
from P
where P.PID in
(select SP.PID
from SP
group by SP.PID
having
count(SP.SID)>1);
SID PID Qty
S1 P5 10