EN_database principle-c3_relationalDB
EN_database principle-c3_relationalDB
1
Review
2
3.1. General concepts
3
Relational model
4
Relational model
Degree = ?
Cardinality = ?
5
Relational model
Student
ID
Class
Full name Sex
Date of birth Address
6
3.2. Relation Schema
• Example:
• Full relation schema :
BOOK (id: integer, title: string, author: string, pub-year: integer)
• Short relation schema :
BOOK (id, title, author, pub-year)
7
For example
• Mathematically: The relation schema R is the Cartesian product of the attributes value
domains :
Book = dom ( int ) × dom (string) × dom (string) × dom ( int )
dom() denotes the value domain
8
3.3. Constraints in relational model
9
Key constraints
10
Key constraints
11
Key constraints
• Foreign key: Phải tham chiếu đến 1 khóa chính của bảng khác
Reference constraints
12
Entity constraints, Reference constraints
13
Check Constraints
14
Check Constraints
ACCOUNT
CUSTOMER ACCOUNT HOLDER branchName balance accountNumber
1. Add Account (HaThanh, 5000, S-20071280) => check S-20071280 corresponds to which
customerNumber?
2. Add Account-Holder(12334, C-12894350) check if there is customer 12334 ?
Is there account C-12894350 ?
3. Edit Account ( C-12894350 , 20000, DongDo ) update BRANCH for DD, TL
to Account (C-12894350, 5000, ThangLong )
4. Delete Customer with customerNumber = '111111' => Delete/update in related tables
15
3.4. Transform ER to Relational Model
16
3.4. Transform ER to Relational Model
17
3.4. Transform ER to Relational Model
18
3.4. Transform ER to Relational Model
19
3.4. Transform ER to Relational Model
20
3.4. Transform ER to Relational Model
From
1 1
• One-to-one Relationship Book set Design
Set: Dành cho
Book(bid, name, author, publisher, pub_date)
Design(dsid, color, shape)
• Both sides are Total participation : From
Book (bid , name, author, publisher, pub_date,
color, shape, from) // every book, every design 1 1
Book set Design
• One side is Total participation :
Book ( bid , name, author, publisher, pub_date , dsid, from) From
// every book is set a design
Design( dsid , color, shape ) // To keep also designs not for book 1 1
Book set Design
21
3.4. Transform ER to Relational Model
• One-to-many relationship
1 N
Author write Book
Book( bid , name, publisher, pub_date )
Author( aid , name, address)
22
3.4. Transform ER to Relational Model
• Many-to-many relationship
• Add a new relation
n n
Student register Class
23
3.4. Transform ER to Relational Model
• “ is-a” relationship
• Convert “parent” entity into a relation
• Convert “child” entity into a relation, containing only its own attributes
• Copy the key of the parent entity to the child entity
REGULAR_CALL
is-a
LONG_DISTANCE is-a CALL
is-a
CELL_CALL
24
3.4. Transform ER to Relational Model
REGULAR_CALL
is-a
LONG_DISTANCE is-a CALL
is-a
CELL_CALL
25
3.4. Transform ER to Relational Model
• Ternary Relationship
• Create a new relationship and add primary key
• 1:1:1 type
• 3 candidate keys:(Aid, Bid), (Aid, Cid), (Bid, Cid)
• 1:1:N type
• 2 candidate keys: (Aid, Bid), (Cid, Bid)
• 1:N:N type
• 1 candidate key: (Aid, Bid)
• N:N:N type
• 1 candidate key: (Aid, Bid, Cid)
26
Schema for a database
• Teams
28