×

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table.

  • A PRIMARY KEY field contains values on each row.
  • PRIMARY KEY field cannot contain an empty string.
  • PRIMARY KEY data is never changed over time.
  • A table can consist of only one PRIMARY KEY, which may consist of single or multiple fields in the table. When more than one column is used as the table's PRIMARY KEY, they are known as the Composite key in the table.
  • There will be no duplicated records for a PRIMARY KEY.
  • If a field is set as a PRIMARY KEY in the table, that field cannot contain the values of the same field more than once in the table.

For example, we have a Student table in the database. The table contains 4 fields Student_Id, Student_Name, Phone_Number, and Email. Student_Id and Email is the PRIMARY KEY in the Student table. There is only one PRIMARY KEY but can have single or multiple fields in the table, as we told above. One student's data is already present in the table, but the same student adds his data again with the same email ID. As the student enters details, a pop-up will display a message “Email has already existed”. It is shown because Email has already existed in the table, and the email field is the PRIMARY KEY, and the PRIMARY KEY cannot contain duplicate values.

Create PRIMARY KEY on SQL TABLE

The following SQL query creates a PRIMARY KEY on the Employee_Id field when the ‘Employee’ table is created.

CREATE TABLE Employee( Employee_Id int PRIMARY KEY, Employee_Name varchar(40) NOT NULL, Salary int NOT NULL, Department varchar(40), City varchar(40) );        

The above query has defined PRIMARY KEY to the Employee_Id field. We can mention the key name after the field name and data type. The above query is the one way we can create Employee_Id as a PRIMARY KEY.

To check whether our PRIMARY KEY is successfully defined or not, we can use the DESC command followed by the Employee table name.

DESC Employee;

FieldsTypeNullKeyDefaultExtra
Employee_IdInt(11)NOPRINULL 
Employee_NameVarchar(40)NO NULL 
SalaryInt(11)NO NULL 
DepartmentVarchar(40)YES NULL 
CityVarchar(40)YES NULL 
SQL Primary Key

The Key field PRI is mentioned in front of the Employee_Id row, which means Employee_Id is successfully defined as the PRIMARY KEY.

Advantages of using PRIMARY KEY in the table:

  1. Fast Access to the data from the table.
  2. Duplicates values are not allowed in the PRIMARY KEY fields.

Another way to create a PRIMARY KEY

CREATE TABLE Employee( Employee_Id int NOT NULL, Employee_Name varchar(40) NOT NULL, Salary int NOT NULL, Department varchar(40), City varchar(40), PRIMARY KEY(Employee_ID) );

In the same query as above, we have to mention NOT NULL in front of the field name, which is created as the PRIMARY KEY, and at the end, we have to write the PRIMARY KEY keyword and Field name in parenthesis just like the above query.

Create a PRIMARY KEY constraint on the fields when the table already exists

Use the following query:

ALTER TABLE Employee ADD PRIMARY KEY(Employee_Id);

Using the ALTER TABLE query to define PRIMARY KEY, the PRIMARY KEY field has already been declared with NOT NULL constraint.

SQL PRIMARY KEY on Multiple Columns

The following SQL query creates a PRIMARY KEY on more than one column when the 'Student' Table is created.

CREATE TABLE Student( Student_Id int NOT NULL, Student_Name varchar(40), Department varchar(40), Phone_Number int(10), Email varchar(100) NOT NULL, PRIMARY KEY(Student_ID, Email));

Only one PRIMARY KEY is made in the example on Student_Id and Email.

To check whether our PRIMARY KEY is successfully defined or not, we can use the DESC command followed by the Student table name.

DESC Student;
FieldsTypeNullKeyDefaultExtra
Student_IdInt(11)NOPRINULL 
Student_NameVarchar(40)YES NULL 
DepartmentVarchar(40)YES NULL 
Phone_NumberInt(10)YES NULL 
EmailVarchar(100)NOPRINULL 

As we can see in front of the Student_Id and Email row, Key fields PRI is mentioned, which means Student_Id and Email are successfully defined as PRIMARY KEY.

SQL Primary Key

We will now execute SHOW CREATE TABLE followed by Student table name, which will describe the student table.

SHOW CREATE TABLE Student;
SQL Primary Key

In the above output, we can see that only one PRIMARY KEY is created, and in parenthesis, the fields name is mentioned, which is created as a PRIMARY KEY while creating the table.

Suppose we want to remove the PRIMARY KEY constraint from the student table. We will use the following query to drop the PRIMARY KEY constraint:

ALTER TABLE Student DROP PRIMARY KEY;

To check whether our PRIMARY KEY is successfully removed or not, we will use the DESC command followed by the Student table name.

DESC Student;
FieldsTypeNullKeyDefaultExtra
Student_IdInt(11)NO NULL 
Student_NameVarchar(40)YES NULL 
DepartmentVarchar(40)YES NULL 
Phone_NumberInt(10)YES NULL 
EmailVarchar(100)NO NULL 
SQL Primary Key

Related Topics

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.