Docsity

Database relational algebra, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download Database relational algebra and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

The Relational Algebra

Querying Databases

What is a query?

an expression in relational algebra, i.e., operations on tables

The result of a query on one or more table(s) is a table

Operations for filtering:

Selection (filter / criteria) = selection of rows
Projection = selection of columns

Operations for combining 2 tables:

Cartesian product (all possibilities)
Join (bridging between related rows)
Union (union of sets of rows)
Difference (elimination of rows)

Selection

• Choice of rows according to a Boolean condition

• s

condition

( Table )

Selection

• Choice of rows according to a Boolean condition

s

condition

( Table )

• Person

People living in London:

s

Address="London, UK"

(Person)

Name Age Address Birthplace James B. 47 London, UK London, UK Clark K. 35 Metropolis, USA Krypton Lois L. 28 Metropolis, USA Metropolis, USA Tarzan 25 London, UK Forest, Benin Name Age Address Birthplace James B. 47 London, UK London, UK Tarzan 25 London, UK Forest, Benin

Selection (contd.)

Persons being less than 30 y.o.:

• s

Age<

(Person)

Name Age Address Birthplace Lois L. 28 Metropolis Metropolis Tarzan 25 London, UK Forest, Benin

this is a table

Selection (contd.)

Persons being less than 30 y.o.:

s

Age<

(Person)

Name Age Address Birthplace Lois L. 28 Metropolis Metropolis Tarzan 25 London, UK Forest, Benin

this is a table too!

Selection (contd.)

People less than 30 y.o.:

s

Age<

(Person)

Composition of selection : people less than 30 living in

London:

s

Address="London, UK"

(s

Age<

(Person))

Conjunction of selection :

s

Address="London, UK" and Age<

(Person)

equivalent to the composition

Name Age Address Birthplace Lois L. 28 Metropolis Metropolis Tarzan 25 London, UK Forest, Benin Name Age Address Birthplace Tarzan 25 London, UK Forest, Benin

Projection

• Choice of columns in a table

p

column(s)

( Table )

Projection (contd.)

Name of people living in London:

• p

Name

(s

Address="London, UK"

(Person))

Name Age Address Birthplace James B. 47 London, UK London, UK Tarzan 25 London, UK Forest, Benin

Projection (contd.)

Name of people living in London:

• p

Name

(s

Address="London, UK"

(Person))

Name James B. Tarzan Name Age Address Birthplace James B. 47 London, UK London, UK Tarzan 25 London, UK Forest, Benin

Cartesian Product

• Each row of the first table is combined with all the rows of the

second table

Table

1

⊗ Table

2

• Man Woman

All man-woman pairs:

• Man ⊗ Woman

Name 1 Address 1 Name 2 Address 2 James B. London Lois L. Metropolis James B. London Catwoman Gotham City Clark K. Metropolis Lois L. Metropolis Clark K. Metropolis Catwoman Gotham City Tarzan London Lois L. Metropolis Tarzan London Catwoman Gotham City Name Address James B. London Clark K. Metropolis Tarzan London Name Address Lois L. Metropolis Catwoman Gotham City

Cartesian Product (contd.)

• Man City

In which country do the men of the Man table live?

1) Man ⊗ City

Name Address CName Population Country James B. London London 7,556,900 UK James B. London Metropolis 8,391,881 USA Clark K. Metropolis London 7,556,900 UK Clark K. Metropolis Metropolis 8,391,881 USA Tarzan London London. 7,556,900 UK Tarzan London Metropolis 8,391,881 USA Name Address James B. London Clark K. Metropolis Tarzan London CName Population Country London 7,556,900 UK Metropolis 8,391,881 USA

Cartesian Product (contd.)

Man-woman pairs living in the same city:

• s

Address 1 =Adrress 2

(Man ⊗ Woman)

Name 1 Address 1 Name 2 Address 2 James B. London Lois L. Metropolis James B. London Catwoman Gotham City Clark K. Metropolis Lois L. Metropolis Clark K. Metropolis Catwoman Gotham City Tarzan London Lois L. Metropolis Tarzan London Catwoman Gotham City

Cartesian Product (contd.)

Man-woman pairs living in the same city:

• s

Address 1 =Adrress 2

(Man ⊗ Woman)

Name 1 Address 1 Name 2 Address 2 James B. London Lois L. Metropolis James B. London Catwoman Gotham City Clark K. Metropolis Lois L. Metropolis Clark K. Metropolis Catwoman Gotham City Tarzan London Lois L. Metropolis Tarzan London Catwoman Gotham City Name 1 Address 1 Name 2 Address 2 Clark K. Metropolis Lois L. Metropolis