DBMS Week4
DBMS Week4
Lecture # 7-8
Instructor
Rida Ayesha
Lecture # 7-8 Agenda:
• The Relational Model:
o Brief History of the Relational Model
o Terminologies
▪ Relational Data Structure/Database Relations
▪ Mathematical Relations
▪ Properties of Relations
▪ Basic Building Blocks of Relational Model (Class Activity 02)
▪ Business Rules (Sources, Importance, Translating)
o Types of Keys (Super Key, Candidate Keys, Primary Key, Alternate Keys, Foreign Key, Composite
Key, Compound Key, Surrogate Key/Artificial Key, Sort/Control Key,
Secondary Key) (Class Activity 03)
o Integrity Constraints
▪ Entity Integrity
▪ Referential Integrity
▪ Domain Integrity
▪ Key Constraints (PK, FK, Not Null, Check, Unique and Default)
3
Terminologies in the
Relational Model
4
Relational Data Structure/Database Relations
5
Relational Data Structure/Database Relations
(cont.)
Linking the Relational Tables:
• The relational tables are linked through a common attribute.
6
Relational Data Structure/Database Relations
(cont.)
Linking the Relational Tables (cont.):
8
Mathematical Relations
Given two sets (domains):
• Set A (Student ID) = {101, 102, 103}
• Set B (Student Name) = {Alice, Bob, Charlie}
This subset represents an actual student database table where each tuple (row)
represents a valid record.
9
Mathematical Relations (Connection to Database
Relations)
• In a database, a relation corresponds to a table.
• The Cartesian product represents all possible data combinations, but only a valid
subset forms the actual database table.
10
Properties of Relations
• Uniqueness: No duplicate rows (tuples)
11
Relational Model: Basic Building Blocks
1. Entity: Unique and distinct object used to collect and store data
12
Relational Model: Basic Building Blocks (cont.)
1. Entity
• An Entity is an Object, Event, Persons, Place or Concept about which the
organization wishes to maintain data.
• For Example: Doctor and Patient Entity Class, Student and Faculty Entity Class
etc.
STUDENT
13
Relational Model: Basic Building Blocks (cont.)
2. Attribute
3. Relationship:
• Describes an association among entities
▪ One-to-one (1:1)
▪ One-to-many (1:M)
▪ Many-to-many (M:N or M:M)
14
Relational Model: Basic Building Blocks (cont.)
For Example: Each citizen has exactly one passport, and each passport is assigned to only
one citizen.
15
Relational Model: Basic Building Blocks (cont.)
For Example: Consider a scenario with "Customers" and "Orders." One customer can
place many orders, but each order belongs to only one customer.
16
Relational Model: Basic Building Blocks (cont.)
For Example: Multiple customers (many) can be associated with one order (one).
17
Relational Model: Basic Building Blocks (cont.)
For Example: A student can be enrolled in multiple courses, and a course can have many
students enrolled. This is a many-to-many relationship between the "Student" and
"Course" entities.
18
Relational Model: Basic Building Blocks (cont.)
4. Constraint:
• The fourth and final basic building block is a constraint, which is a restriction
placed on the data.
• Constraints arise from business rules which are derived from a detailed
description of how an organization operates.
19
Business Rules
• Brief, precise, and unambiguous description of a policy, procedure, or principle
within a specific organization’s environment.
• For Example,
o A customer generate many invoices
o An agent can serve many customers, and each customer can be served by
only one agent.
20
Sources of Business Rules
Company Department
Policy makers
managers managers
Direct
Written
interviews
documentation
with end users
21
Importance of Business Rules
• Help standardize company’s view of data
22
Translating Business Rules Into Relational Model
Components
• Nouns translate into entities
23
Business Rules: Naming Conventions
Proper naming:
• Facilitates communication between parties
• Promotes self-documentation
24
Class Activity 02
25
Class Activity 02 - Solution
26
Types of Keys in the
Relational Model
27
Keys in Relational Model
What is a Key?
• A key is an attribute or set of attributes that uniquely identifies any record/tuple
from the table/relation.
Purpose:
• Key is used to uniquely identify any record or row of data from the table.
• It is also used to establish and identify relationships between tables.
Example:
Student table
28
Relational Keys - Types
• Super Key
• Candidate Keys
• Primary Key (PK)
• Alternate Keys
• Foreign Key (FK)
• Composite Key
• Compound Key
• Surrogate Key/Artificial Key
• Sort/Control Key
• Secondary Key
29
Relational Keys – Super Key
• A Super Key is a combination of all possible attributes that can uniquely identify
the rows/tuple in the given table/relation.
o Super key is a superset of a candidate key.
o A table/relation can have many super keys.
o A super key may contain extra attributes that are not necessary for achieving
uniqueness.
Student table
• Every candidate key is a super key, but not every super key is a candidate key.
• A primary key is chosen from the set of candidate keys.
Properties of Candidate key:
o It must contain unique values
o Candidate key in SQL may have multiple attributes
o Must not contain null values
31
Relational Keys – Candidate Key (cont.)
Candidate Keys
Student table
Candidate Keys:
1. {Stud_Id}
2. {Stud_Email}
3. {Stud_CNIC}
32
Relational Keys – Primary Key
• A Primary Key is one of the candidate key chosen by the database designer to
uniquely identify the record/tuple in the table/relation.
• Rules for defining Primary key:
o Primary key values are not allowed to accept NULL value
o The value of primary key must always be unique (not duplicate)
o The value of primary key must be assigned when inserting a record.
o The value in a primary key column can never be modified or updated if any foreign key
refers to that primary key.
o A relation is allowed to have only one primary key.
Student table
Primary
Key:
1. {Stud_Id}
33
Relational Keys – Alternate Keys
Alternate Key are the keys left behind after the selection of the primary key among
the possible candidate keys of an entity.
For Example: If Stud_id, Stud_CNIC and Stud_Email of a student are candidate keys and
Stud_Id is chosen as the Primary Key then Stud_CNIC and Stud_Email will be called Alternate
Keys.
Alternate Keys
Primary Key
Student table
Alternate Keys:
1. {Stud_CNIC}
2. {Stud_Email}
34
Relational Keys – Foreign Keys (FK)
• A Foreign Key is:
o A key used to link two tables together.
o An attribute (or set of attributes) in one table that refers to the Primary Key in another
table.
• The purpose of the foreign key is
o To ensure (or maintain) referential integrity of the data.
• Referenced relation may also be called as the master/primary/parent table.
• Referencing relation may also be called as the foreign/child table.
• Foreign key can take only those values which are present in the primary key of the
referenced table/relation.
• Foreign key may have a name other than that of primary key.
• Foreign key can take the NULL value.
• There is no restriction on a foreign key to be unique. In fact, foreign key is not
unique most of the time.
35
Relational Keys - Foreign Key (FK) (cont.)
• The values of DeptNo in the EMP relation should either be one of the values stored
in DEPT relation or it may optionally be NULL.
Table Name: EMP (Referencing Relation) Table Name: DEPT (Referenced Relation)
Foreign Key:
1. {DEPTNO} 36
Relational Keys – Composite Key
• A composite key is a candidate key that consists of two or more attributes that together
uniquely identify a record/tuple in the table/relation.
• Sometimes a single attribute is not sufficient to uniquely identify a record/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 will destroy uniqueness.
• Combination ensures uniqueness, if we analyse separately, there comes repeated values.
Order table
Composite Key:
1. {Cust_Id, Product_Code}
37
Relational Keys – Compound Key
• A compound key is a composite key for which each attribute that makes up the key is a
foreign key in its own right.
• A compound key is created when two or more primary keys from different tables are present
as foreign keys within an entity. The foreign keys are used together to uniquely identify each
record.
• Example: In an a hotel reservation system, a Reservation table has the compound key, (Guest_Id,
Hotel_Id, Arrival_Date). Guest_Id identifies a Guest, and references the Guests table. Hotel_Id identifies
a Hotel, and references the Hotels table.
Reservation table
Foreign keys
Compound Key:
1. {Guest_Id, Hotel_Id, Arrival_Date}
38
Relational Keys – Compound Key (cont.)
Statement Is it true?
A compound key must have at least 2 attributes ✅ Yes
All parts of a compound key must be foreign keys ❌ No
A compound key can include foreign keys and non-foreign keys ✅ Yes
39
Composite Key vs Compound Key
40
Relational Keys – Surrogate /Artificial Key
Foreign Keys 41
Relational Keys – Sort / Control Key
• An attribute or set of attributes that is used to physically sequence the stored data
is called Sort Key. It is also known as control key. The stored data can be sorted in
different ways according to the user requirement.
• Example: In the Student table below, the Stud_Marks attribute can be used as a
Sort key as it sorts all the tuples on the basis of Marks.
Student table
42
Relational Keys – Secondary Key
• A Secondary Key is an additional key, which can be use in addition to the primary
key to locate specific data.
• Non-unique attribute or set of attribute use to fetch the group of tuples from
relation.
• Use to retrieve the common unique data from the table.
• Example: In the Student table below, the Stud_Dept attribute can be used as a Secondary
Key as it groups the tuples on the basis of Department.
43
Relational Keys: Class Activity 03
Scenario:
You are given the following table from a university’s enrollment database.
Questions:
• List all possible Super Keys for this table.
• Identify Candidate Keys.
• Choose the most appropriate Primary Key (PK).
• Which attribute(s) can be considered Alternate Key(s)?
• Which column(s) can act as Foreign Key(s) if this table references Student, Course & Department tables?
• Can you spot a Composite Key in this table?
• Is there any Compound Key? If yes, identify it.
• What column could serve as a Surrogate/Artificial Key? Why?
• Which attribute would you use as a Sort/Control Key for generating reports?
• Suggest a possible Secondary Key and justify your choice.
44
Relational Keys: Class Activity 03 (Solution)
45
Integrity Constraints in
the Relational Model
46
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:
b) Dynamic Constraints
5. Action Assertion / Business Rules
47
Integrity Constraints or Integrity Rules (cont.)
1. Domain Constraint:
48
Integrity Constraints or Integrity Rules (cont.)
49
Integrity Constraints or Integrity Rules (cont.)
2. Entity Integrity:
50
Integrity Constraints or Integrity Rules (cont.)
3. Referential Integrity:
• It is a constraint involving two relations called referencing relation and
referenced or target relation.
• The value in the foreign key column (or columns) FK of the referencing relation
R1 can be either:
o A value of an existing primary key value of a corresponding primary
key PK in the referenced relation R2, or
o a null.
• Foreign Key in one table must refer to a valid Primary Key in another
table
52
Integrity Constraints or Integrity Rules (cont.)
3. Referential Integrity (cont.):
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?
53
Integrity Constraints or Integrity Rules (cont.)
4. Key Constraints:
• Within databases, a key constraint is a rule that defines how data in a column(s) can
be stored in a table.
55
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
56
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
57
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
58
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
v. Check Constraints
• A check constraint enforces data integrity by allowing you to specify conditions that
must be met for data to be inserted into a column.
• For example, you could use a check constraint to ensure that only positive integer
values are inserted into a particular column.
• Check constraints are usually used in combination with other constraints (such as NOT
NULL constraints) to enforce more complex rules.
• The following SQL creates a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint ensures that the age of a person
must be 18, or older and less than or equal to 60:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18 and Age<=60)
); 59
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
• To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:
60
Integrity Constraints or Integrity Rules (cont.)
Types of Key Constraints (cont.):
• The following SQL sets a DEFAULT value for • The DEFAULT constraint can also be used
the "City" column when the "Persons" to insert system values, by using functions
table is created: like GETDATE():
61
Integrity Constraints or Integrity Rules (cont.)
62
Integrity Constraints or Integrity Rules (cont.)
5. Action Assertion
• ‘’An Employee can avail a certain facility only after a certain period of service’’.
63
Representing Relational Database Schemas
64
Representing Relational Database Schemas (cont.)
65