• Aucun résultat trouvé

Uncertain Data Management Reminders on Relational Algebra and Calculus

N/A
N/A
Protected

Academic year: 2022

Partager "Uncertain Data Management Reminders on Relational Algebra and Calculus"

Copied!
84
0
0

Texte intégral

(1)

Uncertain Data Management

Reminders on Relational Algebra and Calculus

Antoine Amarilli1, Silviu Maniu2 November 21st, 2017

1Télécom ParisTech

2LRI

Thanks to Pierre Senellart for proofreading these slides.

1/23

(2)

Table of contents

Basics

Relational algebra

SQL

2/23

(3)

Relations

Relation name and arity

Attribute names (optional)

Relation Class, arity 5

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Set of rows (tuples) on a domain (no duplicates)

(4)

Relations

Relation name and arity

Attribute names (optional)

Relation Class, arity 5

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Set of rows (tuples) on a domain (no duplicates)

3/23

(5)

Relational signature

Signature σ : set of relation names and attributes , e.g.:

• Class(date,teacher,resp,name,num)

• Student(id,name)

• Member(student,classname)

Database instance : one relation for each name in σ

→ Query :

• Input: database

• Output:relation

(6)

Relational signature

Signature σ : set of relation names and attributes , e.g.:

• Class(date,teacher,resp,name,num)

• Student(id,name)

• Member(student,classname)

Database instance : one relation for each name in σ

→ Query :

• Input: database

• Output:relation

4/23

(7)

Languages

Two languages to write queries:

The relational algebra:

• operationalway to define queries

• based onoperatorsto construct new relations

The relational calculus:

• declarativeway to define queries

• based onfirst-order logic

→ Codd’s theorem: both have the same expressive power

• SQL, the practical language used by databases

(8)

Languages

Two languages to write queries:

The relational algebra:

• operationalway to define queries

• based onoperatorsto construct new relations

The relational calculus:

• declarativeway to define queries

• based onfirst-order logic

→ Codd’s theorem: both have the same expressive power

• SQL, the practical language used by databases

5/23

(9)

Languages

Two Three languages to write queries:

The relational algebra:

• operationalway to define queries

• based onoperatorsto construct new relations

The relational calculus:

• declarativeway to define queries

• based onfirst-order logic

→ Codd’s theorem: both have the same expressive power

• SQL, the practical language used by databases

(10)

Table of contents

Basics

Relational algebra

SQL

6/23

(11)

Relational algebra

• Basic relations:

• the relation names in the signature

• constant relations, e.g., the empty relation

Projection Π

Selection σ

Renaming ρ

Union

Product × and join ./

Difference

(12)

Projection: keep a subsequence of the attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Π

teacher,resp

(Class) teacher resp

Antoine Fabian Silviu Fabian

→ Duplicates are removed

8/23

(13)

Projection: keep a subsequence of the attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Π

teacher,resp

(Class) teacher resp

Antoine Fabian Silviu Fabian

→ Duplicates are removed

(14)

Projection: keep a subsequence of the attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Π

teacher,resp

(Class) teacher resp Antoine Fabian Silviu Fabian

→ Duplicates are removed

8/23

(15)

Projection: keep a subsequence of the attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

Π

teacher,resp

(Class) teacher resp Antoine Fabian Silviu Fabian

→ Duplicates are removed

(16)

Selection: keep a subset of the tuples

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

σ

teacher=“Silviu”

(Class)

date teacher resp name num

2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

9/23

(17)

Selection: keep a subset of the tuples

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

σ

teacher=“Silviu”

(Class)

date teacher resp name num

2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

(18)

Selection: keep a subset of the tuples

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

σ

teacher=“Silviu”

(Class)

date teacher resp name num

2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

9/23

(19)

Rename: change the name of attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

ρ

resp→boss

(Class)

date teacher boss name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1

2017-11-28 Antoine Fabian Uncert. Data Mgmt 2

2017-12-05 Antoine Fabian Uncert. Data Mgmt 3

2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

(20)

Rename: change the name of attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

ρ

resp→boss

(Class)

date teacher boss name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

10/23

(21)

Rename: change the name of attributes

Class

date teacher resp name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1 2017-11-28 Antoine Fabian Uncert. Data Mgmt 2 2017-12-05 Antoine Fabian Uncert. Data Mgmt 3 2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

ρ

resp→boss

(Class)

date teacher boss name num

2017-11-21 Antoine Fabian Uncert. Data Mgmt 1

2017-11-28 Antoine Fabian Uncert. Data Mgmt 2

2017-12-05 Antoine Fabian Uncert. Data Mgmt 3

2017-12-12 Silviu Fabian Uncert. Data Mgmt 4

(22)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

11/23

(23)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

(24)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

11/23

(25)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

(26)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

