83
A.R. Hurson 323 CS Building Database Systems SQL

CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

AR Hurson323 CS Building

Database SystemsSQL

Database Systems

Structured Query Language (SQL)The SQL language has the following features as

wellEmbedded and Dynamic facilities to allow SQL

code to be called from a host language or a query beconstructed at run-timeTriggers which are actions executed by DBMS

whenever certain changes to the database meetcertain conditions

Structured Query Language (SQL)Security to control usersrsquo accesses to data objectsTransaction management commands to allow the

execution of transactionsRemote database accesses to allow client server or

distributed environments

Database Systems

Data Definition LanguageCREATE TABLE The general format is as follows

CREATE TABLE base-table(column-definition [ column-definition] hellip[ primary-key-definition][ foreign-key-definition [ foreign-key-definition] hellip])

where a ldquocolumn-definitionrdquo is in the form ofcolumn data-type [NOT NULL]

Note that the specification of primary-key isoptional

Database Systems

CREATE TABLE S( S CHAR(5) NOT NULL

SNAME CHAR(20) NOT NULLSTATUS INTEGER NOT NULLCITY CHAR(15) NOT NULL

PRIMARY KEY ( S ) )

This will create an empty table The data valuesnow can be entered using INSERT command

Result S S SNAME STATUS CITY

Database Systems

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 2: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Database Systems

Structured Query Language (SQL)The SQL language has the following features as

wellEmbedded and Dynamic facilities to allow SQL

code to be called from a host language or a query beconstructed at run-timeTriggers which are actions executed by DBMS

whenever certain changes to the database meetcertain conditions

Structured Query Language (SQL)Security to control usersrsquo accesses to data objectsTransaction management commands to allow the

execution of transactionsRemote database accesses to allow client server or

distributed environments

Database Systems

Data Definition LanguageCREATE TABLE The general format is as follows

CREATE TABLE base-table(column-definition [ column-definition] hellip[ primary-key-definition][ foreign-key-definition [ foreign-key-definition] hellip])

where a ldquocolumn-definitionrdquo is in the form ofcolumn data-type [NOT NULL]

Note that the specification of primary-key isoptional

Database Systems

CREATE TABLE S( S CHAR(5) NOT NULL

SNAME CHAR(20) NOT NULLSTATUS INTEGER NOT NULLCITY CHAR(15) NOT NULL

PRIMARY KEY ( S ) )

This will create an empty table The data valuesnow can be entered using INSERT command

Result S S SNAME STATUS CITY

Database Systems

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 3: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Structured Query Language (SQL)Security to control usersrsquo accesses to data objectsTransaction management commands to allow the

execution of transactionsRemote database accesses to allow client server or

distributed environments

Database Systems

Data Definition LanguageCREATE TABLE The general format is as follows

CREATE TABLE base-table(column-definition [ column-definition] hellip[ primary-key-definition][ foreign-key-definition [ foreign-key-definition] hellip])

where a ldquocolumn-definitionrdquo is in the form ofcolumn data-type [NOT NULL]

Note that the specification of primary-key isoptional

Database Systems

CREATE TABLE S( S CHAR(5) NOT NULL

SNAME CHAR(20) NOT NULLSTATUS INTEGER NOT NULLCITY CHAR(15) NOT NULL

PRIMARY KEY ( S ) )

This will create an empty table The data valuesnow can be entered using INSERT command

Result S S SNAME STATUS CITY

Database Systems

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 4: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Data Definition LanguageCREATE TABLE The general format is as follows

CREATE TABLE base-table(column-definition [ column-definition] hellip[ primary-key-definition][ foreign-key-definition [ foreign-key-definition] hellip])

where a ldquocolumn-definitionrdquo is in the form ofcolumn data-type [NOT NULL]

Note that the specification of primary-key isoptional

Database Systems

CREATE TABLE S( S CHAR(5) NOT NULL

SNAME CHAR(20) NOT NULLSTATUS INTEGER NOT NULLCITY CHAR(15) NOT NULL

PRIMARY KEY ( S ) )

