IT - 304
Database Systems
for BS (IT)
Lecture 4:
Relational Database Model
Hasan Raza
Lecturer CS & IT
Govt. Postgraduate College, Sheikhupura
Relational Database Management System (RDBMS)
Recall: Database Management System (DBMS)
❑ DBMS is an application, which holds user data permanently and then
provide different operations on this data e.g., retrieval of data, insertion of
data, updation of data etc.
❑ it is a computerized system whose overall purpose is to maintain information
and to make that information available on demand.
■ RDBMS is an enhancement to DBMS. It is a system in which:
1 The data is perceived by the user as tables; and
2 The operators (or operations) at the user's disposal include atleast RESTRICT
(SELECT), PROJECT and JOIN.
v Relational Database:
A database in which data is perceived as tables
The Relational Model
■ It is a prescription for a way of representing data by means of
tables and a prescription for a way of manipulating such a
representation using some operators.
■ It is an abstract theory of data that is based on certain aspects
of mathematics like set theory and predicate logic.
■ The principles of relational model were originally laid down in
1969-70 by Dr. E.F.Codd.
Terms in Relational Model
■ Relation
■ Tuple, Attribute Employee
Attributes
■ Cardianlity, Degree Relation
■ Domain
Tuples Cardinality
Degree
Important terms and definitions
■ Scalar values (Atomic)
❑ at every row and column position in every table there is always exaclty one data value
■ Repeating Group
A repeating group is a column, or combination of columns that contain
several data values in each row
ID Name Age Max.Marks Marks
• RDBMS does not allow repeating groups
g Optimazation
a system component that determine how to implement user requests
g Catalog
set of system tables
Part and Types of Relation
■ A relation consists of two parts:
❑ Heading consisting of fixed set of attributes or columns.
❑ Body consisting of time varying set of tuples.
■ The schema or description of a relation is written as
❑ Named Relation: Relation_Name(Att-1,Att-2,..Att-n)
■ Base Relation
an autonomous named relation
■ Derived Relation
a relation consists of part of named relations or base relations
■ View
a named derived relation with no physical existence
■ Snapshot
a named derived relation with physical existence
Properties of Relations
■ There are no duplicate Tuples
■ Tuples are unordered
■ Attributes are unordered
■ All attributes have atomic values
Definition Summary
Informal Terms Formal Terms
Table or File Relation
Column or Field Attribute
All possible Column Values Domain
Row or Record Tuple
Table Definition Schema of a Relation
Populated Table State of the Relation
Relational keys
■ Super Key
❑ General definition:
■ A superkey is a group of single or multiple keys which identifies rows in a
table.
Possible Super Keys:
• EmpSSN
• EmpNum
• EmpName
Relational keys
■ Candidate key
❑ General definition:
■ A set of attributes which can uniquely identify each row in the table
❑ Relational Model Definition:
■ Let R be a relation. Then candidate key for R is a subset of the set of attributes of R say
K, such that:
1 Uniqueness Property: no two distinct tuples of R have the same value for K.
2 Irreducibility property: no proper subset of K has the uniqueness propert.
Possible Candidate Keys:
• ID
• NIC
Relational keys
■ Primary key
❑ is a unique identifier for the table, that is , a column or column combination with
the property that, at any given time, no two rows of the table contain same value
in that column or column combination.
❑ One of the candidate keys
❑ There is only One Primary Key in a relation.
❑ A Primary Key can’t contain NULL value.
■ Alternate Keys
❑ All candidate keys other than primary key are called alternate keys
Primary Key: ID
Alternate Key: NIC
Relational keys
Composite Key:
■ Sometimes a single attribute is not sufficient to uniquely identify a tuple. In
such situations, two or more attributes are combined together to form a key
and such a key is called Composite Key.
■ A Composite Key must satisfy the conditions of uniqueness & Minimality i.e.
Elimination of any part of key should & will destroy uniqueness.
Composite Key: OrderNo, ProductID
Relational keys
Surrogate Key
■ An artificial key which aims to uniquely identify each record is called a
surrogate key. These kind of key are unique because they are created
when you don't have any natural primary key. They do not lend any
meaning to the data in the table. Surrogate key is usually an integer.
Surrogate Key:
Start Time
Relational keys
■ Foreign key
❑ General definition:
■ A set of attributes in a table whose values are taken from the values of primary key of some
other table
❑ Relational Model Definition:
■ Let R2 be a relation. Then a foreign key in R2 is a subset of the set of attributes of R1, say FK,
such that:
1 there exists a base relation R1 (R1 and R2 not necessarily distinct) with a candidate key CK
and
2 for all time, each value of FK in the current value of R2 is identical to the value of CK in some
tuple in the current value of R1
Same values
Relational keys
■ Foreign key rules
❑ Restricted
❑ Cascade
❑ possible cases: update, delete
■ Integrity
accuracy or correctness of data in database
■ Integrity Rules
❑ inform DBMS of certain constraints in the real world.
❑ Weights cant be naegative
❑ if city is Lahore then code is 042
■ Referential integrity
database must not contain any unmatced foreign key values
■ Nulls
❑ candidate keys shouldn’t have null values
Integrity Constraints or Integrity Rules
■ To maintain the accuracy & integrity of data in the database
■ There are the following classes and Types of Integrity Constraints:
a) Static or Schema Constraints
1. Domain Constraints
2. NOT NULL Constraint
3. Entity Integrity
4. Referential Integrity
b) Dynamic Constraints
5. Action Assertion / Business Rules
Integrity Constraints or Integrity Rules
1. Domain Constraints
■ Domain: Set of values that may be assigned to an attribute
Attribute Description Domain
Empno Set of all possible Employee Numbers (PK) Character : size 5
Emp_Name Set of all possible Employee Names Character : size 25
(NOT NULL)
Job CLERK, MANAGER, SALESMAN etc. Character : size 15
Sal Sal is between 6000 and 40000 Numeric : size 5
Rank Rank can be between 1 and 5 Numeric : size 1
Integrity Constraints or Integrity Rules
2. NOT NULL Constraint:
● It enforces that an attribute Ai of a relation R must have some value.
● Example: if student tuple must have a valid, non-Null value for the Name
attribute, then Name of STUDENT is required to be NOT NULL
3. Entity Integrity:
● All PK entries are unique and No Component of the PK of a relation is
allowed to have NULL values.
● Note: Some other attributes of a relation may also be constrained to
disallow null values, even though they are not members of the
primary key.
● Formally, t[PK] ≠ null for any tuple t in r(R)
Integrity Constraints or Integrity Rules
3. Referential Integrity:
■ It is a constraint involving two relations called referencing relation and referenced or target
relation.
■ It specifies a relationship among tuples in two relations.
• A tuple t1 in R1 (referencing relation) is said to reference a tuple t2 in R2 (referenced
relation) if t1[FK] = t2[PK].
■ The database must not contain any unmatched FK values.
■ The value in the foreign key column (or columns) FK of the referencing relation R1 can be
either:
1. A value of an existing primary key value of a corresponding primary key PK in the
referenced relation R2, or
2. a null.
■ In case (2), the FK in R1 should not be a part of PK of its containing relation.
Terms in Relational Model
■ Referenced tuple or Target tuple
■ Referencing relation
■ Referenced relation or Target relation
Referencing Target
relation Target tuple relation
Integrity Constraints or Integrity Rules
3. Referential Integrity:
CUSTOMER
Customer_ID Customer_Name Address City State Zip
ORDER
Order_ID Order_Date Customer_ID
ORDER_LINE
Order_ID Product_ID Quantity
PRODUCT
Product_ID Prduct_Desc Product_Finish Standard_Price On_Hand
Integrity Constraints or Integrity Rules
3. Referential Integrity:
Q: What should happen if an attempt is made to delete a record from a target
table, while some matching records exist in the referencing table?
■ There are three possible options:
a) Not Allowed. This is the default option.
b) Cascade: All the matching records in the referencing table will also be
deleted.
c) Nullify: Null will be placed in the FK. This option is only applicable if the FK
is not a part of PK.
Integrity Constraints or Integrity Rules
4. ActionAssertion
■ Control on the actions of the organization
■ “An Employee can avail a certain facility only after a certain period of
service.
Data Dictionary
Data Dictionary (D/D) contains metadata to provide detailed information of all
tables within the database.
Textbook
Carlos Coronel, Steve Morris, “Database Systems” Design, Implementation,
Management, 12th Ed. Course Technology, 2016”.
Reference book
□ Jeffrey Hoffer, “Modern Database Management ” Design, Implementation, Management,
10th Edition”
□ Thomas Connolly, “Database Systems: A Practical Approach to Design, Implementation
and Management (6th Ed.)”
□ Elmasri, “Fundamentals of Database Systems: (7th Ed.)”