11/23

(27)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

(28)

Union

• Take tuples occurring in one of the input tables

• Applies to two tables with the same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S2 id name 42 Zaphod B.

=

Students1 ∪ S2 id name 1 Arthur Dent 2 Ford Prefect 42 Zaphod B.

→ Duplicates are removed here as well

11/23

(29)

Product

Take all combinations of the input tables

Students id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200

1 Arthur Dent E242

2 Ford Prefect E200

2 Ford Prefect E242

(30)

Product

Take all combinations of the input tables Students

id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200 1 Arthur Dent E242 2 Ford Prefect E200 2 Ford Prefect E242

12/23

(31)

Product

Take all combinations of the input tables Students

id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200

1 Arthur Dent E242

2 Ford Prefect E200

2 Ford Prefect E242

(32)

Product

Take all combinations of the input tables Students

id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200 1 Arthur Dent E242 2 Ford Prefect E200 2 Ford Prefect E242

12/23

(33)

Product

Take all combinations of the input tables Students

id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200

1 Arthur Dent E242

2 Ford Prefect E200

2 Ford Prefect E242

(34)

Product

Take all combinations of the input tables Students

id name 1 Arthur Dent 2 Ford Prefect

×

Rooms room E200 E242

=

Students × Rooms

id name room

1 Arthur Dent E200 1 Arthur Dent E242 2 Ford Prefect E200 2 Ford Prefect E242

12/23

(35)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

(36)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

13/23

(37)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

(38)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

13/23

(39)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

(40)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28

Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

13/23

(41)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

(42)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(

Member

)

× Class

13/23

(43)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(Member) × Class

(44)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28 Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(Member) × Class

13/23

(45)

Join

→ Product is useful to express joins :

Member id class

1 UDM

2 UDM

./

Class

class date UDM Nov 21 ABC Nov 24 UDM Nov 28

=

Member ./ Class id class date 1 UDM Nov 21 1 UDM Nov 28 2 UDM Nov 21 2 UDM Nov 28

Express Member ./ Class with the previous operators:

Π

id,class,date

σ

class=class2

ρ

class→class2

(Member) × Class

(46)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3 id name 2 Ford Prefect

14/23

(47)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3

id name

2 Ford Prefect

(48)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3 id name 2 Ford Prefect

14/23

(49)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3

id name

2 Ford Prefect

(50)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3 id name 2 Ford Prefect

14/23

(51)

Difference

• Take tuples that are in one table but not in the other

• Applies to two tables with same attributes

Students1 id name 1 Arthur Dent 2 Ford Prefect

S3 id name 1 Arthur Dent 42 Zaphod B.

=

Students1 − S3

id name

2 Ford Prefect

(52)

Table of contents

Basics

Relational algebra

SQL

15/23

(53)

Basic relations

CREATE TABLE Students(id INT(6), name VARCHAR(30));

INSERT INTO Students VALUES (1, 'Arthur Dent');

INSERT INTO Students VALUES (2, 'Ford Prefect');

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect |

+---+---+

2 rows in set (0.00 sec)

(54)

Basic relations

CREATE TABLE Students(id INT(6), name VARCHAR(30));

INSERT INTO Students VALUES (1, 'Arthur Dent');

INSERT INTO Students VALUES (2, 'Ford Prefect');

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+ 2 rows in set (0.00 sec)

16/23

(55)

Basic relations

CREATE TABLE Students(id INT(6), name VARCHAR(30));

INSERT INTO Students VALUES (1, 'Arthur Dent');

INSERT INTO Students VALUES (2, 'Ford Prefect');

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

2 rows in set (0.00 sec)

(56)

Projection and rename

SELECT name FROM Students;

+---+

| name |

+---+

| Arthur Dent |

| Ford Prefect | +---+

SELECT name, id, id AS identifier FROM Students; +---+---+---+

| name | id | identifier | +---+---+---+

| Arthur Dent | 1 | 1 |

| Ford Prefect | 2 | 2 | +---+---+---+

17/23

(57)

Projection and rename

SELECT name FROM Students;

+---+

| name |

+---+

| Arthur Dent |

| Ford Prefect | +---+

SELECT name, id, id AS identifier FROM Students; +---+---+---+

| name | id | identifier | +---+---+---+

| Arthur Dent | 1 | 1 |

| Ford Prefect | 2 | 2 | +---+---+---+

(58)

Projection and rename

SELECT name FROM Students;

+---+

| name |

+---+

| Arthur Dent |

| Ford Prefect | +---+

SELECT name, id, id AS identifier FROM Students;

+---+---+---+

| name | id | identifier | +---+---+---+

| Arthur Dent | 1 | 1 |

| Ford Prefect | 2 | 2 | +---+---+---+

17/23

(59)

Projection and rename

