postgresql - Why SERIAL is not working on this simple table in Postgres? -


i'm using postgres 9.1. , auto_increment (serial) not working. i've found 'serial': https://www.postgresql.org/docs/current/static/datatype-numeric.html#datatype-serial

create type family as(     id int,     name varchar(35),     img_address varchar(150));  create table families of family(     id serial primary key not null,     name not null     );  error:  syntax error @ or near "serial" line 7:  id serial primary key not null,                          ^   ********** error **********  error: syntax error @ or near "serial" sql state: 42601 

when create table using syntax:

create table xxx of yyyy 

you can add default values , constraints, not alter or specify type of columns.

the type serial in effect combination of data type, not null constraint , default value specification. equivalent to:

integer not null default nextval('tablename_colname_seq') 

see: documentation serial

so, instead have use:

create sequence families_id_seq;  create table families of family(     id options not null default nextval('families_id_seq'),     name options not null );  alter sequence families_id_seq owned families.id; 

you have create sequence families_id_seq manually well, shown above.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -