Updated May 18, 2023
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:
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:
- SMALLINT,
- INT,
- 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;
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.
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;
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;
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';
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';
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';
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';
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.