Download presentation

Presentation is loading. Please wait.

Published byAsia Pennings Modified over 3 years ago

1
Database Languages Chapter 7

3
The Relational Algebra

4
The relational Algebra The relational algebra is a complete set of operations on relations which allows to select data from a relational database. Cartesian product Union, Intersection, Difference Projection -join Division

5
Sample database R r1r2r3 S1 s1s2 1x33p 4x34q 3y44p 2z7 S2 s1s2 4q 2m

6
Cartesian product R x S2r1r2r3s1s2 1x34q 4x34q 3y44q 2z74q 1x32m 4x32m 3y42m 2z72m

7
Projection, -join Projection R [ r2, r3 ] r2r3 x3 y4 z7 -join R [ r3 > s1 ] S1 r1r2r3s1s2 3y43p 2z73p 2z74q 2z74p

8
Left Outer-join r1r2r3s1s2 1x33p 4x33p 3y44q 3y44p 2z7 R[r3 = l s1]S1

9
Union, Intersection, Difference UNION S1 S2 s1s2 3p 4q 4p 2m Intersection S1 S2 s1s2 4q Difference S1 \ S2 s1s2 3p 4p

10
Division S#P# s1p1 s1p2 s1p3 s1p4 s1p5 s1p6 s2p1 s2p2 s3p2 s4p2 s4p4 s4p5 Divide by ÷ Result DEND/DOR DEND DOR 1 P# p1 DOR 2 DOR 3 P# p2 p4 P# p1 p2 p3 p4 p5 p6 S# s1 s2 S# s1 s4 S# s1

11
Example S S#Sname 1Jones 2Duval 3Codd 4Carter SP S#P# 111 115 212 315 1.Give all parts 2.Give names of suppliers supplying part 15 3.Give those suppliers that do not supply part 15 4.Give those suppliers that supply something else than part 15 5.Give those suppliers that supply something but not part 15

12
Solutions 1.SP [ P# ] 2. ( ( S [ S# = S# ]SP )[ P# ÷ 15] [15 ]) [ Sname ] 3.

13
Data Description Language

14
Sample Database SS#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris S4Clark 20London S5Adams30Athens PP#PNAMECOLORWEIGHTCITY P1NutRed12London P2BoltGreen17Paris P3ScrewBlue17Rome P4SrewRed14London P5CamBlue19London P6CogRed19London SPS#P#QTY S1P1300 S1P2200 S1P3400 S1P5200 S1P6100 S2P1300 S2P2400 S3P2200 S4P2200 S4P4300 S4P5400

15
CREATE CREATE TABLE base-table-name ( base-table-element - commmalist ) where base-table-element is a column-definition or a base-table-constraint-definition column-definition: column representation [ default definition ] default definition: NOT NULL, NULL, current-date,....) Create Table

16
Base-table-constraint candidate key: UNIQUE ( column-commalist ) primary key: PRIMARY KEY ( column-commalist ) foreign key: FOREIGN KEY ( column-commalist ) REFERENCE base-table [ ( column-commalist ) ] [ ON DELETE option ] [ ON UPDATE option ] option: NO ACTION, CASCADE, SET DEFAULT, SET NULL check constraint: CHECK ( conditional-expression )

