0% found this document useful (0 votes)
15 views28 pages

EN - Database Principle-C3 - relationalDB

Chapter 3 discusses the relational data model, which uses set theory and first-order logic to represent data in tables with attributes and relationships. It covers key concepts such as relation schemas, constraints, and the transformation of entity-relationship models into relational models. The chapter also explains various types of relationships, including one-to-one, one-to-many, and many-to-many, along with the associated constraints and keys.

Uploaded by

nlthcloneacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views28 pages

EN - Database Principle-C3 - relationalDB

Chapter 3 discusses the relational data model, which uses set theory and first-order logic to represent data in tables with attributes and relationships. It covers key concepts such as relation schemas, constraints, and the transformation of entity-relationship models into relational models. The chapter also explains various types of relationships, including one-to-one, one-to-many, and many-to-many, along with the associated constraints and keys.

Uploaded by

nlthcloneacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 3: RELATIONAL DATA MODEL

Lecturer: PhD. DO Thi Ngoc Diep


SCHOOL OF ELECTRICAL AND ELECTRONIC ENGINEERING
HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY

1
Review

• Entity relationship model?


• Entity, weak entity?
• Attributes, types of attributes?
• Relationships, types of relationships?

2
3.1. General concepts

• Relational data model uses set theory and first-order logic to


represent data.
• Suppose an object need to be stored in the database =>
• Define object attributes
• Define type and value domain for attributes

3
Relational model

• The current values of a relation are specified by a table


• Attribute values are (normally) required to be atomic (indivisible)
• The set of allowed values for each attribute is called the domain of the
attribute
• The special attribute value NULL = “unknown”
• An instance of relation <=> a tuple of attributes <=> a row/record in the
table

4
Relational model

• The current values of a relation are specified by a table :


• Cardinality of relation: number of rows/records of the table
• Degree of relation: number of columns/fields of the table
• None of two rows have the same set of values
• The order of columns/rows is not important

Degree = ?
Cardinality = ?

5
Relational model

• Comparison with ER model:


• Each table corresponds to a set of entities.
• Each column corresponds to an attribute.
• Each row is a set of real values of the a ributes, corresponding to an entity

Student

ID
Class
Full name Sex
Date of birth Address
6
3.2. Relation Schema

• A1, A2, …, An are attributes


• R = (A1, A2, …, An ) is a relation schema
• A relation r defined over schema R is denoted by r (R)

• 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

• BOOK(id, title, author, pub-year)


BOOK
Id Title Author Pub-year

1 The call of the wild Jack London 1903

2 The universe in a nutshell Stephen Hawking 2001

3 Hồng lâu mộng Tào Tuyết Cần 1791

• 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

• Constraint = rule to check data to satisfy certain conditions:


• Semantic constraints:
• Data belongs to a value domain, be smaller than 1 value, …
• Data belongs to a fixed format
• Key constraints
• Entity constraints
• Reference constraints

9
Key constraints

• None of two rows have the same set of values


• Superkey is used to uniquely identify a tuple in a relation.
• Given a relation R defined on the attribute set U = {A1, A2, …, An}.
• A subset K  U is called a super key of relation R if for any two tuples t1,
t2 in relation R: t1 [K]  t2 [K]

Superkeys: {Name, Salary, Eid} , {Eid}, {Name, Birthdate}


EMPLOYEE
Department Eid Name Birthday Salary CellPhone
D1 20001234 Nguyen Van A November 22, 30000 0912304101
1970
D1 20012322 Do Van Khoi February 1, 1978 27000 NULL
D2 20000980 Vu Minh April 30, 1967 50000 0903132123
D3 19991323 Nguyen Van B October 10, 50000 NULL
1966

10
Key constraints

Khóa tối thiểu

• Candidate key: K is a candidate key, if no subset of K can be a super


key: ! K' K where K' is a superkey
• A minimal super key, Removing any attribute causes the remaining
attributes to no longer be a super key
• Primary Key: The best candidate key.
• Each relation can have at most 1 primary key.
Superkeys : {Name, Salary, Eid}, {Eid}, {Name, Birthdate}
Candidate Key : {Eid}, {Name, Birthdate}
EMPLOYEE Primary key : {Eid}
Department Eid Name Birthday Salary CellPhone
D1 20001234 Nguyen Van A November 22, 30000 0912304101
1970
D1 20012322 Do Van Khoi February 1, 1978 27000 NULL
D2 20000980 Vu Minh April 30, 1967 50000 0903132123
D3 19991323 Nguyen Van B October 10, 50000 NULL
1966

