LM 4 - Relational Database Keys
LM 4 - Relational Database Keys
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Course Outcome
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Outline
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• The Relational Algebra
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Introduction to RDBMS
• Relational Database Management System (RDBMS) consists of:
– A set of tables
– A schema
• A schema:
– is a description of data in terms of data model
– Defines tables and their attributes (field or column)
Table 2
CourseID CourseName Credits
101 CSE 5
102 ECE 4
103 IT 3
Example
Table 3
Rollno CourseID
001 101
002 102
003 103
“STUDENT”
Table
Table (or) Relation
• Table is a collection of data items arranged in rows and columns.
• It cannot have duplicate data or rows.
3)Degree:
The number of attributes/columns in the relation is known as degree of the relation.
Eg: The STUDENT relation defined above has degree 5.
4)Cardinality:
The number of tuples in a relation is known as cardinality.
Eg: The STUDENT relation defined above has cardinality 4.
Terminologies
5)Relation Schema:
It describes the structure of the relation, with the name of the relation ,its attributes
and their type.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation
schema for STUDENT.
6)Relation Instance:
The set of tuples of a relation at a particular instance of time is called as relation
instance. Eg: A table contains records with marks above 90 .
Example of a Instructor Relation
attributes
(or columns)
tuples
(or rows
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Relation Schema and Instance
• A1, A2, …, An are attributes
• R = (A1, A2, …, An ) is a relation schema
Example:
instructor = (ID, name, dept_name, salary)
• A relation instance r defined over schema R is denoted by r (R).
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Terminologies
7)Domain:
For each attribute, there is a set of permitted values called domain.
8) Atomic:
The domain is atomic if elements of the domain are considered to be indivisible
units.
Eg: Phone No is non atomic
9)NULL Values:
The value which is not known or unavailable is called NULL value.
It is represented by blank space.
Eg.; PHONE of STUDENT having ROLL_NO 4 is NULL.
Relations are Unordered
• Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
• Example: instructor relation with unordered tuples
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Database Schema
• Database schema -- is the logical structure of the database.
• Database instance -- is a snapshot of the data in the database at a given
instant in time.
• Example:
• schema: instructor (ID, name, dept_name, salary)
• Instance:
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC
Keys
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Why we need a Key?
• Keys help you to identify any row of data in a table.
• In a real-world application, a table could contain thousands of
records and these records could be duplicated.
• Keys in RDBMS ensure that you can uniquely identify a table
record despite these challenges.
• Allows you to establish a relationship between and identify the
relation between tables
• Help you to enforce identity and integrity in the relationship.
Various types of Keys
• Candidate Key
• Super Key
• Primary Key
• Foreign Key
• Composite Key
KEYS
CANDIDATE KEY
The minimal set of attributes that can uniquely identify a tuple is known as a candidate key.
Eg: STUD_NO in STUDENT relation.
The value of the Candidate Key is unique and non-null for every tuple.
The candidate key can be simple (having only one attribute) or composite as well.
Eg:{STUD_NO, COURSE_NO} is a composite candidate key for relation
STUDENT_COURSE.
SUPER KEY
• The set of attributes that can uniquely identify a tuple is known as Super Key.
• There can be more than one candidate key in relation out of which one can be
chosen as the primary key.
• Eg: STUD_NO, as well as STUD_PHONE both, are candidate keys for relation
STUDENT but STUD_NO can be chosen as the primary key (only one out of
many candidate keys).
Candidate Key = Super Key - Primary key
PRIMARY KEY - Rules
• The candidate key other than the primary key which is not chosen by the
database designer to uniquely identify the tuples is called an alternate key.
• Eg: STUD_NO, as well as STUD_PHONE both, are candidate keys for relation
STUDENT but STUD_PHONE will be an alternate key (only one out of many
candidate keys).
Keys
FOREIGN KEY
• It is a single attribute or collection of attributes in one table that refers to the
primary key of other table.
• The table containing the primary key is called the parent table and the table
containing the foreign key is called child table.
• Every employee works in a specific department in a company, employee and
department are two different entities. So we can't store the department's
information in the employee table. That's why we link these two tables through
the primary key of one table.
• We add the primary key of the DEPARTMENT table, Department_Id, as a
new attribute in the EMPLOYEE table.
• In the EMPLOYEE table, Department_Id is the foreign key, and both the tables
are related.
FOREIGN KEY - Example
Composite Key
(or) Concatenated Key
• Whenever a primary key consists of more than one attribute, it is known as a
composite key.
• Eg: In employee relations, we assume that an employee may be assigned
multiple roles, and an employee may work on multiple projects simultaneously.
So the primary key will be composed of all three attributes, namely Emp_ID,
Emp_role, and Proj_ID in combination. So these attributes act as a composite
key since the primary key comprises more than one attribute.