Anda di halaman 1dari 18

- Universit de La Rochelle -

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 -

I. The relational model


1) Relations
2) Primary key
3) Foreign keys
4) Definition domains of the attributes

4-juil.-11

n3

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The tables (relations)

Tables are LOGICAL structures (not physical structures)


Each table has a unique name. Each table consists in columns
(attributes) which name is also unique inside the table (i.e. 2
columns from 2 different tables may have the same name)
Data is stored in the table as a set of rows (records)
Tables are defined by:
R (A1: D1, A2: D2, A3: D3, An: Dn)
.
4-juil.-11

n4

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The tables (relations)

A table contains data relative to one given subject (e.g. the


clients, the stores, etc)
Each table has a primary key and other attributes that depend
on the primary key.
All the data of a database is stored in the tables (relations) of
the database

PRODUCTS (reference, designation, unit_price, producer)

4-juil.-11

n5

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The tables (relations)

EXAMPLE OF A TABLE: products


attributes
reference

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

The primary key of a table is an attribute or a group of attributes


which values is unique for every record in the table.
May be seen as a row identifier
- The values for each row are unique
- Integrity constraint: NOT NULL
- The primary key is minimal
A candidate key is an attribute or a group of attributes of the
table which verifies all the properties of the primary key
but the developer did not select it as a primary key
4-juil.-11

n7

@ JL Sabourin
M Visani

- Universit de La Rochelle -

3)

Foreign keys

- Foreign keys express the links between the tables


- A foreign key of a table (referencing table) references a set of
attributes from another table (referenced table)
- The referenced attribute is unique
- The referenced attribute is not necessarily the primary
key of the referenced table
- We use # to denote the foreign key (in the referencing table)
Referencing table

Referenced table

Primary key

Referencing attribute #

Primary key

Referenced attribute (unique)

4-juil.-11

n8

@ JL Sabourin
M Visani

- Universit de La Rochelle -

3)

Foreign keys

- Foreign keys characterize a rule


- Example:
Tables:
PRODUCTS (reference, designation, unit_price, producer#)
PRODUCER(producer_identifier, name,address)
Rule: Each product is produced by one and only one producer.
Or the producer is contained in the table PRODUCER, or it is
unknown (the value of the attribute producer is NULL).
4-juil.-11

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

-The attributes in the foreign key (referencing attributes) have


the SAME DOMAINE as the referenced attributes in the
referenced table.
- The value of a foreign key:
- may be NULL
- else, they MUST exist in the referenced table
This concept guaranties the REFERENTIAL INTEGRITY
CONSTRAINT

4-juil.-11

n11

@ JL Sabourin
M Visani

- Universit de La Rochelle -

4)

Definition domain
of an attribute

Each attribute of a given table belongs to one and only one


definition domain. It is defined by:
A type (values interval)
A length (for some types of data)
A format (example: DDMMYYY for the dates)

4-juil.-11

n12

@ JL Sabourin
M Visani

- Universit de La Rochelle -

4)

Definition domain
of an attribute

THE ATOMICITY(1st normal form)


For a given record, each attribute has only ONE value in the
definition domain.
Example:
Domain={ blue, red, green, yellow}.
The attribute color is defined on this domain

4-juil.-11

n13

@ JL Sabourin
M Visani

- Universit de La Rochelle -

II.

Languages

1) The different SQL languages


2) Relational algebra
3) Translation relational algebra -> SQL
4) About the SQL query language
5) Division

4-juil.-11

n14

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

Data Definition Language(DDL)


(Create, alter (modify), drop a structure: table, view,
trigger)
Data Manipulation Language (DML)
(Insert, delete, update data)
Data Query Language (DQL)
(select data, calculate, view)
Data Control Language (DCL)
(security, access control).
4-juil.-11

n15

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

DATA DEFINITION LANGUAGE


DDL enables the user to handle the STRUCTURE.
Creating/suppressing a database:
CREATE DATABASE and DROP DATABASE
Creating/suppressing a table:
CREATE TABLE and DROP TABLE.
Modifying a table: ALTER TABLE.

4-juil.-11

n16

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

DATA DEFINITION LANGUAGE


Creating a table :
CREATE TABLE.
create table command(
id_command char(3),
date_command date,
amount numeric(9,2)
);

4-juil.-11

n17

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

DATA MANIPULATION LANGUAGE


DDL enables the user to handle the DATA (records).
Inserting new record(s) in the table: INSERT
Modifying existing record(s) from a table: UPDATE
Deleting existing record(s) from a table: DELETE

4-juil.-11

n18

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

DATA QUERY LANGUAGE


Extracts records based on relational algebra
(projection, selection, join, union, intersection, difference)
Performs CALCULATIONS (sum, maximum, minimum, mean,
count, arithmetic operators) on records from a table
SORTS the data in ascending or descending order
Feeds VIEWS (see later).
Is based on one primitive only: SELECT

4-juil.-11

n19

@ JL Sabourin
M Visani

- Universit de La Rochelle -

1)

The different SQL


languages

DATA CONTROL LANGUAGE


GRANT statement
Gives privileges (or a role) to a user concerning statements
(create database, create table, create view, etc) on specific
structures (tables, views, columns or procedures)
REVOKE statement
Revokes the privileges of a user or of a role concerning
specific structures (tables, views, columns or procedures)

4-juil.-11

n20

