×

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 Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

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

4 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

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

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes 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.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

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.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

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.