This will create an empty table The data valuesnow can be entered using INSERT command

Result S S SNAME STATUS CITY

Database Systems

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 5: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

CREATE TABLE S( S CHAR(5) NOT NULL

SNAME CHAR(20) NOT NULLSTATUS INTEGER NOT NULLCITY CHAR(15) NOT NULL

PRIMARY KEY ( S ) )

This will create an empty table The data valuesnow can be entered using INSERT command

Result S S SNAME STATUS CITY

Database Systems

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 6: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

INSERTINTO S ( S SNAME STATUS CITY)VALUES (lsquoS1rsquo lsquoSMITHrsquo 10 lsquoLONDONrsquo )

RESULTS SNAME STATUS CITYS1 SMITH 10 LONDON

Database Systems

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

SNAME

STATUS

CITY

S1

SMITH

10

LONDON

Page 7: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

ALTER TABLE Allows to add a new column atthe right to an existing base-table Its generalformat is as follows

ALTER TABLE base-table ADD column data-type

Database Systems

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 8: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

ALTER TABLE S ADD DISCOUNT INTEGER

Discount column is added to the table S All existing tuples are(conceptually) expanded and the value of the new column isnull in every record

Specification of NOT NULL is not allowed in ALTERTABLE)

S S SNAME STATUS CITY DISCOUNTRESULT

Database Systems

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 9: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

DROP TABLE Allows to destroy an existingbase-table

DROP TABLE base-table

Database Systems

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 10: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Integrity ConstraintsAs noted before constraints can be defined

either as the table constraints (over a singletable) or assertions

Database Systems

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 11: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tablePrimary KeyCandidate Key

CREATETABLE Student ( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid) )

Studentskey is called the constraint name - It willbe returned if the constraint is violated

Database Systems

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 12: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableForeign KeyKey words FOREIGN KEY and REFERENCE are

used to specify this constraintCREATE TABLE Enroll ( Sid CHAR(20)

Cid CHAR(20)grade CHAR(10)PRIMARY KEY (Sid Cid)

FOREIGN KEY (Sid)REFERENCES Students)

Referenced relation

Database Systems

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 13: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableThe keyword CHECK along with a conditional

expression can be used to enforce a constraint overan attribute

Database Systems

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 14: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableCREATE TABLE Student

( Sid CHAR(20)name CHAR(30)login CHAR(20)age INTEGERgpa REAL UNIQUE (name age)CONSTRAINT StudentskeyPRIMARY KEY (Sid)CHECK (age gt= 16 AND age lt=30))

When a new tuple is inserted into the table or an existing tupleis modified the conditional statement is checked If the resultis false the command is rejected

Database Systems

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 15: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableAssume the following tablesSailors(sidinteger snamestring ratinginteger agereal)Boats(bidinteger bnamestring colorstring)Reserves(sidinteger bidinteger daydate)

Define a constraint that ldquoInterlakerdquo boat cannot bereserved

Database Systems

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 16: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableCREATETABLE Reserves

( Sid INTEGERbid INTEGERday DATEPRIMARY KEY (Sid bid)FOREIGN KEY (Sid) REFERENCES Sailors)FOREIGN KEY (bid) REFERENCES Boats)CONSTRAINT noInterlakeResCHECK (ldquoInterlakerdquo ltgt

(SELECT BbnameFROM Boats BWHERE Bbid = Reservebid)))

Database Systems

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 17: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableCommand CREATE can also be used to define a

domain

CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1CHECK (VALUE gt= 1 AND VALUE lt=1000)

Here INTEGER is the base type for domainQtyvalue however its contents is restricted by theCHECK statement

Database Systems

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 18: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Constraints over a single tableOnce a domain is defined it can be used to limit

contents of a column in a tableThe DEFAULT keyword assigns a default value to a

domain mdash this value will be automatically assumedin an attempt to insert a tuple into the relationwithout an initial value for an attribute defined overQtyvalue

