![]() |
VOOZH | about |
PostgreSQL, a robust open-source relational database management system, offers a variety of tools for managing and organizing data. One such feature is the ability to add an identity to an existing column, which is particularly useful in situations when each row requires a unique identifier. In this article, we'll look at how to add identity to an existing column in PostgreSQL, including a step-by-step instruction and practical examples.
PostgreSQL excels in adding identity to existing columns, providing unique identifiers for efficient data management. Using the ALTER TABLE statement, users can customize identity columns, elevating data organization and uniqueness in this powerful relational database management system.
To add an identity to an existing column, we can use the ALTER TABLE statement with the ADD GENERATED clause. This procedure allows you to define a column as an identity column, and PostgreSQL will produce unique values for that column depending on the conditions you specify.
To add an identity to an existing column, use the code below:
ALTER TABLE table_name ALTER COLUMN column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( identity_options ) ]
Let's understand the main elements of the syntax:
Now, let's take a look at some examples to do the task.
Initially, consider a table named 'EMPLOYEE' structured as follows:
-- create CREATE TABLE EMPLOYEE ( emp_id INTEGER PRIMARY KEY, name TEXT NOT NULL, dept TEXT NOT NULL ); \d EMPLOYEE;
After describing the table columns, there is no default identity set for the columns.
Table "public.employee"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
emp_id | integer | | not null |
name | text | | not null |
dept | text | | not null |
Indexes:
"employee_pkey" PRIMARY KEY, btree (emp_id)In the initial setup, a table named EMPLOYEE was created with columns 'emp_id' (integer), 'name' (text), and 'dept' (text). No default identity was set for the columns.
Now, let's use the above syntax to set the 'IDENTITY' Column for the employee table.
-- create table employee CREATE TABLE EMPLOYEE ( emp_id INTEGER PRIMARY KEY, name TEXT NOT NULL, dept TEXT NOT NULL ); ALTER TABLE EMPLOYEE ALTER COLUMN emp_id ADD GENERATED BY DEFAULT AS IDENTITY; \d EMPLOYEE;
Now let's describe the above table:
Table "public.employee"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+----------------------------------
emp_id | integer | | not null | generated by default as identity
name | text | | not null |
dept | text | | not null |
Indexes:
"employee_pkey" PRIMARY KEY, btree (emp_id)Hence, 'emp_id' is set as the IDENTITY column for the table.
-- create table employee CREATE TABLE EMPLOYEE ( emp_id INTEGER PRIMARY KEY, name TEXT NOT NULL, dept TEXT NOT NULL ); ALTER TABLE EMPLOYEE ALTER COLUMN emp_id ADD GENERATED BY DEFAULT AS IDENTITY (START WITH 1000 INCREMENT BY 5); INSERT into EMPLOYEE (name, dept) values ('Ben', 'HR'); SELECT * FROM EMPLOYEE;
Now, let's check the creation of auto generated value:
emp_id | name | dept
--------+------+------
1000 | Ben | HRThe output displays the "EMPLOYEE" table with an identity column "emp_id" starting at 1000 and incrementing by 5. One record is inserted, showing 'emp_id 1000', name 'Ben,' and department 'HR.'
In conclusion, PostgreSQL's capability to add an identity to an existing column provides valuable functionality for managing unique identifiers. Utilizing the ALTER TABLE statement with the ADD GENERATED clause allows for the efficient customization of identity columns to meet specific requirements. This feature enhances data organization and uniqueness, contributing to PostgreSQL's effectiveness as a robust relational database management system.