Introduction on 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 which has a primary field can contain NULL
A table can have only one Primary Key Constraint that can have single or multiple columns. But when we use more than one field as a Primary Key then it is known as a Composite Key.
Suppose if 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 for any identification we need a different feature that makes it unique from others. Everyone has a unique specialty that makes us judge and verify them on the basis of that quality one has in them.
Likewise, for distinguishing a table from others and for 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 then the server or system will not allow inserting a row with the same key. It helps to improve security for database records also.
In SQL, normally we can 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 Persons_Data table:
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
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 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 at the time, the table was created before.
- DROP a PRIMARY KEY Constraint
We use the following SQL statement for this:
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 auto defined unique constraint on it and remembers we can include only one primary key in one table. Generally, we provide ID as the Primary key for a table that will be unique and has NOT NULL value. So, when we create a table we define a certain field as Primary Key with NOT NULL constraint so that it becomes a unique identifier column in the table.
For Example, the statement explains the above matter:
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 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 as “CustomerID, CustomerName, Address, and City”.
SQL Statement for this is:
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:
We can see in the above table that the column CustomerID has unique values and NOT NULL but on the other side, we can find that the values like in City column contain duplicate data also.
For example, we can pass an SQL statement to use the CustomerID column with SELECT and WHERE statements:
SELECT * From Customers WHERE CustomerID=6;
Here the output is the result of the SQL statement above where we have got 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 then you may receive an error from the server.
- The Primary key constraint is useful 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.
- I hope you might have known what a Primary key is and its uses in the article.
This is a guide to Primary Key in SQL. Here we discuss how to use a primary key in SQL and why do we need it along with the working and syntax. You may also have a look at the following articles to learn more –