17
CREATE table example CREATE TABLE SP (S# S# NOT NULL, P# P# NOT NULL, QTY QTY NOT NULL, PRIMARY KEY ( S#, P# ) FOREIGN KEY ( S# ) REFERENCE S ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY ( P# ) REFERENCE P ON DELETE CASCADE ON UPDATE CASCADE, CHECK ( QTY > 0 AND QTY < 5001 ) ) ;

18
DDL - table modification ALTER table ALTER TABLE base-name-table ADD column-name data-type ; ( “not null” is not permitted ) DROP table DROP TABLE base-table-name

19
DDL - Indexes CREATE index CREATE [ UNIQUE ] INDEX index-name ON base-table-name ( column-name [ ORDER ] [, column-name [ ORDER ]... ) DROP index DROP INDEX index-name ;

20
Data Manipulation Language

21
DML - Data Manipulation Language SELECT [ DISTINCT ] field(s) FROMtable(s) [ WHERE predicate ] [ GROUP BY field(s) [ HAVING predicate ] ] [ ORDER BY field(s) ] ; SQL

22
Simple Retrieval - SQL Get part numbers for all parts supplied SELECT P# FROM SP ; SELECT DISTINCT P# FROM SP ; P# P1 P2 P3 P4 P5 P6 P1 P2 P4 P5 P# P1 P2 P3 P4 P5 P6

23
Simple retrieval - QBE Get part numbers for all parts supplied SP S#P# QTY P._PX SP S#P# QTY P.ALL._PX

24
Retrieval of expressions For all parts get the part number and the weight in grams ( in the table weights are in pounds ). SELECT P.p#, P.weight*454 FROM P ; PP#PnameColorweightCity P._PXP.Weight *454 P# P15448 P27718 P37718 P46356 P55448 P68626 OUTPUT

25
Simple retrieval of table Get full details of all suppliers SELECT * FROM S ; SS#SnameStatusCity P._SX P._SN P._ST P._SC SS#SnameStatusCity P. SQL QBE

26
Qualified retrieval 1 Get supplier numbers for suppliers located in Paris or with status greater than 20. SELECT S# FROM S WHERE City = ‘PARIS’ OR Status > 20 ; SS#SnameStatusCity P._SX > 20 P._SY Paris

27
Qualified retrieval - 2 Get supplier numbers for suppliers located in Paris with status greater than 20. SELECT S# FROM S WHERE City = ‘PARIS’ AND Status > 20 ; SS#SnameStatusCity P._SX > 20 Paris

28
Qualified retrieval with ordering Get supplier numbers and status for suppliers in Paris in descending order of status SELECT S#, Status FROM S WHERE City = ‘Paris’ ORDER BY Status DESC ; SS#SnameStatus City P._SX P.DO._ST Paris

29
Simple Equi-join Get all combinations of supplier and part information such that the supplier and part in question are located in the same city SELECT S.*, P.* FROM S, P WHERE S.City = P.City ; SS#SnameStatus City P. _X P. PP#PnameColorweightCity _X SQL QBE

30
Greater-than join Get all combinations of supplier and part information such that the supplier city follows the part city in alphabetical order. SELECT S.*, P.* FROM S, P WHERE S.City > P.City SS#SnameStatus City P. > _X P. PP#PnameColorweightCity _X SQL QBE

31
Join with additional condition Get all combinations of supplier and part information such that the supplier and part are colocated, but omitting suppliers with status > 20. SELECT S.*, P.* FROM S, P WHERE S.City = P.City AND S.Status NOT > 20 ; SS#SnameStatus City P. NOT > 20 _X P. PP#PnameColorweightCity _X SQL QBE

32
Retrieving specified fields from a join Get all part-number / supplier-number combinations such that supplier and part are colocated SELECT S.S#, P.P# FROM S, P WHERE S.City = P.City SS#SnameStatus City P._SX _X P._PX PP#PnameColorweightCity _X SQL QBE

33
Join of three tables Get all pairs of city names such that a supplier located in the first city supplies a part stored in the second city SELECT DISTINCT S.City, P.City FROM S, P, SP WHERE S.S# = SP.S# AND SP.P# = P.P# ; SS#SnameStatus City _X P._CS _Y PP#PnameColorweightCity P._PC SQL QBE SP S# P# QTY _X _Y

34
Function in a select clause Get the total number of suppliers SQL QBE SELECT count ( * ) FROM S ; SS# Sname StatusCity P.COUNT._SX

35
Function in select clause with predicate Get the total number of suppliers supplying part 2. SQL QBE SELECT count ( * ) FROM SP WHERE P# = ‘P2’ ; SP S# P# QTY P.CNT.ALL._SX P2

36
Join of table with itself Get all pairs of supplier numbers such that the two suppliers are colocated. SELECT FIRST.S#, SECOND.S# FROM S FIRST, S SECOND WHERE FIRST.City = SECOND.City AND FIRST.S# < SECOND.S# ; SS# Sname StatusCity _SX _SY _CZ Conditions _SX < _SY RESULT FIRST SECOND P. _SX _SY

37
Use of GROUP BY For each part supplied, get the part number and the total shipment quantity for that part SQL QBE SELECT P#, SUM(QTY) FROM SP GROUP BY P# ; SP S# P# QTY P.G._PX P.SUM.ALL._QX

38
Use of HAVING Get part numbers for all parts supplied by more than one supplier. SQL QBE SELECT P# FROM SP GROUP BY P# HAVING COUNT(*) > 1 ; SP S# P# QTY _SX P._PX NOT._SX _PX or SP S# P# QTY CNT.ALL._SX> 1 P.G._PX

39
Retrieval involving a subquery Get supplier names for suppliers who supply part P2. SELECT UNIQUE SNAME FROM S, SP WHERE S.S# = SP.S# AND SP.P# = ‘P2’ ; SELECT UNIQUE Sname FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# = ‘P2’) ; SELECT Sname FROM S WHERE ‘P2’ IN (SELECT P# FROM SP WHERE S#= S.S#) ; SP S# P# QTY SS# Sname StatusCity _X P._SN _X P2

40
Single-record Update Change color of part P2 to yellow SQL: Update distinct P Set color = “yellow” where P# = P2 ; QBE: P p# pname color weight city p2 U.yellow

41
Multiple Update Double the status of all suppliers in London SQL Update S Set status = status * 2 where City = “London” ; QBE S S# sname status city _SX_ST London U. _SX 2 * _ST

42
Update involving sub-query Set quantity to zero for all suppliers in London SQL Update SP Set qty = 0 where “London” = ( select city from S where s# = SP.s# ) ; QBE SP s# p# qty U. _SX 0 S s# sname status city _SX London

43
Views CREATE VIEW name AS SELECT statement ; Example: CREATE VIEW good-suppliers AS SELECT s#, status, city FROM S WHERE status = 15 ; The VIEW-definition is stored in the directory but the select is not performed

44
VIEWS - 2 Example: CREATE VIEW PQ ( P#, sumqty ) AS SELECT p#, SUM(qty) FROM SP GROUP BY p# ; Views can be defined in terms of other views Some views are updateble Views can be dropped

45
VIEWS - usage VIEWS can be used just like base tables CREATE VIEW LONDON-SUPPLIERS AS SELECT s, sname, status FROM S WHERE city = ‘London’ ; Two formulations with the same result SELECT *SELECT s#, sname, status FROM LONDON-SUPPLIERSFROM SWHERE status < 50 ORDER by s#;AND city = ‘London’ ORDER BY s# ;

46
SQL System Catalog The system catalog is also a relational database SYSTABLES ( name, creator, colcount,... ) SYSCOLUMNS ( name, tbname, coltype,... ) SYSINDEX ( name, tbname, creator,... ) SELECT tbname FROM SYSCOLUMNS WHERE name = ‘s#’ ; SELECT name FROM SYSCOLUMNS WHERE tbname = ‘S’ ; Examples: Updating the catalog is not possible

47
QBE dictionary Retrieval of table names : Get all tables known to the system P. Creation of new table I. S S# Sname Status City domain S# Sname Status City type char 5 char 20 fixed char 15 key Y U.N U.N U.N invers Y U.N U.N U.N

Similar presentations

Presentation is loading. Please wait....

OK

Further Normalization I

Further Normalization I

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Pdf converter to ppt online The brain anatomy and physiology ppt on cells Ppt on shell structures theory Ppt on 2d and 3d shapes Ppt on nuclear power in india Ppt on faculty development programme iim Ppt on vision mission and objectives Download ppt on biodegradable and nonbiodegradable waste Ppt on max life insurance Ppt on programmable logic device