EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Identity Column
 

PostgreSQL Identity Column

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 18, 2023

PostgreSQL Identity Column

 

 

Definition of PostgreSQL Identity Column

In version 10 of PostgreSQL, it has introduced a new feature named GENERATED AS IDENTITY constraint. The GENERATED AS IDENTITY constraint allows the user to assign a unique value to the column automatically. The SERIAL column constraint and the GENERATED AS IDENTITY constraint are similar constraints provided by PostgreSQL. PostgreSQL allows users to have multiple identity columns in a single table. The GENERATED AS IDENTITY constraint uses the SEQUENCE object the same as the SERIAL constraint. We can change the characteristics of the existing Identity column or alter a table to have a column as an Identity column.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Consider the following syntax in order to illustrate the PostgreSQL Identity column:

column_name data_type
GENERATED { ALWAYS | BY DEFAULT }
AS IDENTITY
[ ( sequence_option ) ]

Explanation:

  • column_name: This defines the name of the column on which we want to apply the GENERATED AS IDENTITY constraint.
  • data_type: The data type can be any of the following:
  1. SMALLINT,
  2. INT,
  3. or BIGINT.

How does PostgreSQL Identity column works?

There are two types of instruction given to PostgreSQL when we use the GENERATED AS IDENTITY constraint, Let’s understand them as defined below.

Generated Always:

If we have GENERATED ALWAYS defined, which means PostgreSQL has to always create new value for the identity column. In case of the column with GENERATED ALWAYS constraint, if we try to insert or update a value for the same, then PostgreSQL throws an error or exception.

Generated By Default:

If we have GENERATED BY DEFAULT, defined means the PostgreSQL will create a new value for the PostgreSQL identity column. In the case of the column with GENERATED BY DEFAULT constraint, if we try to insert or update a value for the same, then PostgreSQL uses the same value and does not use any system-generated value. Also, PostgreSQL does not throw any error or exception.

Examples

Let us discuss examples of PostgreSQL Identity Column.

Example #1 – GENERATED ALWAYS AS IDENTITY

Consider the following example where we will create a new table by using the CREATE TABLE statement, which will store the details of the tuitions.

DROP TABLE tuitions;
CREATE TABLE tuitions (
tuition_id INT GENERATED ALWAYS AS IDENTITY,
tuition_name VARCHAR NOT NULL
);

Now we will insert a row into the tuitions table by using the INSERT INTO statement as follow:

INSERT INTO tuitions(tuition_name)
VALUES('Maths');

Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.

SELECT * FROM tuitions;

GENERATED ALWAYS AS IDENTITY

Now we will try to insert another record in the tuitions table by providing tuition_id and tuition_name, both as follows:

INSERT INTO tuitions(tuition_id, tuition_name)
VALUES(2, 'English');

As we have used GENERATED ALWAYS AS IDENTITY constraint the, PostgreSQL will throw an error or exception.

Illustrate the result of the above INSERT INTO statement by using the following snapshot.

PostgreSQL Identity Column 2

Example #2 – GENERATED BY DEFAULT AS IDENTITY

Consider the following example where we will create a new table by using the CREATE TABLE statement, which will store the details of the tuitions.

DROP TABLE tuitions;
CREATE TABLE tuitions (
tuition_id INT GENERATED BY DEFAULT AS IDENTITY,
tuition_name VARCHAR NOT NULL
);

Now we will insert a row into the tuitions table by using the INSERT INTO statement as follow:

INSERT INTO tuitions(tuition_name)
VALUES('Maths');

Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.

SELECT * FROM tuitions;

GENERATED BY DEFAULT AS IDENTITY

Now we will try to insert another record in the tuitions table by providing tuition_id and tuition_name, both as follows:

INSERT INTO tuitions(tuition_id, tuition_name)
VALUES(2, 'English');

As we have seen in case of the GENERATED ALWAYS AS IDENTITY constraint, the PostgreSQL was throwing an error or exception by for GENERATED BY DEFAULT AS IDENTITY constraint it will work as expected.

Illustrate the result of the above INSERT INTO statement by using the following SQL statement and snapshot.

SELECT * FROM tuitions;

PostgreSQL Identity Column 4

Example #3 – Add column Identity

For adding an Identity column to the existing table the, PostgreSQL provides the following syntax:

ALTER TABLE
table
ALTER COLUMN
column
ADD
{ ALWAYS | BY DEFAULT }
AS IDENTITY
{ ( sequence_option ) }

Consider the following example where we will create a new table by using the CREATE TABLE statement, which will store the details of the tuitions.

DROP TABLE tuitions;
CREATE TABLE tuitions (
tuition_id int NOT NULL,
tuition_name VARCHAR NOT NULL
);

Now we will describe the identity column status for table tuitions by using the following SQL statement and snapshot:

SELECT
column_name, is_identity, identity_generation
FROM
information_schema.columns
WHERE
TABLE_NAME = 'tuitions';

Add column Identity 1

We can change the tuition_id column to the Identity column by using the following syntax, before defining any column as an Identity column, we have to make it a NOT NULL column.

ALTER TABLE
tuitions
ALTER COLUMN
tuition_id
ADD
GENERATED ALWAYS AS IDENTITY;

we will describe the identity column status for table tuitions by using the following SQL statement and snapshot:

SELECT
column_name, is_identity, identity_generation
FROM
information_schema.columns
WHERE
TABLE_NAME = 'tuitions';

Add column Identity 2

Example #4 – Change the characteristics of the existing Identity column

For changing the characteristics of an Identity column of the existing table, the PostgreSQL provides the following syntax:

ALTER TABLE
table
ALTER COLUMN
Column
{
SET
GENERATED { ALWAYS| BY DEFAULT }
}

In order to understand this topic, consider the table created in the previous section.

We can change the Identity column tuition_id column’s constraint by using the following syntax

ALTER TABLE
tuitions
ALTER COLUMN
tuition_id
SET GENERATED BY DEFAULT;

we will describe the identity column status for table tuitions by using the following SQL statement and snapshot:

SELECT
column_name, is_identity, identity_generation
FROM
information_schema.columns
WHERE
TABLE_NAME = 'tuitions';

characteristics of the existing Identity column

Example #5 – Drop the column Identity

For deleting an Identity column of the existing table the, PostgreSQL provides the following syntax:

ALTER TABLE
table
ALTER COLUMN
column
DROP
IDENTITY [ IF EXISTS ]

In order to understand this topic, consider the table created in the previous section.

We can drop the identity of the tuition_id column by using the following syntax:

ALTER TABLE
tuitions
ALTER COLUMN
tuition_id
DROP
IDENTITY IF EXISTS;

we will describe the identity column status for table tuitions by using the following SQL statement and snapshot:

SELECT
column_name, is_identity, identity_generation
FROM
information_schema.columns
WHERE
TABLE_NAME = 'tuitions';

Drop the column

Conclusion

We hope from the above article, you have understood how to use the PostgreSQL Identity column and how the PostgreSQL Identity column works. Also, we have added several examples of the PostgreSQL Identity column to understand it in detail.

Recommended Articles

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

  1. PostgreSQL Roles
  2. MIN() PostgreSQL
  3. PostgreSQL Current Date
  4. SET PostgreSQL

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW