EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial Primary Key in SQL

Primary Key in SQL

Aanchal Sharma
Article byAanchal Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 28, 2023

Primary Key in SQL

Introduction to Primary Key in SQL

In SQL, a Primary Key is a special relational database table field or a combination of fields that uniquely identifies a record in the table of multiple records. The main feature of the primary key is it holds a unique value for each row of table data in the database. In the DBMS table, there should be only one primary key, and none of those fields with a primary field can contain NULL A table with only one Primary Key Constraint with single or multiple columns. But when we use more than one field as a Primary Key, it is known as a Composite Key. Suppose we have defined a column as a Primary Key; then there should not be any two records with the same value as that of the column.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Why do we Need a Primary Key in SQL?

Suppose we need a different feature for any identification that makes it unique from others. Everyone has a unique specialty that makes us judge and verify them based on the quality one has in them. Likewise, for distinguishing a table from others and identifying them, we need a special key constraint that can be used to validate the records of that table and maintain uniqueness, consistency, and integrity.

Also, for joining any two tables in the relational database systems, a Primary key and another table’s Primary key are known as a Foreign Key, which plays a vital role. So, we can use this Primary Key while creating a table or altering it in the database. If a Primary Key is already present in the table, the server or system will not allow inserting a row with the same key. It helps to improve the security of database records also.

Syntax:

In SQL, we usually use either CREATE TABLE query or ALTER TABLE SQL statement to create a PRIMARY KEY Constraint for a table.

We can use the following syntax for this:

SQL Primary Key on Create Table statement

The following query in SQL creates Primary Key on the PersonID column when we create the Persons_Data table.

MySQL:

CREATE TABLE Persons_Data ( PersonID int NOT NULL, Name varchar(255) NOT NULL, Address varchar(255), Phone int, PRIMARY KEY (PersonID) );

MS Access |SQL Server | Oracle:

CREATE TABLE Persons_Data (PersonID int NOT NULL PRIMARY KEY, Name Varchar(255) NOT NULL, Address varchar(255), Phone int );

For naming a PRIMARY KEY Constraint and for creating a PRIMARY KEY Constraint on multiple fields, use the succeeding SQL.

Syntax:

MS Access | MySQL | SQL Server | Oracle:

CREATE TABLE Persons_Data(ID int NOT NULL,Name varchar(255) NOT NULL, Address varchar(255), Phone int, CONSTRAINT PersonID PRIMARY KEY (ID,Name) );
  • SQL PRIMARY KEY on ALTER TABLE

The query below in SQL declares a Primary Key constraint on the PersonID field when the Persons_Data table already exists:

Oracle | MS Access | MySQL | SQL Server:

ALTER TABLE Persons_Data ADD PRIMARY KEY (PersonID);

Here, we define Primary Key Constraint on multiple columns:

MS Access| MySQL | SQL Server | Oracle :

ALTER TABLE Persons_Data ADD CONSTRAINT PK_PersonID PRIMARY KEY (PersonID, Name);

It becomes easier for us to use SQL ALTER query to add a Primary Key to a field in the table when it has been stated as NOT NULL already when the table was created.

  • DROP a PRIMARY KEY Constraint

We use the following SQL statement for this:

MySQL:

ALTER TABLE Persons_Data DROP PRIMARY KEY;

MS Access | SQL Server | Oracle:

ALTER TABLE Persons_Data DROP CONSTRAINT PK_PersonID;

Working of Primary Key in SQL

A primary key has an auto-defined unique constraint and remembers we can include only one primary key in one table. Generally, we provide ID as the Primary key for a unique table with a NOT NULL value. So, when we create a table, we define a certain field as Primary Key with a NOT NULL constraint so that it becomes a unique identifier column in the table.

Example:

Code:

Create Table Persons_Data (PersonID int NOT NULL, Name Varchar(255) NOT NULL, Address Varchar(255) NOT NULL, Phone int NOT NULL, PRIMARY KEY (PersonID, Name));

Here we have declared the Primary key on PersonID and Name columns on the Persons_Data Table. Note that a “Constraint allows us to define the limit data type when we create or alter the table queries in SQL.”

How to Use a Primary Key in SQL with Row, Column, Table, etc.?

We use a primary key in SQL database queries as follows:

Suppose we create a table named Customers with columns “CustomerID, CustomerName, Address, and City.”

Code:

CREATE TABLE Customers (CustomerID int NOT NULL PRIMARY KEY, CustomerName varchar (255) NOT NULL, Address varchar (255) NOT NULL, City varchar (255) );

We have the screenshot as output below, where we have also inserted some data values:

Output:

Primary Key in SQL output 1

We can see in the above table that the column CustomerID has unique values and is NOT NULL, but on the other side, we can find that the values, like in the City column, contain duplicate data also.

For example, we can pass an SQL statement to use the CustomerID column with SELECT and WHERE statements.

Code:

SELECT * From Customers WHERE CustomerID=6;

Output:

Primary Key in SQL output 2

Here the output is the result of the above SQL statement, where we have the row data whose CustomerID is equal to 6 in the Customers table. Likewise, there are many uses of the primary key in the SQL database; if you do not enter the primary key value when you do not define it Auto, you may receive an error from the server.

Conclusion

The Primary key constraint is helpful in the process of Normalization, where it helps to maintain redundancy and dependency in a database table by establishing fields and tables of a database in an organized way. In SQL, it helps to relate many tables in the database, determining relationships in the SQL queries like JOINS, Denormalization process, Triggering, Logical or Aggregate functions, and other statements too where we need to connect two tables or more with a unique Primary key This helps in easy data retrieval and manages the process to reduce redundancy and RDBMS becomes more convenient.

Recommended Articles

We hope that this EDUCBA information on “Primary Key in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Composite Key in SQL
  2. SQL Keys
  3. LIKE Query in SQL
  4. IF ELSE Statement in SQL
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more