Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

CMP - 270

Database Systems
for BS (IT)

Lecture 4: Relational Database Model


Hareem Aslam
Hareem.aslam@pucit.edu.pk

Punjab University College of Information Technology (PUCIT)


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
attributes Employee Relation
 Cardianlity, Degree
 Domain
ID Name Age Department
S1 Ahmad 23 Sales
S2 Salman 34 Marketing
tuples S3 Karim 21 Sales Cardinality
S4 Tariq 29 Admin
S5 Sadiq 32 Sales

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


 Optimazation
a system component that determine how to implement user requests
 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.

EmpSSN EmpNum EmpName


Possible Super Keys: 9812345098 AB05 Shown
• EmpSSN
• EmpNum 9876512345 AB06 Roslyn
• EmpName 199937890 AB07 James
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.

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
Possible Candidate Keys: S2 Salman 34 Marketing 234-66-245368
• ID
S3 Karim 21 Sales 255-79-256369
• NIC
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
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
 Alternate Keys
 All candidate keys other than primary key are called alternate keys

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
Primary Key: ID S2 Salman 34 Marketing 234-66-245368
Alternate Key: NIC S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
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
OrderNo PorductID Product Quantity
Name
Composite Key: OrderNo, ProductID B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20

B004 DKT321573 USB 15


B002 OMG446789 Laser Printer 3
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.

Fname Lastname Start Time End Time

Surrogate Key: Anne Smith 9:00 18:00


Start Time Jack Francis 8:00 17:00
Anna McLean 11:00 20:00

Shown Willam 14:00 23:00


Relational keys
 Foreign key
 General definition:
 A set of attributes in a table whose values are taken from the values of candidate 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

ID Name Age Department NIC Department Location


S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368 Sales Floor 1
S3 Karim 21 Sales 255-79-256369 Marketing Floor 3
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
Admin Floor 5

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 relation Target relation


Target tuple

ID Name Age Department NIC


S1 Ahmad 23 Sales 245-77-245367
Department Location
S2 Salman 34 Marketing 234-66-245368 Sales Floor 1
S3 Karim 21 Sales 255-79-256369
Marketing Floor 3
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371 Admin Floor 5
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. Action Assertion
 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.
RELATIONAL ALGEBRA
What is Relational Algebra?

 Operators
 Relational Algebra consists of eight operators:
 Four traditional set operators: UNION, INTERSECTION, DIFFERENCE, CARTESIAN
PRODUCT
 Four special relational operators: RESTRICTION, PROJECTION, JOIN, DIVISION
 Operands
 Relations are the operands used in Relational Algebra
 Closure property of Relations
 output from some relational operator is always a relation.
Traditional Set Operators
 UNION
 the union of two type-compatible relations A and B (A UNION B) is a relation with the
same heading as each of A and B and with a body consisting of the set of all tuples
belonging to A and B.
 relations must have same heading
 same tuples are included once

B
Traditional Set Operators
 UNION
 Returns a relation consisting of all tuples appearing in either or both relations

A B
ID Name Age Department NIC ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367 S3 Karim 21 Sales 255-79-256369
S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371

A UNION B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
Traditional Set Operators
 INTERSECTION
 the intersection of two type-compatible relations A and B (A INTERSECT B) is a relation
with the same heading as each of A and B and with a body consisting of the set of all
tuples belonging to both A and B.
 relations must have same heading

A
B
Traditional Set Operators
 INTERSECTION
 Returns a relation consisting of all tuples appearing in both of two specified relations

A B
ID Name Age Department NIC ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367 S3 Karim 21 Sales 255-79-256369
S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371

A INTERSECT B
ID Name Age Department NIC
S4 Tariq 29 Admin 245-71-325370
Traditional Set Operators
 DIFFERENCE
 the difference between two type-compatible relations A and B, in that order (A MINUS
B) is a relation with the same heading as each of A and B and with a body consisting of
the set of all tuples belonging to A and not to B.
 relations must have same heading
 direction of operation does matter

A
B
Traditional Set Operators
 DIFFERENCE
 Returns a relation consisting of all tuples appearing in the first and not in the second of
two specified relations
A B
ID Name Age Department NIC ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367 S3 Karim 21 Sales 255-79-256369
S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371

A MINUS B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
B MINUS A
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S5 Sadiq 32 Sales 245-68-345371
Traditional Set Operators

 PRODUCT (CARTESIAN PRODUCT)


 the product of two relations A and B with no common attribute, (A TIMES B) is a
relation with a same heading as set of all attributes in each of A and B and with a
body consisting of the set of all tuples such that each resulting tuple is combination
of a tuple from A and a tuple from B.
 Cardinality of resulting relation is equal to the product of the cardinalities of A and

B.
 Degree of resulting relation is equal to the sum of the degrees of A and B.

 Returns a relation consisting of all possible tuples that are a combination of two
tuples, one from each of two specified relations.
Traditional Set Operators
 PRODUCT (CARTESIAN PRODUCT) B
A
ID Name Age Department NIC
S1 Ahmad 23 Sales 255-79-256369
S2 Salman 34 Admin 245-71-325370
S4 Tariq 29 Sales 245-68-345371
A TIMES B
ID Name Age Department NIC
S1 Ahmad 23 Sales 255-79-256369
S1 Ahmad 23 Admin 245-71-325370
S1 Ahmad 23 Sales 245-68-345371
S2 Salman 34 Sales 255-79-256369
S2 Salman 34 Admin 245-71-325370
S2 Salman 34 Sales 245-68-345371
S4 Tariq 29 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Sales 245-68-345371
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.)”

You might also like