SELECT name FROM Students;

+---+

| name |

+---+

| Arthur Dent |

| Ford Prefect | +---+

SELECT name, id, id AS identifier FROM Students;

+---+---+---+

| name | id | identifier | +---+---+---+

| Arthur Dent | 1 | 1 |

| Ford Prefect | 2 | 2 | +---+---+---+

(60)

Selection

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Students WHERE id='2'; +---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

18/23

(61)

Selection

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Students WHERE id='2'; +---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

(62)

Selection

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Students WHERE id='2';

+---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

18/23

(63)

Selection

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Students WHERE id='2';

+---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

(64)

Union

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S2; +---+---+

| id | name | +---+---+

| 42 | Zaphod B | +---+---+

(SELECT * FROM Students) UNION

(SELECT * FROM S2); +---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect |

| 42 | Zaphod B | +---+---+

19/23

(65)

Union

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S2;

+---+---+

| id | name | +---+---+

| 42 | Zaphod B | +---+---+

(SELECT * FROM Students) UNION

(SELECT * FROM S2); +---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect |

| 42 | Zaphod B | +---+---+

(66)

Union

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S2;

+---+---+

| id | name | +---+---+

| 42 | Zaphod B | +---+---+

(SELECT * FROM Students) UNION

(SELECT * FROM S2);

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect |

| 42 | Zaphod B | +---+---+

19/23

(67)

Union

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S2;

+---+---+

| id | name | +---+---+

| 42 | Zaphod B | +---+---+

(SELECT * FROM Students) UNION

(SELECT * FROM S2);

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect |

| 42 | Zaphod B | +---+---+

(68)

Product

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Rooms; +---+

| room | +---+

| E200 |

| E242 | +---+

SELECT * FROM Students, Rooms; +---+---+---+

| id | name | room | +---+---+---+

| 1 | Arthur Dent | E200 |

| 2 | Ford Prefect | E200 |

| 1 | Arthur Dent | E242 |

| 2 | Ford Prefect | E242 | +---+---+---+

20/23

(69)

Product

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Rooms;

+---+

| room | +---+

| E200 |

| E242 | +---+

SELECT * FROM Students, Rooms; +---+---+---+

| id | name | room | +---+---+---+

| 1 | Arthur Dent | E200 |

| 2 | Ford Prefect | E200 |

| 1 | Arthur Dent | E242 |

| 2 | Ford Prefect | E242 | +---+---+---+

(70)

Product

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Rooms;

+---+

| room | +---+

| E200 |

| E242 | +---+

SELECT * FROM Students, Rooms;

+---+---+---+

| id | name | room | +---+---+---+

| 1 | Arthur Dent | E200 |

| 2 | Ford Prefect | E200 |

| 1 | Arthur Dent | E242 |

| 2 | Ford Prefect | E242 | +---+---+---+

20/23

(71)

Product

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM Rooms;

+---+

| room | +---+

| E200 |

| E242 | +---+

SELECT * FROM Students, Rooms;

+---+---+---+

| id | name | room | +---+---+---+

| 1 | Arthur Dent | E200 |

| 2 | Ford Prefect | E200 |

| 1 | Arthur Dent | E242 |

| 2 | Ford Prefect | E242 | +---+---+---+

(72)

Join

SELECT * FROM Member;

+---+---+

| id | class | +---+---+

| 1 | UDM |

| 2 | UDM | +---+---+

SELECT * FROM Classes; +---+---+

| class | date | +---+---+

| UDM | Nov 21 |

| ABC | Nov 24 |

| UDM | Nov 28 | +---+---+

SELECT * FROM

Member NATURAL JOIN Classes; +---+---+---+

| class | id | date | +---+---+---+

| UDM | 1 | Nov 21 |

| UDM | 2 | Nov 21 |

| UDM | 1 | Nov 28 |

| UDM | 2 | Nov 28 | +---+---+---+

21/23

(73)

Join

SELECT * FROM Member;

+---+---+

| id | class | +---+---+

| 1 | UDM |

| 2 | UDM | +---+---+

SELECT * FROM Classes;

+---+---+

| class | date | +---+---+

| UDM | Nov 21 |

| ABC | Nov 24 |

| UDM | Nov 28 | +---+---+

SELECT * FROM

Member NATURAL JOIN Classes; +---+---+---+

| class | id | date | +---+---+---+

| UDM | 1 | Nov 21 |

| UDM | 2 | Nov 21 |

| UDM | 1 | Nov 28 |

| UDM | 2 | Nov 28 | +---+---+---+

(74)

Join

SELECT * FROM Member;

+---+---+

| id | class | +---+---+

| 1 | UDM |

| 2 | UDM | +---+---+

SELECT * FROM Classes;

+---+---+

| class | date | +---+---+

