Use sequence value as a primary key : Primary Key « Constraints « PostgreSQL






Use sequence value as a primary key

postgres=#
postgres=# CREATE SEQUENCE myseq MINVALUE 0;
postgres=# -- Create auto-incrementing DEFAULT and PRIMARY KEY constraint, is:
postgres=#
postgres=# CREATE TABLE shipments
postgres-#          (id integer DEFAULT nextval('"myseq"'::text) PRIMARY KEY,
postgres(#           customer_id integer,
postgres(#           isbn text,
postgres(#           ship_date timestamp);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "shipments_pkey" for table "shipments"
CREATE TABLE
postgres=#
postgres=# drop sequence myseq;
DROP SEQUENCE
postgres=# drop table shipments;
DROP TABLE
postgres=#
postgres=#

           
       








Related examples in the same category

1.Implicit index is created when creating a table for a primary key
2.Primary keys constrains more than one column
3.Indicate one column as the primary key column
4.PRIMARY KEY will create implicit index
5.Primary key with check option
6.Add PRIMARY KEY in table creation