Updated June 28, 2023
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.
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:
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:
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.