Primary Key in DBMS (original) (raw)

Last Updated : 15 Jan, 2025

In DBMS there are different types of keys available that are used for various purposes, for which the most common key is known as a primary key. A primary key is a unique identifier assigned to each record within a database table.

Since the primary key is used to identify the tuples of a relation none of its attribute values can be null. Since a relation may have more than one candidate key (such as EMP_ Id, PAN no in EMPLOYEE table). So one of these candidate keys should be chosen as the primary key.

Rules For Defining a Primary Key

Syntax For Creating and Deleting Primary Key

Let's see what syntax is used for creating a table.

**Syntax

For Creation

ALTER TABLE table_name

ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);

For Deletion

ALTER TABLE table_name

DROP CONSTRAINT constraint_name;

Consider an example of the table EMPLOYEE Relation. Since for each employee, there exists a unique EMP_ID so it acts as a primary key. Now consider the same EMPLOYEE Relation table with EMP_ID as the Primary Key.

Understanding Primary Key

For Example, a table consists of columns EMP_ID, Name, PAN_NO, Salary, Dept. Below is the syntax for creating the Primary Key on the EMP_ID attribute of the EMPLOYEE table:

CREATE TABLE EMPLOYEE (

Employee_Id int NOT NULL PRIMARY KEY,

Name varchar (200) NOT NULL,

PAN_NO int NOT NULL

Salary int NOT NULL

Dept varchar (200) NOT NULL

} ;

Read more about SQL Primary Key Constraint.

The table is created as shown below.

EMP_ID Name PAN_NO Salary Dept
E1 Ram Null 23000 D1
E2 Sita PSJ1234 Null D2
E3 Sham DJF2324 12000 D3
Null Sapna WCD232 13000 D4

Here, we find that Employee E1 has not been yet allotted a PAN number i.e. it does not exist for him so it has been allotted a value null and will be allotted a value later on. Employee E2's Salary has not been fixed so it is unknown. Therefore we have allotted it a Null Value. But it will not cause a problem since it is not a primary key But if we allot a null value to the primary key EMP_ID then it may cause a problem as per the condition a key to be a primary key must not have a null value.

Now suppose that every employee has been allotted a PAN _NO.

EMP_ID Name PAN_NO Salary
E1 Ram Null 23000
E2 Sita PSJ1234 15000
E3 Sham DJF2324 12000
E4 Sapna WCD232 13000

Here the attributes EMP_ID, PAN_NO, and Name (where no names repeat) can act as candidate keys for the EMPLOYEE Relation.

The Name is a time-variant attribute as when the database grows two employee may have the same names. So it may act as a possible candidate key when databases are very small. Since we know that the primary key is formed from one of the candidate keys both EMP_ID and PAN_NO can act as a primary key, but one at a time. We assume EMP_ID as the primary key in most of the cases.

A Primary Key can be formed from the combination of more than one attribute.

When it comes to primary keys, there are several related terminologies you may come across as follows:

Advantages of Primary Key

Conclusion

In conclusion, in a database management system (DBMS), the primary key is the unique identifier for every record in the table, which guarantees data integrity and facilitates efficient data retrieval by uniquely identifying each record.