RELATIONAL
MODEL
RELATIONAL MODEL
OBJECTIVES
To allow a high degree of data independence.
To provide substantial grounds for dealing with data
semantics, consistency, and redundancy problems.
TERMINOLOGY
Relation: A relation is a table with columns and rows.
A RDBMS requires only that the database be perceived by the
user as tables.
Attribute: An attribute is a named column of a relation.
A relation is represented as a two dimensional table in which the rows of the
table correspond to individual records and the table columns correspond to
attributes.
Domain:
EXAMPLE
DOMAIN
A domain is the set of allowable values for one or more
attributes.
Domains are an extremely powerful feature of the relational
model. Every attribute in a relation is defined on a domain
Tuple A tuple is a row of a relation.
The elements of a relation are the rows or tuples in the table.
In the Branch relation, each row contains four values, one for
each attribute.
Degree The degree of a relation is the number of attributes it
contains.
A relation with only one attribute would have degree one and
be called a unary relation or one-tuple. A relation with two
attributes is called binary, one with three attributes is called
ternary, and after that the term n-ary is usually used.
Cardinality The cardinality of a relation is the number of
tuples it contains.
EXERCISE
PROPERTIES OF
RELATIONS
the relation has a name that is distinct from all other relation
names in the relational schema;
each cell of the relation contains exactly one atomic (single)
value.
each attribute has a distinct name;
the values of an attribute are all from the same domain;
each tuple is distinct; there are no duplicate tuples;
the order of attributes has no significance;
the order of tuples has no significance, theoretically.
RELATIONAL KEYS
We need to be able to identify one or more attributes (called relational
keys) that uniquely identifies each tuple in a relation.
A DBMS key is an attribute or set of an attribute which helps you to identify
a row(tuple) in a relation(table).
WHY WE NEED KEYS?
Keys help you to identify any row of data in a table.
Allows you to establish a relationship between and identify the relation
between tables
Help you to enforce identity and integrity in the relationship
Super key An attribute, or set of attributes, that uniquely
identifies a tuple within a relation.
A superkey uniquely identifies each tuple within a relation.
However, a superkey may contain additional attributes that
are not necessary for unique identification
Candidate key A superkey such that no proper subset is a
superkey within the relation. CANDIDATE KEY is a set of
attributes that uniquely identify tuples in a table. Every table
must have at least a single candidate key. A table can have
multiple candidate keys but only a single primary key.
Uniqueness. In each tuple of R, the values of K uniquely identify that
tuple.
Irreducibility. No proper subset of K has the uniqueness property.
EXAMPLE
Super key: EmpSSN+EmpNum
EXAMPLE
PRIMARY KEY
The candidate key that is selected to identify tuples uniquely
within the relation.
Two rows can't have the same primary key value
It must for every row to have a primary key value.
The primary key field cannot be null.
KEY TYPES
Keys may be simple: one attribute (SSN), or
composite: a set of attributes whose values together uniquely
identify an entity type.
COMPOSITE KEY
FOREIGN KEY
FOREIGN KEY is a column that creates a relationship between
two tables.
The purpose of Foreign keys is to maintain data integrity and
allow navigation between two different instances of an entity.
When an attribute appears in more than one relation, its
appearance usually represents a relationship between tuples
of the two relations
EXAMPLE
In the previous example the two tables are not connected.
In this table, adding the foreign key in Deptcode to the Teacher
name, we can create a relationship between the two tables.
SUMMARY
A superkey is an attribute, or set of attributes, that identifies
tuples of a relation uniquely, and a
Candidate key is a minimal superkey.
A primary key is the candidate key chosen for use in
identification of tuples. A relation must always have a primary
key.
A foreign key is an attribute, or set of attributes, within one
relation that is the candidate key of another relation.
EXAMPLE
In a university system, the administration are offering set of
courses to be registered by students. The courses belong to
some specific departments. In addition each student is
supervised by a specific tutor. Each tutor can supervise many
students.
In order to design the database for the above scenario we need
to create the following entities
Student
Course
Department
Tutor
STUDENT
sID sName Address City BOD Phone
1 Ali Tela Ali Amman 1/1/99 789879890
2 Sami Naowr Amman 3/2/00 987654321
3 Aya Khalda Amman 2/2/01 965432890
4 Salma Masoom Zarqa 7/8/00 765432190
COURSE
cNum cTitle Credit
1 Java 3
2 DB 3
3 Networking 3
4 English 3
5 Arabic 3
DEPARTMENT
dNum dName Phone
1 CS 78654321
2 Computing 32456789
TUTOR
tID tName Office
1 Mohammed 201
2 Ahmad 202
3 Mariam 205
COURSE AND DEPARTMENT
Each course is offered by a department which means that we have a
relation between Courses and departments.
Each course is offered by one department and many courses are offered by
the same department.
The relation is called one to many.
In order to reflect one to many relations we need to use foreign key.
The primary key of department is repeated in the student table.
In department table, it is primary key. In students table it is foreign key.
Primary SOLUTION
Key
dNum dName Phone
Foreign
1 Computin 78654321
Key
g
2 General 32456789
cNum cTitle Credit dNum
1 Java 3 1
2 DB 3 1
3 Networking 3 1
4 English 3 2
5 Arabic 3 2
TUTOR AND STUDENTS
Tutor supervises many students
Student is supervised by one tutor
One to many relationship
One is the tutor
Many is the students
Primary key of tutor is used as a foreign key in student table.
Primary SOLUTION
Key
tID tName Office
1 Mohammed 201 Foreign
2 Ahmad 202 Key
3 Mariam 205
sID sName Address City BOD Phone tutorID
1 Ali Tela Ali Amman 1/1/99 78987989 1
0
2 Sami Naowr Amman 3/2/00 98765432 1
1
3 Aya Khalda Amman 2/2/01 96543289 2
0
4 Salma Masoom Zarqa 7/8/00 76543219 1
0
STUDENTS AND COURSES
Many students can register many courses
Many courses can be registered by many students
The relation is many to many.
Solution is to create new table that contains the primary key from student
and the primary key from courses.
Registration
sID cNum
1 1
Course
1 2
cNum cTitle Credit dNum
1 5
1 Java 3 1
2 3
2 DB 3 1
2 4
3 Networking 3 1
3 5
4 English 3 2
4 1
5 Arabic 3 2
Student
sID sName Address City BOD Phone tutorID
1 Ali Tela Ali Amman 1/1/99 78987989 1
0
2 Sami Naowr Amman 3/2/00 98765432 1
1
3 Aya Khalda Amman 2/2/01 96543289 2
EXERCISE
Book (ISBN, title, edition, year) contains details of book titles in the library
and the ISBN is an id that identifies each book.
BookCopy (copyNo, available) contains details of the individual copies of
books in the library
Borrower (borrowerNo, borrowerName, address) contains details of library
members who can borrow .
BookLoan (dateout, dateDue) contains details of the book copies that are
borrowed by library members.
Task: Identify the relationships between tables and specify the primary keys
and the foreign keys in each table.
SOLUTION
EXAMPLE