Database Systems

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 19: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Data Definition LanguageCREATE TABLECREATE VIEWCREATE DOMAINDROP TABLEDROP VIEWALTER TABLEDefine KEY CONSTRIANTSCHECK

Database Systems

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 20: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Data Manipulation Language (DML)SQL provides four DML statements

SELECT UPDATE DELETE and INSERT

Database Systems

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 21: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

The following strategy is used to evaluatean SQL expressionCompute the cross-product of relation-listDiscard resulting tuples if they fail

qualifications (restrict)Delete attributes that are not in target-list

(project)If DISTINCT is specified then duplicate tuples

are eliminated

Database Systems

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 22: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

SELECT SELECT specifies field (s) FROM aspecific table WHERE specific condition (s) istrueSELECT [DISTINCT] item(s)FROM table (s)[WHERE condition][GROUP BY field (s)][ORDER BY field (s)]

Target list a list of attributes

Relation list

Qualifier mdash expressions involvingconstants andor column names combined using AND OR and NOT

Database Systems

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 23: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

The following tables are assumed for the rest ofthis section

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

SupplierRelationS

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 24: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

PartRelationP

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 25: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SPRelation

Database Systems

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 26: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Simple QueriesSELECT S STATUSFROM SWHERE CITY = lsquoParisrsquo

S StatusS2 10S3 30

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S2

10

S3

30

Page 27: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Simple RetrievalSELECT PFROM SP

PP1P2

P3

P4P5

P6

P1

P2

P2

P2

P4

P5

RESULT

Duplicates are not removed

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

P1

P2

P2

P2

P4

P5

Page 28: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

SELECT DISTINCT PFROM SP

RESULT

PP1P2

P3

P4P5

P6

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

P1

P2

P3

P4

P5

P6

Page 29: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Retrieval of Computed Values Assume weight inlsquoPart relationrsquo is in PoundSELECT PP lsquoWeight in Grams = rsquo PWeight 454FROM P

PP1 Weight in Grams = 5448P2 Weight in Grams = 7718P3 Weight in Grams = 7718P4 Weight in Grams = 6356P5 Weight in Grams = 5448P6 Weight in Grams = 8626

Result

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

Weight in Grams =

5448

P2

Weight in Grams =

7718

P3

Weight in Grams =

7718

P4

Weight in Grams =

6356

P5

Weight in Grams =

5448

P6

Weight in Grams =

8626

Page 30: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Naming Fields in the resultant relationAS and = are two ways to name fields in result

SELECT Supplier name = Sname STATUSFROM SWHERE CITY = lsquoParisrsquo S Sname Status City

S1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 31: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

SELECT Sname As Supplier name STATUSFROM SWHERE CITY = lsquoParisrsquo

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name StatusJones 10Blake 30

Result

Database Systems

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Supplier name

Status

Jones

10

Blake

30

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 32: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

LIKE is the keyword that allows stringmatching (pattern matching) operation

Note that lsquo_rsquo stands for any one character(Donrsquot care) and lsquorsquo stands for 0 or morearbitrary characters (repeated donrsquot care)

SELECT Sname As Supplier name CityFROM SWHERE CITY LIKE lsquosrsquo

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 33: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Supplier name CityJones ParisBlake Paris

Adams Athens

Result

Database Systems

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Supplier name

City

Jones

Paris

Blake

Paris

Adams

Athens

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 34: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get all parts whose names begin with the letter CSELECT PFROM PWHERE PPname LIKE lsquoCrsquo P Pname Color Weight City

P1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P Pname Color Weight CityP5 Cam Blue 12 ParisP6 Cog Red 19 London

Result

Database Systems

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 35: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Similarly NOT LIKE can also be used in theWHERE clauseSELECT PFROM PWHERE PCity NOT LIKE lsquoErsquo

In this case the condition is evaluated to ldquotruerdquo if City doesnot contain an lsquoErsquo

Database Systems

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 36: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Retrieval Involving NULLAssume for the sake of the example thatsupplier S5 has a status value of nullGet supplier numbers for supplier with statusgreater than 25

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 37: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Result

