EDUCBA

EDUCBA

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

PostgreSQL Primary Key

By Sohel SayyadSohel Sayyad

PostgreSQL Primary Key

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

Syntax

Below are the syntax used in the Primary key:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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,
column_name2 data_type,
.
.
.
);

2. We can create a primary key constraint containing two or more columns as follows:

CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
.
.
.
PRIMARY KEY (column_name1, column_name2)
);

Syntax #2 – UPDATE PRIMARY KEY

We can add a primary key constraint for an existing table, which is rare. 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 the primary key constraint name explicitly then PostgreSQL assigns a default name to the primary key constraint. It assigns table_name_pkey as the default name for the primary key constraint. If we have a table named CITIES, then 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 the same:

Code:

CONSTRAINT constraint_name PRIMARY KEY(column_name1, column_name2,...);

Examples

Let’s create a table named COUNTRIES using the CREATE TABLE statement in order to understand the examples.

Example #1 – Single Column PRIMARY KEY

Code:

CREATE table COUNTRIES
(
country_id serial PRIMARY KEY,
country_name VARCHAR (256) NOT null,
last_updated DATE NULL
);

The country_id is the COUNTRIES table’s primary key, which used to identify the country in the COUNTRIES table uniquely.

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.

Code:

CREATE table  CITIES
(
city_id serial,
country_id INT,
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.

Code:

CREATE TABLE invoices (
invoice_id INT,
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.

Code:

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.

Code:

CREATE TABLE invoices (invoice_data VARCHAR(255));
  • NOW insert some data in the invoices table using the INSERT statement.

Code:

INSERT INTO invoices (invoice_data)
VALUES
('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.

Code:

SELECT   * FROM   Invoices;

Output:

PostgreSQL Primary - 1

  • 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:

Code:

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.

Code:

SELECT   invoice_id,invoice_data FROM   invoices;

Output:

PostgreSQL Primary - 2

  • 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:

Code:

ALTER TABLE invoices
DROP CONSTRAINT invoices_pkey;

Advantages of using Primary Key in PostgreSQL

  1. The PostgreSQL PRIMARY KEY helps us to search records quickly as the index is getting used based on the PRIMARY KEY.
  2. By using PostgreSQL PRIMARY KEY, we can uniquely identify the table records for an update or delete operation.
  3. The PostgreSQL PRIMARY KEY is used to sort the data based on it.
  4. By using PostgreSQL PRIMARY KEY, we can avoid duplicate row insertion in the table.

Conclusion

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.

Recommended Articles

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

  1. PostgreSQL Database
  2. INTERSECT PostgreSQL
  3. PostgreSQL Constraints
  4. PostgreSQL Cross Join
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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.

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

*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