Introduction to PostgreSQL Primary Key
The row of the table can be uniquely identified using a column or group of columns termed as a primary key. We can specify the primary keys using the primary key constraints. The PRIMARY KEY constraint is the combination of a UNIQUE constraint and a NOT NULL constraint. The table can consist of only one PRIMARY KEY, Adding a primary key on each table is a best practice in the database management system.
Below are the syntax used in Primary key:
Syntax #1 – Define the primary key
1. Add the primary key while creating the table. We can add the primary key constraint to a table while creating the table itself using the CREATE TABLE statement.
CREATE TABLE table_name (
column_name1 data_type PRIMARY KEY,
2. We can create a primary key constraint containing two or more columns as follows:
CREATE TABLE table_name (
PRIMARY KEY (column_name1, column_name2)
Syntax #2 – UPDATE PRIMARY KEY
We can add a primary key constraint for an existing table also which is a very rare situation. We can achieve the same using an ALTER TABLE statement as follows:
ALTER TABLE table ADD PRIMARY KEY (column1, column);
Syntax #3 – Remove the primary key
With the help of the ALTER TABLE statement, we can remove an existing primary key constraint of the table as follows:
ALTER TABLE table_name DROP CONSTRAINT primary_key_constraint;
How does Primary Key work in PostgreSQL?
The PostgreSQL creates a unique B-TREE index on PRIMARY KEY columns if we have added PRIMARY KEY on the table using a column or a group of columns of a table. If we don’t define explicitly the primary key constraint name then the PostgreSQL assigns a default name to the primary key constraint. The PostgreSQL assigns table_name_pkey as the default name for the primary key constraint. Like, If we are having a table named CITIES then the PostgreSQL creates the primary key constraint with the name cities_pkey for the same. We can use the CONSTRAINT clause as follows to assign the name for same:
CONSTRAINT constraint_name PRIMARY KEY(column_name1, column_name2,...);
Examples to Implement Primary Key in PostgreSQL
Let’s create a table named COUNTRIES using the CREATE TABLE statement in order to understand the examples.
Example #1 – Single Column PRIMARY KEY
CREATE table COUNTRIES
country_id serial PRIMARY KEY,
country_name VARCHAR (256) NOT null,
last_updated DATE NULL
The country_id is the primary key of the COUNTRIES table, which used uniquely identify the country in the COUNTRIES table.
Example #2 – Multiple Columns PRIMARY KEY
The following statement will create a table named ‘CITIES’ whose primary key is a combination of city_id and country_id.
CREATE table CITIES
city_name VARCHAR (256) NOT NULL,
last_updated DATE null,
PRIMARY KEY (CITY_id, country_id)
Example #3 – Adding PRIMARY KEY
Example to ALTER TABLE for adding PRIMARY KEY after table creation. The following statement creates a table named invoices which will not have any PRIMARY KEY.
CREATE TABLE invoices (
invoice_data VARCHAR (256) NOT NULL
- Now with the help of the following statement, we can add a primary key constraint to the invoices table.
ALTER TABLE invoices
ADD PRIMARY KEY (invoice_id);
Example #4 – Auto-incremented PRIMARY KEY
Example to ALTER TABLE for adding auto-incremented PRIMARY KEY after table creation. Now with the help of the following statement, we can add an auto-incremented primary key constraint to the invoices table.
CREATE TABLE invoices (invoice_data VARCHAR(255));
- NOW insert some data in the invoices table using the INSERT statement.
INSERT INTO invoices (invoice_data)
('Purchase of Mobile'),
('Purchase of Laptop'),
('Purchase of PC'),
('Purchase of Tablet');
- Illustrate the result of an INSERT statement with the help of the following SQL statement and snapshot.
SELECT * FROM Invoices;
- Here we will add an auto-incremented by one primary key named invoice_id into the invoices table with the help of the following statement:
ALTER TABLE invoices ADD COLUMN invoice_id SERIAL PRIMARY KEY;
- Illustrate the result of an ALTER statement with the help of the following SQL statement and snapshot.
SELECT invoice_id,invoice_data FROM invoices;
- Now the invoices table has the primary key invoice_id.
Example #5 – Remove a PRIMARY KEY of the table
In order to remove the primary key constraint of the invoices table we have to use the following statement:
ALTER TABLE invoices
DROP CONSTRAINT invoices_pkey;
Advantages of using Primary Key in PostgreSQL
- The PostgreSQL PRIMARY KEY helps us to search records quickly as index is getting used based on the PRIMARY KEY.
- By using PostgreSQL PRIMARY KEY we can uniquely identify the table records for an update or delete operation.
- The PostgreSQL PRIMARY KEY is used to sort the data based on it.
- By using PostgreSQL PRIMARY KEY we can avoid duplicate row insertion in the table.
We hope from the above article you have understood how to add, update and remove primary key constraints using CREATE TABLE and ALTER TABLE statements. Also with the help of examples and syntax explained in detail.
This is a guide to PostgreSQL Primary Key. Here we discuss an introduction to PostgreSQL Primary Key along with appropriate syntax and respective examples. You can also go through our other related articles to learn more –