Post on 14-Jun-2020
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.PNamefrom P, Swhere S.City=P.City;
select S.SID, S.SName, S.City, P.PID, P.PNamefrom S join P using City;
select S.SID, S.SName, S.City, P.PID, P.PNamefrom 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 SA<>SB;
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
S3 P2 20
S2 P4 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
Screw-nut
Result
PID Name
P1 Bolt
P2 Screw-nut
P3 Screw
P4 Screw-nut
P5 Angle-part
P
19
Selection using IN operation
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S
where S.SID in
(select SP.SID
from SP
where SP.PID='P2');
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
20
Selection using EXISTS operation
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S
where exists (
select *
from SP
where SP.SID=S.SID and
SP.PID='P2');
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
21
04.11.2015
8
Selection using subqueries
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S,
(select S.SID
from S, SP
where SP.SID=S.SID and
SP.PID='P2') as SIDs_P2
where S.SID=SIDs_P2.SID;
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
22
Selection using , operation
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S, SP
where S.SID=SP.SID
and SP.PID='P2';
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
23
Selection
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S
where 0 <
(
select count(*)
from SP
where S.SID=SP.SID
and SP.PID='P2'
);
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
24
04.11.2015
9
Selection using ANY operation
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select distinct S.Name
from S
where 'P2' = any
(
select SP.PID
from SP
where S.SID=SP.SID
);
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
25
Selection using grouping
© M. ZymblerDatabase systems
Get names of suppliers that
supply part with P2 ID.
select S.Name
from S, SP
where S.SID=SP.SID and
SP.PID='P2'
group by S.Name;
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
IT-monster
AnyImport
Result
SID Name
S1 Suspense +
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
26
Selection
© M. ZymblerDatabase systems
Get names of suppliers that do not supply part with P2 ID. select distinct S.Name
from Swhere not exists (select *from SPwhere SP.SID=S.SID and P.PID='P2');
select distinct S.Namefrom Swhere S.SID not in (select SP.SIDfrom SPwhere SP.PID='P2');
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
Vegetable Inc.
MacroHard
Result
SID Name
S1 Vegetable Inc.
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
27
04.11.2015
10
Selection
© M. ZymblerDatabase systems
Get names of suppliers that supply red parts. select distinct S.Name
from Swhere S.SID in
(select SP.SIDfrom SPwhere SP.PID in
(select P.PIDfrom Pwhere P.Color='Red'));
select distinct S.Namefrom S, P, SPwhere S.SID=SP.SID andSP.PID=P.PID and P.Color='Red';
SID PID Qty
S1 P5 10
S3 P2 20
S2 P2 30
S7 P1 40
S5 P4 50
S8 P2 30
SP
Name
Vegetable Inc.
MacroHard
Result
SID Name
S1 Vegetable Inc.
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
PID Color
P1 Red
P2 Green
P3 Blue
P4 Red
P5 Red
P
28
Selection using NOT EXISTS
© M. ZymblerDatabase systems
Get names of suppliers that supply all the parts.
select distinct S.Namefrom Swhere not exists (
select *from Pwhere not exists (
select *from SPwhere SP.SID=S.SID
and SP.PID=P.PID));
SID PID Qty
S1 P1 10
S1 P2 20
S1 P3 30
S2 P1 40
S2 P2 50
S2 P3 30
S3 P3 20
S7 P2 100
SP
Name
IT-monster
Result
SID Name
S1 IT-monster
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
PID
P1
P2
P3
P
29
Selection
© M. ZymblerDatabase systems
Get names of suppliers that supply
all the parts.
select distinct S.Name
from S
where
(select count(SP.PID)
from SP
where SP.SID=S.SID) =
(select count(P.PID)
from P);
SID PID Qty
S1 P1 10
S1 P2 20
S1 P3 30
S2 P1 40
S2 P2 50
S2 P3 30
S3 P3 20
S7 P2 100
SP
Name
IT-monster
Result
SID Name
S1 IT-monster
S2 IT-monster
S3 AnyImport
S5 MacroHard
S7 Vegetable Inc.
S8 AnyImport
S
PID
P1
P2
P3
P
30
04.11.2015
11
Selection using UNION operation
© M. ZymblerDatabase systems
Get names of parts that either have
price more than 100, or are supplied
by suppliers from Chelyabinsk, or
both.
select P.Name
from P, SP
where SP.PID=P.PID and P.Price>100
union
select P.Name
from S, P, SP
where SP.PID=P.PID and SP.SID=S.SID
and S.City='Chelyabinsk';
SID PID Qty
S1 P1 10
S2 P1 40
S3 P5 20
S7 P2 100
S8 P2 300
S5 P4 500
SP
Name
Screw-nut
Bolt
Screw
Result
SID City
S1 Moscow
S2 Uryupinsk
S3 Chelyabinsk
S5 Odessa
S7 Chelyabinsk
S8 Chelyabinsk
S
PID Name Price
P1 Bolt 200
P2 Screw-nut 10
P3 Bolt 150
P4 Angle-part 15
P5 Screw 130
P
31
Selection using INTERSECT operation
© M. ZymblerDatabase systems
Get names of parts that have price more than 100 and are supplied by suppliers from Chelyabinsk.
select P.Namefrom P, SPwhere SP.PID=P.PID and P.Price>100intersectselect P.Namefrom S, P, SPwhere SP.PID=P.PID andSP.SID=S.SID andS.City='Chelyabinsk';
SID PID Qty
S1 P1 10
S2 P1 40
S3 P5 20
S7 P2 100
S8 P5 300
S5 P4 500
SP
Name
Bolt
Screw
Result
SID City
S1 Moscow
S2 Uryupinsk
S3 Chelyabinsk
S5 Odessa
S7 Chelyabinsk
S8 Chelyabinsk
S
PID Name Price
P1 Bolt 200
P2 Screw-nut 10
P3 Bolt 150
P4 Angle-part 15
P5 Screw 130
P
32
Selection using EXCEPT operation
© M. ZymblerDatabase systems
Get names of parts that have price
more than 100 and are not supplied
by suppliers from Chelyabinsk.
select P.Name
from P, SP
where SP.PID=P.PID and P.Price>100
except
select P.Name
from S, P, SP
where SP.PID=P.PID and SP.SID=S.SID
and S.City='Chelyabinsk';
SID PID Qty
S1 P1 10
S2 P3 40
S3 P5 20
S7 P2 100
S8 P5 300
S5 P4 500
SP
Name
Bolt
Result
SID City
S1 Moscow
S2 Uryupinsk
S3 Chelyabinsk
S5 Odessa
S7 Chelyabinsk
S8 Chelyabinsk
S
PID Name Price
P1 Bolt 200
P2 Screw-nut 10
P3 Bolt 150
P4 Angle-part 15
P5 Screw 130
P
33
04.11.2015
12
Selection using ALL operation
© M. ZymblerDatabase systems
Get names of parts that have price
more than price of all parts from
Chelyabinsk.
select distinct P.Name
from P
where P.Price > all
(select P.Price
from P
where P.City='Chelyabinsk'); Name
Timber
Screw-nut
Bolt
Result
PID Name Price City
P1 Bolt 30 Chelyabinsk
P2 Timber 99 Shepetovka
P3 Bolt 50 Chelyabinsk
P4 Angle-part 40 Odessa
P5 Screw 23 Chelyabinsk
P6 Screw-nut 77 Moscow
P7 Bolt 88 Kopeisk
P
34
Selection and conditional output
© M. ZymblerDatabase systems
Get IDs, names and rating by words.
select S.SID, S.Name,
case
when S.Rating < 10 then 'Problematic'
when S.Rating < 15 then 'Neutral'
when S.Rating < 20 then 'Acceptable'
when S.Rating < 25 then 'Reliable'
end as Rating
from S;
35
Insertion rows into a table
© M. ZymblerDatabase systems
insertinto table [list of columns]values (list of values) | select query;
Insert one row insert
into P (PID, Color, Name, Weight, City)values ('P10', 'White', 'Timber', 3, 'Moscow');
Insert more than one row insert
into AvgRatings (City, Rating)select S.City, avg(S.Rating)from Sgroup by S.City;
36
04.11.2015
13
Update rows in a table
© M. ZymblerDatabase systems
update tableset list of modified columns[where condition];
Update one row update S
set S.SID='S007', S.Name='Bond'where S.SID='S7';
Update more than one row update S
set S.Rating=S.Rating+1where S.Rating<(select avg(S.Rating) from S);
update Pset P.City = (select S.City from S where S.SID='S007')where P.Color='White';
37
Delete rows from a table
© M. ZymblerDatabase systems
delete [*]from table[where condition];
Delete one row
deletefrom Swhere S.SID='S007';
Delete more than one row
deletefrom Swhere S.Rating<(select avg(S.Rating) from S);
delete from SP;
38
Creating tables
© M. ZymblerDatabase systems
create table S (SID char(4) primary key,Name char(10) not null,City char(10) not null,Rating int not null);
create table P (PID char(4) primary key,Name char(10) not null,City char(10) not null,Price int not null,Color char(10) not nullWeight float not null);
create table SP (SID char(4),PID char(4),Qty int not null,primary key (SID, PID),foreign key (SID)references S (SID)on delete cascade*
on update cascade,foreign key (PID)references P (PID)on delete cascadeon update cascade,
check (Qty>0));
*Available modes are cascade, set null, set default, no action.
39
04.11.2015
14
Alter and drop tables
© M. ZymblerDatabase systems
alter table P
add column Discount float default 0;
alter table P
add constraint check (Price>0),
add constraint check (Weight>0);
drop table S restrict;
drop table SP cascade;
40
SQL/PSM: an example
© M. ZymblerDatabase systems
create procedure Statistics(aliveS out number, deadS out number,aliveP out number, deadP out number) as
declaret number;
beginselect count(*) into t from S;select count(*) into aliveS from SPwhere SP.SID=S.SID;deadS:=t-aliveS;select count(*) into t from P;select count(*) into aliveP from SPwhere SP.PID=P.PID;deadP:=t-aliveP;
end;
41