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

Lecture 2

RELATIONAL
MODEL
TYPES OF DATABASE MODEL
POPULATED DATABASE STATE FOR COMPANY
POPULATED DATABASE STATE FOR COMPANY
ENTITIES AND ATTRIBUTES

Two entities, EMPLOYEE e1 and

COMPANY c1 and their attributes


ENTITIES AND ATTRIBUTES EXAMPLE
ATTRIBUTE

Attribute Each attribute of a relation has a name

Set of allowed values for each attribute is called the


domain

Attribute values are (normally) required to be atomic

The special value null is a member of every domain


SIMPLE VS COMPOSITE ATTRIBUTE

 Simple Attributes are atomic values, which cannot be divided


further.
Ssn

 Composite Attributes are made of more than one simple


attribute.
RELATION SCHEMA

Relation schema R

 Denoted by R(A1, A2, ...,An)

 Made up of a relation name R and a list of attributes, A1, A2, ..., An

 Schema of a relation consists of


 attribute definitions
 name
 type/domain
 integrity constraints
RELATIONAL SCHEMA - COMPANY
DATABASE
RELATIONAL INSTANCE

 The current values (relation instance) of a relation are specified by


a table
 An element t of r is a tuple, represented by a row in a table
 Order of tuples is irrelevant (tuples may be stored in an arbitrary
order)

Tuples
DATABASE AND RELATIONS

 A database consists of multiple relations


 Information about company is broken up into parts, with each
relation storing one part of the information
 E.g. Employee: information about employee
and their department
Department: information about department
Dept_locations: information about department
locations.
WHY SPLIT INFORMATION ACROSS
RELATIONS?

 Storing all information as a single relation such as

Company(Employee, Department, Department_Loc, ..)

results in
 repetition of information

 the need for null values


 Normalization deals with the

designing of relational schemas.


KEYS
Foreign

Alternative Primary

KEY

Candidate Superkey
CANDIDATE KEY

 It must contain unique values


 Candidate key may have multiple attributes
 It should contain minimum fields to ensure uniqueness
 Uniquely identify each record in a table
 Eg. Ssn : Candidate Key

Ssn, Ename: Not Candidate Key


CANDIDATE KEY EXAMPLE

SSN and Dname can be considered as candidate keys  


CANDIDATE KEY DRILL

 In the given table which columns can be considered as


candidate keys which help us to uniquely identify the student
record in the table?

StudID Roll No  FirstName  LastName  Email 


1  11  Tom  Price  abc@gmail.com

2  12  Nick  Wright  xyz@gmail.com

3  13  Dana  Natan  mno@yahoo.com


KEY CONSTRAINTS
SIMPLE VS COMPOUND CANDIDATE
KEY

Ssn: Simple Key


Fname, Minit, Lname: Compound Key
Fname, Address ????
PRIME VS NON-PRIME ATTRIBUTE

Relation R
Attributes

Non-prime
Prime Attributes
Attributes

Belonging to Not belonging to


Candidate Keys Candidate Keys
PRIMARY KEY

 Two rows can't have the same primary key value 


 The primary key field cannot be null.
 The value in a primary key column can never be modified or
updated if any foreign key refers to that primary key.
 Atmost one primary key for any relation.

i.e. any candidate key whose values are always NOT NULL.
 Eg. Ssn: Primary Key (no null value allowed)
PRIMARY KEY DRILL

Identify Primary Key??


ALTERNATIVE KEY

 All candidate keys except primary keys.

 Allows NULL value

 More than 1 alternate key possible

 Eg. { Ename, Address, Bdate }


ALTERNATIVE KEY EXAMPLE

Ssn, Dnumber, Dname are qualified to become a primary key. But since Ssn,
Dnumber are the primary key, DName becomes the alternative key. 
ALTERNATIVE KEY DRILL

Identify the candidate, primary and alternative

key in the following SCHOOL Database.

StudID Roll No  FirstName  LastName  Email 


1  11  Tom  Price  abc@gmail.com

2  12  Nick  Wright  xyz@gmail.com

3  13  Dana  Natan  mno@yahoo.com


