You are on page 1of 6

Database Management System

Lec4 : Database Design Using the E-R Model part 2

4.1 What are the Keys in DBMS

A key in DBMS is an attribute or a set of attributes that help to uniquely identify a


tuple (or row) in a relation (or table). Keys are also used to establish relationships
between the different tables and columns of a relational database.

4.2Why are the Keys Required

A key is used in the definitions of various kinds of integrity constraints. A table in


a database represents a collection of records or events for a particular relation.
Now there can be thousands and thousands of such records, some of which may be
duplicated.

There should be a way to identify each record separately and uniquely, i.e. no
duplicates. Keys allow us to be free from this hassle.

Let us take a real-life example of the database of each student studying in an


engineering college.

What attribute of the student do you think will uniquely identify each of them?
You could refer to a student by using their name, department, year and section. Or,
you can mention only the university roll number of the student, and you can get all
the other details from that.

1
Database Management System

Lec4 : Database Design Using the E-R Model part 2

A key could either be a grouping of more than one attribute (or columns) or just a
single attribute. The main motive of this is to give each record a unique identity.

4.3 Types of Keys in DBMS

There are broadly seven types of keys in DBMS:

1. Primary Key
2. Candidate Key
3. Super Key
4. Foreign Key
5. Composite Key
6. Alternate Key
7. Unique Key

1. Primary Key

A primary key is a column of a table or a set of columns that helps to identify


every record present in that table uniquely. There can be only one primary Key in a
table. Also, the primary Key cannot have the same values repeating for any row.
Every value of the primary key has to be different with no repetitions.

The PRIMARY KEY (PK) constraint put on a column or set of columns will not
allow them to have any null values or any duplicates. One table can have only one
primary key constraint. Any value in the primary key cannot be changed by any
foreign keys (explained below) which refer to it.

2
Database Management System

Lec4 : Database Design Using the E-R Model part 2

2. Super Key

Super Key is the set of all the keys which help to identify rows in a table uniquely.
This means that all those columns of a table than capable of identifying the other
columns of that table uniquely will all be considered super keys.

Super Key is the superset of a candidate key (explained below). The Primary Key
of a table is picked from the super key set to be made the table’s identity attribute.

3. Candidate Key

Candidate keys are those attributes that uniquely identify rows of a table. The
Primary Key of a table is selected from one of the candidate keys. So, candidate
keys have the same properties as the primary keys explained above. There can be
more than one candidate keys in a table.

3
Database Management System

Lec4 : Database Design Using the E-R Model part 2

4. Alternate Key

As stated above, a table can have multiple choices for a primary key; however, it
can choose only one. So, all the keys which did not become the primary Key are
called alternate keys.

5. Foreign Key

Foreign Key is used to establish relationships between two tables. A foreign key
will require each value in a column or set of columns to match the Primary Key of
the referential table. Foreign keys help to maintain data and referential integrity.

6. Composite Key

4
Database Management System

Lec4 : Database Design Using the E-R Model part 2

A composite key, in the context of relational databases, is a combination of two or


more columns in a table that can be used to uniquely identify each row in the table.
Uniqueness is only guaranteed when the columns are combined; when taken
individually the columns do not guarantee uniqueness.

7. Unique Key

Unique Key is a column or set of columns that uniquely identify each record in a
table. All values will have to be unique in this Key. A unique Key differs from a
primary key because it can have only one null value, whereas a primary Key
cannot have any null values.

Example of how null can be used

Use the Salary table (Salary_tbl) in Figure 8.6 to follow an example of how null
can be used.

5
Database Management System

Lec4 : Database Design Using the E-R Model part 2

Figure 8.7. Salary table for null example, by A. Watt.

To begin, find all employees (emp#) in Sales (under the jobName column) whose
salary plus commission are greater than 30,000.

• SELECT emp# FROM Salary_tbl


• WHERE jobName = Sales AND
• (commission + salary) > 30,000 –> E10 and E12

This result does not include E13 because of the null value in the commission
column. To ensure that the row with the null value is included, we need to look at
the individual fields. By adding commission and salary for employee E13, the
result will be a null value. The solution is shown below.

• SELECT emp# FROM Salary_tbl


• WHERE jobName = Sales AND
• (commission > 30000 OR
• salary > 30000 OR
• (commission + salary) > 30,000 –>E10 and E12 and E13

You might also like