Course Rm
Course Rm
Modeling a database
There are four stages in designing a database:
Logical
Model
Physical
Model
Modeling a database
- ER: Entity-Relation
- UML: Unified Modelling Language
Modélisation d’une base de données
Entity/Relationship Model
Entity
Property
Occurrence
Id
Relationship or association
Cardinality
Relational Model
Domain
The domain of an attribute corresponds to the set of values that this
attribute can take. This set can be finite or countable or it can be
infinite.
Example:
- Domain (colors) = {Blue, Red, Green, ...}
- Domaine (Names) = {Ali, Salah, Yasmine...}
Relational Model
Cartesian product of a set of domains
Relation
A relation is defined by a list of attributes A1, A2, A3,....,
An having respectively domain D1, D2, D3,....., Dn.
We note it R(A1, A2, A3,....., An) where R is the name of
the relation.
It is composed of a set of tuples (a1, a2, a3,....., an) or ai
Di i = 1, 2, 3,....., n, this set constituting a subset of
the Cartesian product: D1 x D2 x D3 x..... x Dn.
Relational Model
Tuple of a relation
A tuple of a relation simply designates a row in the table
representing this relation. An attribute would rather
designate a column in the same table.
Relational Model
Cardinality of a relation
The cardinality of a relation is the number of tuples in this
relation. The cardinality of a relation R will therefore be an
integer and is denoted:|R|.
Degree of a relation
Example:
Domains:
LNAME = { Zaoui, Badaoui }
FNAME = { Ali, Nora, Larbi }
BIRTHDATE = {Date between 1/1/1990 and
31/12/2020}
SPORT = { judo, tennis, foot }
Relation STUDENT
Student LNAME × FNAME × BIRTHDATE
STUDENT ={(Zaoui, Ali,1/1/1992), (Badaoui, Nora ,
2/2/1994) }
Relational Model
INSCRIPTION Relation
INSCRIPTION LNMAE × SPORT
INSCRIPTION={(Zaoui, judo),
(Zaoui, foot),(Badaoui, tennis)}
To easily visualize the content of a relation we use the tabular
representation.
Each line corresponds to a vector
Each column corresponds to a domain
Relational Model
Example
INSCRIPTION LNAME × SPORT
LNAME SPORT
Zaoui Judo
Zaoui Foot
Badoui Tennis
Relational Model
Diagram of a relation
Example:
Consider relation
CATALOG_PRICE (Product_Code, Designation , Supplier ,
Price).
This relation is defined by its attributes which are:
Product_Code, Designation, Supplier and
Price.
Its name is: CATALOG_PRICE.
Relational Model
Schema of a database
Key of a relation
One of the integrity constraints of a schema is the uniqueness of
identification of the tuples of a relation. This unique identification is
ensured by the notion of relation key.
- A key can be composed of a single attribute or a list of attributes
that characterizes a tuple (tuple) of the relationship in a unique way.
- A relation can have several keys. A key with a minimum of attributes
will be chosen as the primary key, the other possible keys are called
candidate keys.
- By convention, the primary key of a relation is underlined in a
relation schema.
Relational Model
Relational Model
Example
Composite key
Example 1
PRODUCT (NProd, prod_name, price)
NProd → prod_name
NProd → price
Therefore NProd is a key
Relational Model
Example 2
Consider the following relation:
Pursache(Num_customer, prod_num, pursache_date, Qty)
Suggest a primary key for this relation.
Foreign key
Example