Help Center/ GaussDB/ Developer Guide(Distributed_8.x)/ FAQs/ How do I create an auto-increment column?
Updated on 2024-06-03 GMT+08:00

How do I create an auto-increment column?

Answer: GaussDB supports the creation of auto-increment columns. You can specify the SERIAL data type when creating a table.

Example:
gaussdb=# CREATE TABLE table_name(id serial, name varchar(20));

You can also use the following method:

--Create a sequence.
gaussdb=# CREATE SEQUENCE tbl_person_id_seq;
-- Create the tbl_persion table. The value of the id column is automatically increased based on the sequence specified by tbl_person_id_seq.
gaussdb=# CREATE TABLE tbl_persion(
    id int NOT NULL DEFAULT nextval('tbl_person_id_seq'::regclass),
    name varchar(20));