You are on page 1of 42

Keys in DBMS

Keys play an important role in the


relational database.

It is used to uniquely identify any record


or row of data from the table.
What are Keys in
DBMS?
It is also used to establish and identify
relationships between tables.

A Key can be a single attribute or a


group of attributes, where the
combination may act as a key.
employee ID is a primary key because it
uniquely identifies an employee record. In
this table, no other employee can have the
same employee ID.
Types of DBMS
Keys
Super Key is defined as a set of attributes
within a table that can uniquely identify
each record within a table.

Super Key is a superset of Candidate key.


Super Key

A super key is not restricted to have any


specific number of attributes.
• Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )

• Given below are the examples of super keys since each set can uniquely
identify each student in the Student table-

( roll , name , sex , age , address , class , section )


( class , section , roll )
(class , section , roll , sex )
( name , address )
Candidate keys are defined as the
minimal set of fields which can uniquely
identify each record in a table.

It is subset of a super key

Candidate
key It is called a minimal super key or
irreducible super key.

Every table must have at least a single


candidate key but at the same time there
can be more than one candidate key.
• Properties of Candidate key:

• It must contain unique values

• Candidate key may have multiple attributes

• Must not contain null values

• Uniquely identify each record in a table

• Removing any attribute from the candidate key fails in


identifying each tuple uniquely.

• It is possible to have multiple candidate keys in a relation.

• Those attributes which appears in some candidate key are


called as prime attributes.
Example:
In the given STUDENT Table:
Stud ID, Roll No, and email are candidate keys which help us to uniquely identify the student record in the table.

3 candidate keys:
1) StudID
2) Rollno
3) Email
PRIMARY KEY is a column or group of columns in a
table that uniquely identify every row in that table.

The Primary Key can't be a duplicate meaning the


same value can't appear more than once in the
table.
Primary A table cannot have more than one primary key.

Key
It can be a single attribute of the table or
combination of attribute.

Primary key can also be called as a key which is


most suitably chosen from set of candidate keys.
• Consider the data of professors of a college.

• Here, “College ID No.” can be termed as a primary key because it can


uniquely identify all the details of the professors easily. Two professor
can have same name that is why name is not a primary key.

EMPLOYEE

EmpId EmpName EmpAddress PassportNum LicNum

•The key which is most suitable from those lists become a primary key.

•In the EMPLOYEE table, ID can be primary key since it is unique for each employee.

•In the EMPLOYEE table, we can even select Lic_Num and PassportNum as primary
key since they are also unique.
Rules for defining Primary key:

The value of primary key can never be NULL.

The value of primary key must always be unique.


The value of primary key must be assigned when inserting a
record.
A relation is allowed to have only one primary key.
The value in a primary key column can never be modified or
updated if any foreign key refers to that primary key.
Difference between candidate key and Primary
key
Unique Key

• Similar to a primary key, Unique key


constraints also identifies an
individual tuple uniquely in a
relation.

• A table can have more than one


unique key.

• Unique key constraints can accept


only one NULL value for a column.
Secondary or Alternative
key

• Candidate keys can be more


than one. Out of which only
one is chosen as a primary
key.
• The remaining keys which are
not selected are called as
alternate key.
Foreign key

• A foreign key is an attribute in one


table that acts as a primary key in
another table.
• The foreign key is useful for
establishing the relationship
between two tables in a database.
Create the primary key on the field department_id of
the DEPARTMENT table.

Now in the student table, Department_id is the foreign


key, and both the tables are linked.
Foreign key Foreign key can take Foreign key may
only those values which have a name other
references the are present in the
primary key of the primary key of the than that of a
table. referenced relation. primary key.

There is no
Foreign key can In fact, foreign key
restriction on a
take the NULL is not unique most
foreign key to be
value. of the time.
unique.

Referenced relation Referencing


may also be called relation may also
as the master table be called as the
or primary table. foreign table.
Surrogate Key

A surrogate key is a special key which has no meaning or


purpose other than to uniquely identify each record.

Surrogate keys are often a combination of letters and


numbers.

A new column is created to be the primary key.


Surrogate Key
Composite Key

• Key that consists of two or more attributes that uniquely identify any
record in a table is called Composite key.
• But the attributes which together form the Composite key are not a
key independently or individually.
Why we need a Key?

• Here are some reasons :


• Keys help you to identify any row of data in a table. In a real-world
application, a table could contain thousands of records. Moreover, the
records could be duplicated. Keys 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.
Schemas
• Database Schema
• The description of a database.
• Includes descriptions of the database structure and the
constraints that should hold on the database.
• Specified during database design
• Not expected to change frequently
• A displayed schema is called a schema diagram
• Schema is also called intension
• Schema diagram is a diagrammatic display that represents only some
aspects of a schema.
• Example: Each object in the schema-such as STUDENT or COURSE-is a
schema construct.
Database Instance
• The actual data stored in a
database at a particular
moment in time. Also called
database state (or
occurrence).
• Instance is also called
extension
• The database instance/state
changes every time the
database is updated.
• When database is defined, it is in empty
state
• Then at any point in time, the database
state has a current state
• Initial database state: the database state
when it is initially loaded into the system
• Valid state: A state that satisfies the
structure and constraints of the database
Database Users
1) Actors on
Scene
Database Users
2) Workers
behind the scene
Persons whose job
involves use of a
Actors on large database are:
Scene • Database administrator
• Database designers
• End Users
• System Analyst
Database administrator
Database Administrator is a person or group of persons responsible for overall control of
database system. All the activities of database system are controlled by DBA.

•Deciding the information contents of


database system
•Deciding hardware device to be used
•Deciding the users and data to be used by
Some of the main activities are as follows: users
•Deciding the back-up and recovery
method
•Monitoring performance and responding
to changing requirements
Database Designers
• Database designers task is undertaken before the
database is actually implemented.
• Database designer responsible for:
• Identifying the data to be stored
• Choosing appropriate structures to represent
and store this data
Those whose jobs require access to the database

Naive or
parametric end • canned queries and updates
users

End users Casual end users


• occasional, special-purpose
access

Sophisticated end • deep knowledge of database


users design and DBMS facilities

Standalone users • users of personal databases


System analysts
System analysts & • Determine requirements of end
Application users

programmers Application programmers

• Implement complex
specifications (business logic) as
programs
• DBMS
Persons whose designers and
job involves implementers
Workers behind design,
development,
• Tool
Developers
the scene operation and
maintenance • Operator and
of DBMS
software are: maintenance
personal
DBMS system Operators and
designers and Tool developers maintenance
implementers personnel

Design and Design and Responsible for


implement the DBMS implement tools running and
modules and maintenance of
interfaces as a hardware and
software package software
environment for
database system

You might also like