Database System Lect 04
Database System Lect 04
Database Systems
for BS (IT)
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
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
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
Example: if student tuple must have a valid, non-Null value for the Name
relation.
It specifies a relationship among tuples in two relations.
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
CUSTOMER
ORDER
ORDER_LINE
PRODUCT
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
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.)”