PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the SERIAL
keyword allows you to create columns that auto-increment. It's commonly used for primary key columns. When you define a column as SERIAL
, PostgreSQL automatically takes care of creating a sequence object and setting up the default value for the column to fetch values from that sequence.
Here's how you can use SERIAL
:
CREATE TABLE table_name ( column_name SERIAL PRIMARY KEY, ... -- other columns );
For instance, if you're creating a table to store users, it might look like this:
CREATE TABLE users ( user_id SERIAL PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL );
In this example, every time you insert a new user without specifying a value for user_id
, PostgreSQL will automatically generate a new value.
When you define a column as SERIAL
, a few things happen behind the scenes:
users_user_id_seq
.SERIAL
column is set to fetch the next value from this sequence: nextval('users_user_id_seq'::regclass)
.If you anticipate needing a larger range of numbers than provided by the SERIAL
type (which is essentially an INTEGER
), you can use BIGSERIAL
, which is akin to a BIGINT
in terms of storage and range.
CREATE TABLE large_data ( id BIGSERIAL PRIMARY KEY, data TEXT );
Remember, even though the SERIAL
column will auto-increment, you can still explicitly set a value if needed, as long as it doesn't conflict with an existing value or a value the sequence might provide in the future.
If you're using PostgreSQL 10 or newer, you might consider using identity columns (GENERATED AS IDENTITY
) as a more standardized way to achieve auto-increment behavior. They provide similar functionality but with additional capabilities and SQL standard compliance.
Creating an auto-increment column in PostgreSQL using SERIAL:
CREATE TABLE your_table ( id SERIAL PRIMARY KEY, other_column VARCHAR(255) );
Defining a primary key with auto-increment in PostgreSQL:
CREATE TABLE your_table ( id SERIAL PRIMARY KEY, other_column VARCHAR(255) );
Inserting data into a table with SERIAL column in PostgreSQL:
INSERT INTO your_table (other_column) VALUES ('some_value');
Viewing the current value of a SERIAL column in PostgreSQL:
SELECT currval('your_table_id_seq');
Resetting the SERIAL sequence in PostgreSQL:
SELECT setval('your_table_id_seq', 1);
Adding SERIAL column to an existing table in PostgreSQL:
ALTER TABLE your_existing_table ADD COLUMN new_serial_column SERIAL PRIMARY KEY;
Handling concurrency with SERIAL columns in PostgreSQL:
-- Automatically handled by the SERIAL column
Customizing the start value and increment of a SERIAL column:
CREATE TABLE your_table ( id SERIAL START WITH 100 INCREMENT BY 5 PRIMARY KEY, other_column VARCHAR(255) );