Databases
July 2011
4-juil.-11
n1
@ JL Sabourin
M Visani
- Universit de La Rochelle -
SCHEDULE
I. The relational model
II. The languages
III. Integrity constraints
IV. The PL/PGSQL language
4-juil.-11
n2
@ JL Sabourin
M Visani
- Universit de La Rochelle -
4-juil.-11
n3
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
n4
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n5
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
records
4-juil.-11
designation
139 Lamp rhapsody
248 Female shoes camargue
258 Male shoes camargue
416 Backpack dolpo
426 Backpack nepal
765 tent
unit_price
30.00
75.00
87.00
100.00
155.00
300.00
n6
producer
1623
1623
1623
1369
1369
1502
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Primary key
n7
@ JL Sabourin
M Visani
- Universit de La Rochelle -
3)
Foreign keys
Referenced table
Primary key
Referencing attribute #
Primary key
4-juil.-11
n8
@ JL Sabourin
M Visani
- Universit de La Rochelle -
3)
Foreign keys
n9
@ JL Sabourin
M Visani
- Universit de La Rochelle -
3)
Foreign keys
EXAMPLE
PRODUCTS
PRODUCER
Reference
Designation
Unit_price
Producer #
Producer_identifier
Name
Address
- The name of the attribute may not be the same in the two tables
(here Producer and Producer_identifier)
- If foreign keys are declared when creating the tables, the table
PRODUCER must be created BEFORE the table PRODUCTS
- If tables have to be dropped before dropping the foreign key
constraint, then the table PRODUCTS must be dropped BEFORE
the table PRODUCER
4-juil.-11
n10
@ JL Sabourin
M Visani
- Universit de La Rochelle -
3)
Foreign keys
4-juil.-11
n11
@ JL Sabourin
M Visani
- Universit de La Rochelle -
4)
Definition domain
of an attribute
4-juil.-11
n12
@ JL Sabourin
M Visani
- Universit de La Rochelle -
4)
Definition domain
of an attribute
4-juil.-11
n13
@ JL Sabourin
M Visani
- Universit de La Rochelle -
II.
Languages
4-juil.-11
n14
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
n15
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n16
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n17
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n18
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n19
@ JL Sabourin
M Visani
- Universit de La Rochelle -
1)
4-juil.-11
n20
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
4-juil.-11
n21
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
designation
139 Lamp rhapsody
248 Female shoes camargue
258 Male shoes camargue
416 Backpack dolpo
426 Backpack nepal
765 tent
unit_price
30.00
75.00
87.00
100.00
155.00
300.00
producer
1623
1623
1623
1369
1369
1502
4-juil.-11
designation
426 Backpack nepal
765 Tent
unit_price
155.00
300.00
n22
producer
1369
1502
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
reference
designation
139 Lamp rhapsody
248 Female shoes camargue
258 Male shoes camargue
416 Backpack dolpo
426 Backpack nepal
765 tent
R2 = reference, designation(PRODUCTS)
4-juil.-11
n23
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
SELECTION + PROJECTION
R1 = unit_price>=155(PRODUCTS)
R2 = reference, designation(R1)
TABLE PRODUCTS
reference
designation
139 Lamp rhapsody
248 Female shoes camargue
258 Male shoes camargue
416 Backpack dolpo
426 Backpack nepal
765 tent
unit_price
30.00
75.00
87.00
100.00
155.00
300.00
producer
1623
1623
1623
1369
1369
1502
TABLE R2
reference
4-juil.-11
designation
426 Backpack npal
765 Tent
n24
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
designation
139 Lamp rhapsody
248 Female shoes camargue
258 Male shoes camargue
416 Backpack dolpo
426 Backpack nepal
765 tent
and
unit_price
30.00
75.00
87.00
100.00
155.00
300.00
producer
1623
1623
1623
1369
1369
1502
S =PRODUCERS
Producer_id Name
address
1369
denecker sarl
Lyon
1370
chen'alpe diffussion
Lyon
1502
rodenas francois
Toulouse
1623
adidas
Dettwiller
n25
@ JL Sabourin
M Visani
- Universit de La Rochelle -
address
unit_price
producer
1369
denecker sarl
Lyon
reference
designation
30.00
1623
1369
denecker sarl
Lyon
75.00
1623
1369
denecker sarl
Lyon
87.00
1623
1369
denecker sarl
Lyon
100.00
1369
1369
denecker sarl
Lyon
155.00
1369
1369
denecker sarl
Lyon
765 tent
300.00
1502
1370
chen'alpe diffussion
Lyon
30.00
1623
1370
chen'alpe diffussion
Lyon
75.00
1623
1370
chen'alpe diffussion
Lyon
87.00
1623
1370
chen'alpe diffussion
Lyon
100.00
1369
1370
chen'alpe diffussion
Lyon
155.00
1369
1370
chen'alpe diffussion
Lyon
765 tent
300.00
1502
1502
rodenas francois
Toulouse
30.00
1623
1502
rodenas francois
Toulouse
75.00
1623
1502
rodenas francois
Toulouse
87.00
1623
1502
rodenas francois
Toulouse
100.00
1369
1502
rodenas francois
Toulouse
155.00
1369
1502
rodenas francois
Toulouse
765 tent
300.00
1502
1623
adidas
Dettwiller
30.00
1623
1623
adidas
Dettwiller
75.00
1623
1623
adidas
4-juil.-11
Dettwiller
258 Male
shoes camargue
n26
1623
adidas
Dettwiller
1623
@87.00
JL Sabourin
M Visani
100.00
1369
- Universit de La Rochelle -
2)
Relational algebra
Address
1369
1369
1502
1623
1623
denecker sarl
denecker sarl
rodenas francois
adidas
adidas
Lyon
Lyon
Toulouse
Dettwiller
Dettwiller
1623
adidas
Dettwiller
Reference
designation
Unit_pric producer
e
100.00 1369
155.00 1369
300.00 1502
30.00 1623
75.00 1623
87.00 1623
n27
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
n28
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
JOIN + PROJECTION
What are the references and unit prices of the products, for each
producer?
raison_sociale
denecker sarl
denecker sarl
rodenas francois
adidas
adidas
adidas
ref_produit prix_unitaire
416
100.00
426
155.00
765
300.00
139
30.00
248
75.00
258
87.00
n29
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
JOIN + SELECTION
Example: What are the products produced by Adidas?
Producer_id Name
Address
1623
1623
adidas
adidas
Dettwiller
Dettwiller
1623
adidas
Dettwiller
Reference
designation
Unit_pric producer
e
30.00 1623
75.00 1623
87.00 1623
n30
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
UNION
Example: What are the products which unit price is superior to 155,
or which are produced by Adidas?
Union is an INCLUSIVE OR.
Constraint: both tables in the UNION must share the same scheme:
- same number of attributes,
- in the same order,
- same types (definition domains)
4-juil.-11
n31
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
UNION
In our example, the schemes of the two tables are different => we
first have to build the two tables
Products which price >=155: SELECTION.
R1 = unit_price>=155(PRODUCTS)
Products
Products which are produced by Adidas: JOIN + SELECTION
R2= Products (producer) Producers (producer_id)
R3 = Producers.name="Adidas" (R2)
Building the same scheme:
R4= reference,designation(R1)
R5= reference,designation(R3)
Union of the two tables:
R6 = R4 R5
4-juil.-11
n32
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
DIFFERENCE
Like union, difference is applied on two tables with the same schema.
The result of R-S id the set of records from R which are not in S.
Difference is the only operator capable of expressing queries
including a NEGATION.
4-juil.-11
n33
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
DIFFERENCE
Example: Which products have never been ordered.
We consider that the table ORDER_LINES contains all the ordered
products (see relational scheme 1).
Building the list of products:
R1 = reference (PRODUCTS).
Building the list of the ordered products:
R2 = reference (ORDER_LINES)
Building the difference on identical schemes:
R3 = R1 - R2
4-juil.-11
n34
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
INTERSECTION
Example: Which products in our procduct table have been
ordered.
We consider that the table ORDER_LINES contains all the ordered
products (see relational scheme 1).
Building
Building the list of products:
R1 = reference (PRODUCTS).
Building the list of the ordered products:
R2 = reference (ORDER_LINES)
Building the intersect on identical schemes:
R3 = R1 R2
4-juil.-11
n35
@ JL Sabourin
M Visani
- Universit de La Rochelle -
2)
Relational algebra
4-juil.-11
n36
@ JL Sabourin
M Visani