PostgreSQL: autoincrement primary key field

Trabla: PostgreSQL: autoincrement primary key field

Solving:

use type BIGSERIAL  + constraint PRIMARY KEY

CREATE TABLE tablename ( colname SERIAL  PRIMARY KEY);

is equal to

CREATE SEQUENCE tablename_colname_seq; 
CREATE TABLE tablename ( 
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')  PRIMARY KEY
); 
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

Example:

CREATE TABLE tbl_cars
(
     id BIGSERIAL PRIMARY KEY,
     name  varchar(200)

)

 Official Docs: http://www.postgresql.org/docs/9.2/static/datatype-numeric.html



No comments:

Post a Comment