@ JL Sabourin
M Visani

- Universit de La Rochelle -

2)

Relational algebra

Set of operations for handling tables (i.e. a set of records).


A query is an algebrical expression which is applied on a set of
tables and products a final table (result).
Relational algebra is composed of a set of both UNARY and
BINARY operators
Selection, projection, cartesian product, union, difference.

4-juil.-11

n21

@ JL Sabourin
M Visani

- Universit de La Rochelle -

2)

Relational algebra

SELECTION (unary operator)


Example: I want to extract from the table PRODUCTS only the
records which unit price is 155.
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

Selection corresponds to: R1 = unit_price>=155(PRODUCTS)


reference

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

PROJECTION (unary operator)


Projection a1,a2,a3,a4,ak(R) applies to a table R and extracts only the
attributes a1,a2,a3,ak from that table.
Therefore, contrary to the selection, the columns (and not the lines)
are suppressed
PRODUCTS

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

CARTESIAN PRODUCT (binary operator)


The first and most important binary operator is the cartesian
product x. The cartesian product between two tables R and S is
denoted by: R x S and creates a new table where each record from R
is associated to each record de S.
Given R=PRODUCTS
reference

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

Beware of the attributes which share the same name !!!!


4-juil.-11

n25

@ JL Sabourin
M Visani

- Universit de La Rochelle -

CARTESIAN PRODUCT result


Producer_id Name

address

unit_price

producer

1369

denecker sarl

Lyon

reference

139 Lamp rhapsody

designation

30.00

1623

1369

denecker sarl

Lyon

248 Female shoes camargue

75.00

1623

1369

denecker sarl

Lyon

258 Male shoes camargue

87.00

1623

1369

denecker sarl

Lyon

416 Backpack dolpo

100.00

1369

1369

denecker sarl

Lyon

426 Backpack nepal

155.00

1369

1369

denecker sarl

Lyon

765 tent

300.00

1502

1370

chen'alpe diffussion

Lyon

139 Lamp rhapsody

30.00

1623

1370

chen'alpe diffussion

Lyon

248 Female shoes camargue

75.00

1623

1370

chen'alpe diffussion

Lyon

258 Male shoes camargue

87.00

1623

1370

chen'alpe diffussion

Lyon

416 Backpack dolpo

100.00

1369

1370

chen'alpe diffussion

Lyon

426 Backpack nepal

155.00

1369

1370

chen'alpe diffussion

Lyon

765 tent

300.00

1502

1502

rodenas francois

Toulouse

139 Lamp rhapsody

30.00

1623

1502

rodenas francois

Toulouse

248 Female shoes camargue

75.00

1623

1502

rodenas francois

Toulouse

258 Male shoes camargue

87.00

1623

1502

rodenas francois

Toulouse

416 Backpack dolpo

100.00

1369

1502

rodenas francois

Toulouse

426 Backpack nepal

155.00

1369

1502

rodenas francois

Toulouse

765 tent

300.00

1502

1623

adidas

Dettwiller

139 Lamp rhapsody

30.00

1623

1623

adidas

Dettwiller

248 Female shoes camargue

75.00

1623

1623

adidas
4-juil.-11

Dettwiller

258 Male
shoes camargue
n26

1623

adidas

Dettwiller

416 Backpack dolpo

1623
@87.00
JL Sabourin
M Visani
100.00

1369

- Universit de La Rochelle -

2)

Relational algebra

CARTESIAN PRODUCT + SELECTION


R1= PRODUCTS x PRODUCER
R2 = producer=producer_id (R1)
Producer_id Name

Address

1369
1369
1502
1623
1623

denecker sarl
denecker sarl
rodenas francois
adidas
adidas

Lyon
Lyon
Toulouse
Dettwiller
Dettwiller

1623

adidas

Dettwiller

Reference

designation

416 backpack dolpo


426 backpack nepal
765 tent
139 lamp rhapsody
248 Female shoes
camargue
258 Male shoes camargue

Unit_pric producer
e
100.00 1369
155.00 1369
300.00 1502
30.00 1623
75.00 1623
87.00 1623

EQUALITY JOIN: cartesian product, then selection based on


the equality of 2 attributes (here the foreign key of
PRODUCTS and the primary key of PRODUCER)
4-juil.-11

n27

@ JL Sabourin
M Visani

- Universit de La Rochelle -

2)

Relational algebra

EQUALITY JOIN (or join)


R= Products (producer_id)  Producers (producer)
where  is the join between 2 atttributes.
The attributes must share the same type
But they are not necessarily linked by a foreign key
Join is a fundamental operation of the queries, for:
- creating links between different tables
- projecting attributes from different tables
- selecting records using criteria concerning different
tables
4-juil.-11

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

R1= Products (producer)  Producers (producer_id)


R2= name, reference,unit_price(R1)
Extracts only the chosen attributes from the result of
the join.
4-juil.-11

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

139 lamp rhapsody


248 Female shoes
camargue
258 Male shoes camargue

Unit_pric producer
e
30.00 1623
75.00 1623
87.00 1623

R1= Products (producer)  Producers (producer_id)


R2 = Producer.name="Adidas" (R1)
Only the records for which the producers name is
Adidas are extracted.
4-juil.-11

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

Exercises: exercise list n1

4-juil.-11

n36

@ JL Sabourin
M Visani

Anda mungkin juga menyukai