| UDM | Nov 21 |

| ABC | Nov 24 |

| UDM | Nov 28 | +---+---+

SELECT * FROM

Member NATURAL JOIN Classes;

+---+---+---+

| class | id | date | +---+---+---+

| UDM | 1 | Nov 21 |

| UDM | 2 | Nov 21 |

| UDM | 1 | Nov 28 |

| UDM | 2 | Nov 28 | +---+---+---+

21/23

(75)

Join

SELECT * FROM Member;

+---+---+

| id | class | +---+---+

| 1 | UDM |

| 2 | UDM | +---+---+

SELECT * FROM Classes;

+---+---+

| class | date | +---+---+

| UDM | Nov 21 |

| ABC | Nov 24 |

| UDM | Nov 28 | +---+---+

SELECT * FROM

Member NATURAL JOIN Classes;

+---+---+---+

| class | id | date | +---+---+---+

| UDM | 1 | Nov 21 |

| UDM | 2 | Nov 21 |

| UDM | 1 | Nov 28 |

| UDM | 2 | Nov 28 | +---+---+---+

(76)

Difference

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S3; +---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 42 | Zaphod B. | +---+---+

SELECT * FROM Students WHERE (id, name) NOT IN

(SELECT * FROM S3); +---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

22/23

(77)

Difference

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S3;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 42 | Zaphod B. | +---+---+

SELECT * FROM Students WHERE (id, name) NOT IN

(SELECT * FROM S3); +---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

(78)

Difference

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S3;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 42 | Zaphod B. | +---+---+

SELECT * FROM Students WHERE (id, name) NOT IN

(SELECT * FROM S3);

+---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

22/23

(79)

Difference

SELECT * FROM Students;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 2 | Ford Prefect | +---+---+

SELECT * FROM S3;

+---+---+

| id | name |

+---+---+

| 1 | Arthur Dent |

| 42 | Zaphod B. | +---+---+

SELECT * FROM Students WHERE (id, name) NOT IN

(SELECT * FROM S3);

+---+---+

| id | name |

+---+---+

| 2 | Ford Prefect | +---+---+

(80)

Composing operations

Our translation of:

SELECT * FROM

Member NATURAL JOIN Classes;

can be expressed as:

SELECT id, class, date FROM (SELECT * FROM

(SELECT id, class AS class2 FROM Member) sub1, Classes

) sub2

WHERE class = class2;

→ SQL can express the relational algebra

• ... but please,neverwrite queries like this!

23/23

(81)

Composing operations

Our translation of:

SELECT * FROM

Member NATURAL JOIN Classes;

can be expressed as:

SELECT id, class, date FROM (SELECT * FROM

(SELECT id, class AS class2 FROM Member) sub1, Classes

) sub2

WHERE class = class2;

→ SQL can express the relational algebra

• ... but please,neverwrite queries like this!

(82)

Composing operations

Our translation of:

SELECT * FROM

Member NATURAL JOIN Classes;

can be expressed as:

SELECT id, class, date FROM (SELECT * FROM

(SELECT id, class AS class2 FROM Member) sub1, Classes

) sub2

WHERE class = class2;

→ SQL can express the relational algebra

• ... but please,neverwrite queries like this!

23/23

(83)

Composing operations

Our translation of:

SELECT * FROM

Member NATURAL JOIN Classes;

can be expressed as:

SELECT id, class, date FROM (SELECT * FROM

(SELECT id, class AS class2 FROM Member) sub1, Classes

) sub2

WHERE class = class2;

→ SQL can express the relational algebra

• ... but please,neverwrite queries like this!

(84)

References i

Abiteboul, S., Hull, R., and Vianu, V. (1995).

Foundations of Databases.

Addison-Wesley.

http://webdam.inria.fr/Alice/pdfs/all.pdf.

ISO (2008).

ISO 9075:2008: SQL.

Références

Documents relatifs

There are at least three major reasons for incorporating daily mobility in public health research: (i) to identify specific population groups, including socially

Dans toute la feuille, K désigne un corps commutatif..

Montrer que l’on d´ efinit ainsi deux cartes compatibles de S n , qui munissent la sph` ere d’une structure de vari´ et´ e diff´ erentiable.. Tore de

En consid´ erant Z et Q comme des parties de R montrer que tous les points de Z sont isol´ es mais qu’aucun point de Q n’est isol´

2012: Research group leader Max Planck Institute/DE 2013: Associate Professor.. T ´el ´ecom

Antoine Amarilli 1 , Silviu Maniu 2 November 21st, 2017... Class 1 (Nov 21): Introduction and

• Catalog(sid, pid, cost), listing the prices charged for parts by suppliers3. Write the following queries in relational algebra and

Relational algebra on uncertain instances Remember from last class:.. • Extend relational algebra operators to