Using PostgreSQL SERIAL To Create Auto Increment Column
Using PostgreSQL SERIAL To Create Auto Increment Column
increment Column
?
When creating a new table, the sequence is created through the SERIAL pseudo-type as follows:
1 CREATE TABLE table_name(
2 id SERIAL
3 );
By assigning the SERIAL pseudo-type to the id column, PostgreSQL will perform the following:
Creates a sequence object and set the next value generated by the sequence as the default
value for the column.
Adds the NOT NULL constraint to the column because a sequence always generates an
integer, which is a non-null value.
Assigns the owner of the sequence to the id column; as a result, the sequence object is
deleted when the id column or table is dropped
Behind the scenes, the following statement:
In this tutorial, you have learned how to use the serial data type to create an auto-increment column
for a database table.
Related Tutorials
PostgreSQL Data Types