SELECT SFROM SWHERE Status gt 25

SS3S5

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

S

S3

S5

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 38: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams perp Athens

Result

SS3

Unlike previous case S5 does not qualify

Database Systems

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

(

Athens

Page 39: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get full detail of all suppliersSELECT FROM S

This is equivalent toSELECT SS SSname SStatus SCityFROM S

Database Systems

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 40: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

RESULT

Database Systems

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 41: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Qualified Retrieval Get supplier numbers forsuppliers in lsquoParisrsquo with STATUS gt 20SELECT SFROM SWHERE CITY = lsquoParisrsquo

AND STATUS gt 20

SS3

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

S3

Page 42: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Retrieval with Ordering The result may beordered based on the contents of one or severalfieldscolumn [ order ] [ column [ order ] ] hellip

where lsquoorderrsquo is either ASC or DESC and ASCas the default

Database Systems

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

Page 43: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get supplier numbers and Status for suppliersin lsquoParisrsquo in descending order of status

SELECT S STATUSFROM SWHERE CITY = lsquoParisrsquoORDER BY STATUS DESC

S StatusS3 30S2 10

RESULT

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

S

Status

S3

30

S2

10

Page 44: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Join QueriesSimple equi-join

SELECT S PFROM S PWHERE SCITY = PCITY

Note that the field referenced in the WHERE clausehere must be qualified by the table names

Database Systems

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 45: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Conceptually you may generate theCartesian product of the tables listed in theFROM clause Then eliminate all thetuples that do not satisfy the join conditiondefined in WHERE clause

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 46: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 47: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status SCity P Pname Color Weight PCityS1 Smith 20 London P1 Nut Red 12 LondonS1 Smith 20 London P4 Screw Red 14 LondonS1 Smith 20 London P6 Cog Red 19 LondonS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 ParisS4 Clark 20 London P1 Nut Red 12 LondonS4 Clark 20 London P4 Screw Red 14 LondonS4 Clark 20 London P6 Cog Red 19 London

RESULT

Database Systems

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S1

Smith

20

London

P1

Nut

Red

12

London

S1

Smith

20

London

P4

Screw

Red

14

London

S1

Smith

20

London

P6

Cog

Red

19

London

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S4

Clark

20

London

P1

Nut

Red

12

London

S4

Clark

20

London

P4

Screw

Red

14

London

S4

Clark

20

London

P6

Cog

Red

19

London

Page 48: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Greater-than join Get all combinations ofsupplier and part information such that thesupplier city follows the part city in alphabeticalorder

SELECT S PFROM S PWHERE SCITY gt PCITY

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 49: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 50: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P1 Nut Red 12 LondonS2 Jones 10 Paris P4 Screw Red 14 LondonS2 Jones 10 Paris P6 Cog Red 19 LondonS3 Blake 30 Paris P1 Nut Red 12 LondonS3 Blake 30 Paris P4 Screw Red 14 LondonS3 Blake 30 Paris P6 Cog Red 19 London

RESULT

Database Systems

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P1

Nut

Red

12

London

S2

Jones

10

Paris

P4

Screw

Red

14

London

S2

Jones

10

Paris

P6

Cog

Red

19

London

S3

Blake

30

Paris

P1

Nut

Red

12

London

S3

Blake

30

Paris

P4

Screw

Red

14

London

S3

Blake

30

Paris

P6

Cog

Red

19

London

Page 51: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get all combinations of supplier informationand part information where the supplier and partconcerned are co-located but omitting supplierwith status 20

SELECT S PFROM S PWHERE SCITY = PCITY

AND STATUS lt gt 20

Database Systems

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 52: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S P

Database Systems

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

Page 53: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S Sname Status SCity P Pname Color Weight PCityS2 Jones 10 Paris P2 Bolt Green 17 ParisS2 Jones 10 Paris P5 Cam Blue 12 ParisS3 Blake 30 Paris P2 Bolt Green 17 ParisS3 Blake 30 Paris P5 Cam Blue 12 Paris

RESULT

Database Systems

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

S

Sname

Status

SCity

P

Pname

Color

Weight

PCity

S2

Jones

10

Paris

P2

Bolt

Green

17

Paris

S2

Jones

10

Paris

P5

Cam

Blue

12

Paris

S3

Blake

30

Paris

P2

Bolt

Green

17

Paris

S3

Blake

30

Paris

P5

Cam

Blue

12

Paris

Page 54: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Aggregate FunctionsAggregate functions are used to enhance the

retrieval power of SQL These areCOUNT number of values in the columnSUM sum of the values in the columnAVG average of the values in the columnMAX largest value in the columnMIN smallest value in the column

Database Systems

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

Page 55: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Aggregate FunctionsFor SUM and AVG column must be numeric

valuesKey word DISTINCT can be used to eliminate the

duplicate valuesFor COUNT DISTINCT must be specified

Database Systems

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

Page 56: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get the total number of suppliersSELECT COUNT ()FROM S

Note that the result is a table with a singlevalue

RESULT

5

S Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

Database Systems

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

5

Page 57: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get the total number of suppliers currentlysupplying part

SELECT COUNT ( DISTINCT S)FROM SP

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

Page 58: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get the number of shipments for part lsquoP2rsquoSELECT COUNT ()FROM SPWHERE P = lsquoP2rsquo

RESULT

4

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

4

Page 59: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get the total quantity of part lsquoP2rsquo supplied

SELECT SUM (QTY)FROM SPWHERE P = lsquoP2rsquo

RESULT

1000

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

Database Systems

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

1000

Page 60: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Query Using Union Union is traditional unionoperator borrowed from set theoryGet supplier numbers for parts that either

weight more than 16 Pounds or are supplied bysupplier lsquoS2rsquo

Database Systems

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 61: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

SELECT PFROM PWHERE WEIGHT gt 16UNIONSELECT PFROM SPWHERE S = lsquoS2rsquo

Note redundant duplicate rows are always eliminatedHowever we can use UNION ALL operator to includethe duplicates

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 62: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP1P2

P3

P6

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P1

P2

P3

P6

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 63: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Previous query can also be written asSELECT DISTINCT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16OR SPS = lsquoS2rsquo

Database Systems

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 64: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Query Using INTERSECT SimilarlyINTERSECT operator has also been borrowed fromtraditional set theory

SELECT PFROM PWHERE WEIGHT gt 16INTERSECTSELECT PFROM SPWHERE S = lsquoS2rsquo

Database Systems

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 65: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

PP2

RESULTP Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

Database Systems

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

P

P2

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Page 66: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Previous query can also be specified asSELECT PFROM P SPWHERE PP = SPPAND PWEIGHT gt 16AND SPS = lsquoS2rsquo)

