• Non ci sono risultati.

DatabasesSQL language: set operatorsElena Baralisand Tania Cerquitelli©2013 Politecnico di Torino1

N/A
N/A
Protected

Academic year: 2021

Condividi "DatabasesSQL language: set operatorsElena Baralisand Tania Cerquitelli©2013 Politecnico di Torino1"

Copied!
5
0
0

Testo completo

(1)

D B

M

G

SQL language: basics

Set operators Set operators

D B

M

G

Set operators

The UNION operator The INTERSECT operator The EXCEPT operator

D B

M

G

Set operators

The UNION operator The UNION operator

D B

M

G

The UNION operator Set union operator

It performs the union of the two relational expressions A and B

relational expressions A and B may be generated by SELECT statements

it requires schema compatibility between A and B removal of duplicates

UNION removes duplicates

UNION ALL does not remove duplicates A UNIONB

UNION: example

P

SP

Find the codes of products that are either red or supplied by supplier S2 (or both)

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId PId Qty S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200 S1 P5 100 S1 P6 100 S2 P1 300 S2 P2 400 S3 P2 200 S4 P3 200

UNION: example

SELECTPId FROMP

WHEREColor=‘Red’

P

PId P1 P6

Find the codes of products that are eitherredor supplied by supplier S2 (or both)

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

(2)

D B

M

G

Find the codes of the products that areeither red or supplied by supplier S2(or both)

UNION: example

SELECTPId FROMSP WHERESId=‘S2’

SP

PId P1 P2 SId PId Qty

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

S4 P5 400

D B

M

G

UNION: example

SELECTPId FROMP

WHEREColor=‘Red’

UNION SELECTPId FROMSP WHERESId=‘S2’;

R

PId P1 P2 PId P6

P1 P2 PId P1 P6

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B

M

G

UNION: example

SELECTPId FROMP

WHEREColor=‘Red’

UNION SELECTPId FROMSP WHERESId=‘S2’;

R

PId P1 P2 Duplicate P6

removal

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B

M

G

UNION: example

SELECTPId FROMP

WHEREColor=‘Red’

UNION SELECTPId FROMSP WHERESId=‘S2’;

Schema compatibility

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B

M

G

UNION ALL: example

SELECTPId FROMP

WHEREColor=‘Red’

UNION ALL SELECTPId FROMSP WHERESId=‘S2’;

R

PId P1 P1 P2 PId P6

P1 P2 PId P1 P6

Find the codes of products that are either red or supplied by supplier S2 (or both)

D B

M

G

Set operators

The INTERSECT operator The INTERSECT operator

(3)

D B

M

G

Set intersection operator

It performs the intersection of the two relational expressions A and B

relational expressions A and B may be generated by SELECT statements

it requires schema compatibility between A and B The INTERSECT operator

A INTERSECTB

D B

M

G

INTERSECT: example

P

S

Find the cities where both one or more suppliers and one or more stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B

M

G

INTERSECT: example

SELECTCity FROMS

City London

Paris Paris London Athens

S

Find the cities where both one or more suppliers and one or more stores are based

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B

M

G

INTERSECT: example

SELECTStore FROMP

P

Store London

Paris Rome London

Paris London

Find the cities where both one or more suppliers and one or more stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

Find the cities where both one or more suppliers and one or more stores are based

INTERSECT: example

SELECTCity FROMS INTERSECT SELECTStore FROMP;

London Paris

R

City London

Paris Paris London Athens

Store London

Paris Rome London

Equivalence with other operators

The intersection operation may also be performed by means of

a join the INoperator

(4)

D B

M

G

Equivalence with join

The FROMclause contains the relations involved in the intersection

The WHEREclause contains join conditions between the attributes listed in the SELECT clauses of relational expressions A and B

D B

M

G

Equivalence with join: example

SELECTCity FROMS, P

WHERE S.City=P.Store;

Find the cities where both one or more suppliers and one or more stores are based

D B

M

G

Equivalence with the IN operator

One of the two relational expressions is turned into a nested query using operator IN

The attributes in the outerSELECT clause, joined together by a tuple constructor, make up the left- hand side of the IN operator

D B

M

G

Equivalence with IN: example

SELECTStore FROMP

WHEREStore IN(SELECTCity FROMS);

Find the cities where both one or more suppliers and one or more stores are based

D B

M

G

Set operators

The EXCEPT operator The EXCEPT operator

D B

M

G

The EXCEPT operator

A EXCEPTB Set difference operator

It subtracts relational expression B from relational expression A

it requires schema compatibility between A and B

(5)

D B

M

G

EXCEPT: example

P

S

Find the cities where one or more suppliers, but no stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B

M

G

EXCEPT: example

SELECTCity FROMS

City London

Paris Paris London Athens

S

Find the cities where one or more suppliers, but no stores are based

SId SName #Employees City

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

D B

M

G

EXCEPT: example

SELECTStore FROMP

P

Store London

Paris Rome London Paris London

Find the cities where one or more suppliers, but no stores are based

PId PName Color Size Store

P1 Jumper Red 40 London

P2 Jeans Green 48 Paris

P3 Blouse Blue 48 Rome

P4 Blouse Red 44 London

P5 Skirt Blue 40 Paris

P6 Shorts Red 42 London

D B

M

G

EXCEPT: example

SELECTCity FROMS EXCEPT SELECTStore FROMP;

Athens

R

City London

Paris Paris London Athens

Store London

Paris Rome London Paris London

Find the cities where one or more suppliers, but no stores are based

Equivalence with the NOT IN operator

The difference operation may also be performed by means of the NOT IN operator

relational expression B is nested within the NOT IN operator

the attributes in the SELECTclause of relational expression A, joined together by a tuple constructor, make up the left-hand side of the NOT IN operator

Equivalence with the NOT IN operator: example

SELECTCity FROMS

WHERECityNOT IN (SELECTStore FROMP);

Find the cities where one or more suppliers, but no stores are based

Riferimenti

Documenti correlati

The tuple identified by code P1 is updated Update the features of product P1: assign Yellow to Color, increase the size by 2 and assign NULL to Store.

Integrity constraints are checked after each SQL command that may cause their violation Insert or update operations on the referencing table that violate the constraints are

The view is updatable CREATE VIEW SUPPLIER_LONDON AS SELECT SId, SName, #Employees, City FROM S.

The database is in a new (final) correct state The changes to the data executed by the transaction

If an SQL command returns a single row it is sufficient to specify in which host language variable the result of the command shall be stored If an SQL command returns a table (i.e.,

if Rossi has propagated the privilege to other users, the privilege is revoked in cascade if Rossi has created a view using the SELECT privilege, the view is removed. REVOKE

Operations carried out to execute the query reading of the accessory physical structure to recover the physical locations of the records corresponding to Residence=Como

Find the codes and the names of the pilots who are qualified to fly on an aircraft that can cover distances greater than 5,000 km (MaximumRange>=5,000). AIRCRAFT (AId,