ch02 The Relational Model
ch02 The Relational Model
Introduction to Databases
3
Why Study the Relational Model?
5
Relational Database: Definitions
Relational database: a set of relations
Relation: a table with rows and columns
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
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
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.
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.
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
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.
31
Views
Base Relation
View
32
Purpose of Views
Provides security mechanism by hiding parts
of database from certain users.
33
Thank you for listening