11
Key constraints

• Foreign key: Phải tham chiếu đến 1 khóa chính của bảng khác

• One or several attributes which point/refer to tuples in another relation


• K is a foreign key of R1 that refers to relation R2 if K is a primary key of R2
• A set of attributes that belong to a relation, are not keys of that relation,
but are primary keys of another relation

Reference constraints

12
Entity constraints, Reference constraints

• Entity constraint: No attribute in the primary key can be assigned a


NULL value.
• Reference constraints by foreign keys:
• Identified between two relationships
• Maintains the association between tuples belonging to two relations
• If the foreign key is not null then there must be a tuple in the referenced
relationship where the primary key has the same value as the foreign key

13
Check Constraints

• Data in a relational database changes continuously over time.


• When there is an operation on the database => need to check the
constraints
• Add operations: data type constraints, semantic constraints, key constraints,
reference constraints,…
• Edit operations: data type constraints, semantic constraints, key constraints,
reference constraints,…
• Delete operations: reference constraints,…

14
Check Constraints
ACCOUNT
CUSTOMER ACCOUNT HOLDER branchName balance accountNumber

customer Name Address customer account HaThanh 20000 C-12894349


Number Number Number DongDo 20000 C-12894350
111111 Anh Hai Ba Trung 111111 C-12894349 DongDo 3500 S-141510751
121314 Van Anh Hai Ba Trung 121314 C-12894350 HaThanh 50000 S-520522620
515016 Son Hoan Kiem 121314 S-141510751
BRANCH
515016 S-520522620
branchName address assets
111111 C-12894350
HaThanh Hai Ba Trung 900000000
DongDo Dong Da 400000000
ThangLong Hoan Kiem 500000000

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

• Mapping of Regular Entity Types


• Mapping of Weak Entity Types
• Mapping of Binary 1:1 Relation Types
• Mapping of Binary 1:N Relationship Types
• Mapping of Binary M:N Relationship Types
• Mapping of Multivalued attributes
• Mapping of Ternary Relationship Types

16
3.4. Transform ER to Relational Model

• 1 entity set <=> 1 relation


• a ribute → column
• en ty → row/record/tuple
• Primary key of entity set → Primary key of relation

=> EMPLOYEE( EID , Name, Address, Birthdate, Salary)

17
3.4. Transform ER to Relational Model

• Transform composite attributes:

EMPLOYEE( Eid , Name, Address, …)


EMPLOYEE( Eid , Name, Number, Street, City, …)

18
3.4. Transform ER to Relational Model

• Transform multi-valued attributes


• Add a new entity

EMPLOYEE( Eid , Name, Address, Birthdate, Salary)


EMP_DEGREES( Eid , Degree )

19
3.4. Transform ER to Relational Model

• Transform weak entity

EMPLOYEE( EID , EName, Address, …)


EMP-DEPENDENT( EID , DName , Relationship, …)

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

• Both side are Partial participations : From


Book( bid, name, author, publisher, pub_date )
Design( dsid , color, shape, bid, from ) // bid can be NULL !!! 1 1
Book set Design

Book( bid, name, author, publisher, pub_date )


Design( dsid , color, shape)
Book_Design( bid, dsid, from ) // or dsid as Primarykey, bid as foreign key. Need one more relation !!!

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)

• Use foreign key


Book( bid , name, publisher, pub_date , aid )
Thêm 1 cột
2 cách Author( aid , name, address)
• Add a new relation
Book( bid , name, publisher, pub_date) Thêm 1 bảng mới
Author( aid , name, address)
Book_Author(bid, aid) // Need one more relation !!!

22
3.4. Transform ER to Relational Model

• Many-to-many relationship
• Add a new relation

n n
Student register Class

Student(s id , name, birthday)


Class( cid , name, room, teacher)
Registration( sid , cid )

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

CALL ( Call_Identifier , Customer_Id , Source_Number , Destination_Number ,


TimeOfDay )

REGULAR_CALL ( Call_Identifier , Duration)


LONG_DISTANCE ( Call_Identifier , Duration, LongDistanceCarrier )
CELL_CALL ( Call_Identifier , Air_Time , Land_Time )

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

• A relational model is represented as a diagram: the logical


structure of the database

Database System Concepts – Siberschatz, Korth, Sudarshan


27
Exercise

• Teams

28

You might also like