SOLUTION - ALTERNATIVE KEY
DRILL
SUPERKEY

A superkey is a group of single or multiple keys which identifies rows


in a table. A Super key may have additional attributes that are not
needed for unique identification.

EmpSSN EmpNum Empname


9812345098  AB05  Shown 
9876512345  AB06  Roslyn 
199937890  AB07  James 

 Eg. EmpSSN and EmpNum name are superkeys.


PRIMARY, CANDIDATE AND SUPER
KEY

Minimal SuperKey Set of attributes that


uniquely identifies records

Candidate key with NO


NULL values
FOREIGN KEY

 A foreign key is a set of attributes to create a relationship

with another relation.

 Foreign keys help us to maintain data integrity and also allows

navigation between two

different instances of an entity.


FOREIGN KEY EXAMPLE
FOREIGN KEY DRILL

Identify the primary and foreign key in the following SCHOOL


database.

DeptCode DeptName
001  Science 
002  English 
005  Computer 

Teacher ID Fname Lname


B002  David  Warner 
B017  Sara  Joseph 
B009  Mike  Brunton 
SOLUTION - FOREIGN KEY DRILL

In this table, adding the foreign key in Deptcode to the Teacher


name, creates a relationship between the two tables. 

This concept is also known as Referential Integrity.

Teacher ID DeptCode Fname Lname


B002  002  David  Warner 
B017  002  Sara  Joseph 
B009  001  Mike  Brunton 
INTEGRITY CONSTRAINTS
REFERENTIAL INTEGRITY
CONSTRAINT
Referenced Relation

Referencing
Relation
REFERENTIAL INTEGRITY CONSTRAINT
(CONT.)
 Tuples in the referencing relation R1 have attributes FK (called foreign
key attributes) that reference the primary key attributes PK of the
referenced relation R2.

A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].

R2 R1
……. ……. ……. ……. PK ……. ……. FK
 The value in the foreign key column (or columns) i.e. FK of the the
referencing relation R1 can be either:
a) a value of an existing primary key value of a corresponding primary key
PK in the referenced relation R2,
or
b) a null.
REFERENTIAL INTEGRITY
CONSTRAINTS

Referential integrity also includes the rules that dictate

what types of data manipulation are allowed on

referenced values and how these actions affect dependent

values.
REFERENTIAL INTEGRITY
CONSTRAINTS

Insertion No Violation

Referenced ● ON DELETE, NO ACTION


Relation
Deletion ● ON DELETE, CASCADE
( DEPT ) ● ON DELETE, SET NULL

Updation ● ON UPDATE, NO ACTION


● ON UPDATE, CASCADE
Referential ● ON UPDATE, SET NULL
Integrity
Constraint Insertion Restricted

Referencing Deletion
Relation No Violation
( EMP )
Updation Restricted
REFERENTIAL INTEGRITY CONSTRAINT -
INSERTION

Insertion in Referenced
Relation is allowed

Insertion in Referencing
Relation is restricted
DELETION/UPDATION IN REFERENCED
RELATION

ON DELETE, CASCADE

ON UPDATE, CASCADE
DELETION/UPDATION IN REFERENCED
RELATION

ON DELETE, SET NULL

ON UPDATE, SET NULL


DELETION/UPDATION IN REFERENCED
RELATION

ON DELETE, NO ACTION

ON UPDATE, NO ACTION
DELETION IN REFERENCING RELATION
UPDATION IN REFERENCING RELATION
PRIMARY KEY VS FOREIGN KEY

Primary Key Foreign Key


• Helps you to uniquely identify a • It is a field in the table that is the
record in the table.  primary key of another table. 

• A foreign key may accept multiple


• Primary Key never accept null null values. 
values. 
• A foreign key cannot automatically
• Primary key is a clustered index and create an index, clustered or non-
data in the DBMS table are clustered. However, you can
physically organized in the sequence manually create an index on the
of the clustered index.  foreign key. 

• You can have multiple foreign keys


• You can have the single Primary key in a table. 
in a table. 
SUMMARY
THANKS!!

You might also like