0% found this document useful (0 votes)
49 views30 pages

LM 4 - Relational Database Keys

The document provides information about relational databases and keys. It defines relational databases and various keys like candidate key, primary key and foreign key. It also provides examples to explain these concepts.

Uploaded by

Bala Krish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views30 pages

LM 4 - Relational Database Keys

The document provides information about relational databases and keys. It defines relational databases and various keys like candidate key, primary key and foreign key. It also provides examples to explain these concepts.

Uploaded by

Bala Krish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

KGiSL Institute of Technology

(Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai)


Recognized by UGC, Accredited by NBA (IT)
365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.

Department of Computer Science and Engineering

Name of the Faculty : Ms. Aruna T N

Subject Name & Code : CS3492/ Database Management Systems

Branch & Department : Computer Science and Engineering

Year & Semester : II / IV

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Course Outcome

CO 1 Demonstrate fundamentals of Data models and Relational


databases - K3 LEVEL

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES

Purpose of Database System – Views of data – Data Models –


Database System Architecture – Introduction to relational
databases – Relational Model – Keys – Relational Algebra – SQL
fundamentals – Advanced SQL features – Embedded SQL–
Dynamic SQL

CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC

Introduction to Relational Databases


&
Relational Model

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)

• Relational database is a collection of tables having Unique Names.


Example
Table 1
Rollno Name MobileNo
001 AAA 11111111111
002 BBB 2222222222
003 CCC 3333333333

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

We can establish the relationship among the two tables


by defining the third table
Relational Model
⮚ Relational Model represents how data is stored in Relational
Databases.
⮚ A relational database stores data in the form of relations (tables).
⮚ Consider a relation STUDENT with attributes ROLL_NO, NAME,
ADDRESS, PHONE and AGE

“STUDENT”
Table
Table (or) Relation
• Table is a collection of data items arranged in rows and columns.
• It cannot have duplicate data or rows.

Rollno Name Marks MobileNo


001 AAA 88 11111111111
002 BBB 99 2222222222
003 CCC 77 3333333333
Terminologies
1)Attribute:
Attributes are the properties that define a relation.
e.g.; ROLL_NO, NAME
2)Tuple:
Each row in the relation is known as tuple.

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.

There can be more than one candidate key in a relation.


Eg: STUD_NO is the candidate key for relation STUDENT.

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.

Eg: STUD_NO, (STUD_NO, STUD_NAME), etc.


• Adding zero or more attributes to the candidate key generates the super key.
• A candidate key is a super key but every super key is not a candidate key
PRIMARY 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

• Primary key may have one or more attributes

• There is only one primary key in the relation

• The value of primary key attribute cannot be NULL

• The value of primary key attribute does not get changed.


ALTERNATE KEY

• 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.

You might also like