0% found this document useful (0 votes)
1 views

ch02 The Relational Model

Relational model

Uploaded by

sartmsbah7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

ch02 The Relational Model

Relational model

Uploaded by

sartmsbah7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Course Title

Introduction to Databases

Dr.Eng. Mahmoud Abu_Alfutuh


Chapter 2
The Relational Model
Chapter 2 - Objectives
Terminology of relational model.
How tables are used to represent data.
Connection between mathematical relations
and relations in the relational model.
Properties of database relations.
How to identify CK, PK, and FKs.
Meaning of entity integrity and referential
integrity.
Purpose and advantages of views.

3
Why Study the Relational Model?

Most widely used model


– Vendors: IBM, Microsoft, Oracle, …

Major advantages over older models:


– simple, easy to understand, express
complex queries with ease
Basic Definition
Entities and Attributes
– Entity is a person, place, event, or thing
about which data is collected
– Attributes are characteristics of the entity
Tables
– Holds related entities or entity set
– Also called relations
– Comprised of rows and columns

5
Relational Database: Definitions
Relational database: a set of relations
Relation: a table with rows and columns

sid name login age gpa


53666 Gad gad@cs 18 3.4
53688 Samy samy@cs 18 3.2
53650 Samy samy@math 19 3.8
Relational Model Terminology
Attribute is a named column of a relation.
Degree is the number of attributes in a relation.
The set of allowed values for each attribute is
called the Domain of the attribute

7
Examples of Attribute Domains

8
Relational Model Terminology
Tuple is a row of a relation.
Cardinality is the number of tuples in a relation.

9
Example Instance of Students Relation
Students

sid name login age gpa


53666 Gad gad@cs 18 3.4
53688 Samy samy@cs 18 3.2
53650 Samy samy@math 19 3.8

❑Cardinality = 3, degree = 5, all rows distinct


Terminology for Relational Database
Formal terms Alternative 1 Alternative 2
Relation Table File

Tuple Row Record

Attribute Column Field

11
Mathematical Definition of Relation
Consider two sets, D1 & D2,
where D1 = {A, B} and D2 = {1, 3, 5}.
Cartesian product, D1 × D2, is set of all ordered
pairs, where first element is member of D1 and
second element is member of D2.

D1 ×D2 = {(A, 1), (A, 3), (A, 5), (B, 1), (B, 3), (B, 5)}
Any subset of Cartesian product is a relation;
e.g. R = {(A, 1), (B, 1)}

12
Relation schema Example
Consider two sets, D1 (Name ) & D2 (City), where
Name = {Aly, Sally, … } /* Set of all names */
City = {Cairo, Tanta, … }. /* set of all citeis names*/

» Name × City = {(Aly, Cairo), (Aly, Tanta), (Saly, Cairo), (Saly, Tanta)}
Any subset of Cartesian product is a relation; e.g.
Then R = {(Aly, Cairo), (Saly, Tanta)} is a relation over
Name x City
Name City
Aly Cairo
Saly Tanta
13
Relation schema
Applying Mathematical Definition of Relation to
the database, we define:
– Relation schema
» specifies name of relation, plus name of each
columns.
Example:
Student ( sid, name, age, gpa)
– Relational database schema
» Set of relation schemas, each with a distinct
name. 14
Relation schema
Schema for the table
Graphical representation
Student
sid name age gpa

Text description
Student (sid, name, age, gpa)
15
Schemas and Instances
Schema – the logical structure of the database :
– Students (sid, name, age, gpa)
sid name age gpa

Instance – the actual content of the database at a


particular point in time
sid name age gpa
23 Aly 15 3.5
46 Hany 16 3.8
Properties of Relations
Relation name is distinct from all other relation
names in relational schema.
Each cell of relation contains exactly one atomic
(single) value.
Each attribute (column) has a distinct name.
Values of an attribute are all from the same
domain.

17
Properties of Relations (cont.)
Each tuple (row) is distinct; there are no
duplicate tuples.
Order of attributes (columns) has no significance.
Order of tuples (rows) has no significance.

18
Relational Keys
Superkey
– An attribute, or set of attributes, that uniquely
identifies a row within a relation.

Candidate Key
– A minimal Superkey. A superkey that does not
contain a subset of attributes that itself a
superkey.

19
Relational Keys Example
Consider the relation:
– Customer (customer_name, customer_street, city )
Superkeys:
– {customer_name, customer_street}
– {customer_name}
Candidate key:
{customer_name} is a candidate key, since it is a
superkey and no subset of it is a superkey.
Relational Keys
Primary Key (PK)
– An attribute or set of attributes that
uniquely identifies rows within a relation.

– Foreign Key (FK)


– Attribute, or set of attributes, within one
relation that matches Primary key of some
relation.

21
An Example of a Simple Relational Database

22
An Example of a Simple Relational Database

23
Integrity Constraints ‫قيود التكامل‬
There are two Integrity Constraints:
– Entity Integrity
– Referential Integrity
Before we define entity and referential integrity, we define Null.
Null: No data entry
Can represent
– An unknown attribute value
– A known, but missing, attribute value
– A “not applicable” condition
24
Integrity Constraints
Entity Integrity ‫( تكامل الكيان‬applies to PK)
– In a base relation, no attribute of a primary
key can be null.

Referential Integrity ‫( تكامل مرجعي‬applies to FK)


– If foreign key exists in a relation, either
foreign key value must match a primary key
value of some row in its home relation or
foreign key value must be wholly null.

25
An Illustration of Integrity Rules

26
Exercises: For each table, identify the primary key
and the foreign keys.
Students Exams
RegNum Surname FirstName City Age RegNum CourceCode Grade

1 Gad Hana Giza 15 2 C01 B

2 Samy Mary Banha 17 3 C01 C

3 Samy Taha Giza 14 2 C02 B

4 Tamer Hana Cairo 18 1 C02 D

Courses
Code Title Tutor
C01 Physics Aly
C02 Computers Samya
C03 Electronics Hany
27
A Sample Relational Database
Exercises
For each
table,
identify the
primary key
and the
foreign keys.

28
Exercises (Cont’d)
For each table, identify the primary key and the
foreign keys.
Do the tables exhibit entity integrity? Explain
So the tables exhibit referential integrity? Explain

29
Views
Base Relation
– Named relation corresponding to an entity
in conceptual schema, whose rows are
physically stored in database.

View
– Dynamic result of one or more relational
operations operating on base relations to
produce another relation.

30
Views
A virtual relation that is produced upon request.

Contents of a view are defined as a query on one


or more base relations.

Views are dynamic, meaning that changes made


to base relations that affect view attributes are
immediately reflected in the view.

31
Views
Base Relation

View

32
Purpose of Views
Provides security mechanism by hiding parts
of database from certain users.

Permits users to access data in a customized


way, according to their needs.

Can simplify complex operations on base


relations.

33
Thank you for listening

‫شكرا لحسن االستماع‬


35

You might also like