Database Systems

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 67: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Modification OperationsThe SQL DML supports three modification

operationsUPDATEDELETEINSERT

Note these operations change the contents ofthe database hence they may violate theintegrity constraints

Database Systems

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 68: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

UPDATE The general format of UPDATEoperation is

UPDATE tableSET field = scalar-expression

[ field = scalar-expression] hellip[ WHERE condition]

All records in table satisfying condition aremodified in accordance with the assignments

Database Systems

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 69: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Change the color of part lsquoP2rsquo to yellowincrease its weight by 5 and set its city tounknown (null)

UPDATE PSET COLOR = lsquoYellowrsquo

WEIGHT = WEIGHT + 5CITY = NULL

WHERE P = lsquoP2rsquo

Database Systems

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 70: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Double the status of all suppliers in lsquoLondonrsquo

UPDATE SSET STATUS = STATUS 2WHERE CITY = lsquoLondonrsquo

Database Systems

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 71: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Set the shipment quantity to zero for allsupplies in lsquoLondonrsquo

UPDATE SPSET QTY = 0WHERE lsquoLondonrsquo =

( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 72: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Update Multiple TablesUPDATE SSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

UPDATE SPSET S = lsquoS9rsquoWHERE S = lsquoS2rsquo

The first UPDATE will force the database to become inconsistentsince now in shipment table there is a supplier lsquoS2rsquo that does notexist The database remains in inconsistent state until after thesecond UPDATE is executed

Database Systems

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 73: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

DELETE This command has the followinggeneral format

DELETEFROM table

[ WHERE condition ]

All records in table satisfying condition are deleted

Database Systems

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 74: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Delete all shipments with quantity greater than 300DELETEFROM SP

WHERE QTY gt 300

Delete supplier lsquoS5rsquoDELETEFROM S

WHERE S = lsquoS5rsquo

Database Systems

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 75: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Delete all shipmentsDELETEFROM SP

Note that now SP is an empty tableDelete all shipments for suppliers in lsquoLondonrsquo

DELETEFROM SP

WHERE lsquoLondonrsquo =( SELECT CITYFROM SWHERE SS = SPS )

Database Systems

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 76: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

INSERT Insert comes in two formats

INSERTINTO table [ (field [ field ] hellip ) ]VALUE (literal [ literal ] hellip )

In this case a record with the contents definedin VALUE clause is added to the table

Database Systems

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 77: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

INSERTINTO table [ (field [ field ] hellip ) ]

sub-query

In this case the result of the sub-query (may bemultiple rows) is added to the table

In both cases omitting the list of fields means allfields in the table in left to right order

Database Systems

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 78: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Add part lsquoP7rsquo (city lsquoAthensrsquo weight 24) nameand color unknown to the P relation

INSERTINTO P ( P CITY WEIGHT)VALUE ( lsquoP7rsquo lsquoAthensrsquo 24)

Note we assumed that COLOR and Pname are not definedas lsquoNOT NULLrsquo

Database Systems

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 79: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Add part lsquoP8rsquo (name lsquoSprocketrsquo color lsquoPinkrsquo citylsquoNicersquo weight 14) to the P relation

INSERTINTO PVALUE ( lsquoP8rsquo lsquoSprocketrsquo lsquoPinkrsquo 14 lsquoNicersquo )

Add a new shipment with supplier number lsquoS20rsquopart number lsquoP20rsquo and quantity 1000

INSERTINTO SP (S P QTY)VALUE ( lsquoS20rsquo lsquoP20rsquo 1000 )

Database Systems

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 80: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

For each part supplied get the part number andthe total quantity supplied supported for thatpart and save the result in the database

CREATE TABLE TEMP( P CHAR (6) NOT NULLTOTQTY INTEGER NOT NULL

PRIMARY KEY (P ))

Database Systems

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 81: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

INSERTINTO TEMP (P TOTQTY )

SELECT (P SUM (QTY)FROM PGROUP BY P

SELECT is executed and result is copied in Temprelation User now can do whatever heshe wantsto do with Temp relation EventuallyDROP TABLE TEMP

will eliminate Temp relation from the database

Database Systems

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 82: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Questions Using the following relationsS Sname Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens

S

P Pname Color Weight CityP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London

P

S P QTYS1 P1 300S1 P2 200S1 P3 400S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400

SP

Database Systems

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems

S

P

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

P

Pname

Color

Weight

City

P1

Nut

Red

12

London

P2

Bolt

Green

17

Paris

P3

Screw

Blue

17

Rome

P4

Screw

Red

14

London

P5

Cam

Blue

12

Paris

P6

Cog

Red

19

London

S

Sname

Status

City

S1

Smith

20

London

S2

Jones

10

Paris

S3

Blake

30

Paris

S4

Clark

20

London

S5

Adams

30

Athens

Page 83: CSE441W Database Systems · The DEFAULT keyword assigns a default value to a ... Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT

Get supplier names for suppliers who supply part P2 Get supplier names for suppliers who supply at least one red

part Get supplier names for suppliers who supplies all parts Get supplier numbers for suppliers who supply at least all those

parts supplied by supplier S2 Get supplier names for suppliers who do not supply part P2 Get all pairs of supplier numbers such that the two suppliers

concerned are co-located

Database Systems

  • Database SystemsSQL
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems
  • Database Systems