EDUCBA

EDUCBA

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

PostgreSQL Identity Column

By Sohel SayyadSohel Sayyad

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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