我使用的是Postgres 9.1。并且auto_increment(串行)不起作用。我刚发现这是关于' 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 at or near "SERIAL"
LINE 7: id SERIAL primary key NOT NULL,
^
********** Error **********
ERROR: syntax error at or near "SERIAL"
SQL state: 42601
答案 0 :(得分:4)
使用以下语法创建表时:
CREATE TABLE xxx OF yyyy
您可以添加默认值和约束,但不能更改或指定列的类型。
SERIAL类型实际上是数据类型,NOT NULL约束和默认值规范的组合。它相当于:
integer NOT NULL DEFAULT nextval('tablename_colname_seq')
所以,你必须使用:
CREATE SEQUENCE families_id_seq;
CREATE TABLE FAMILIES of FAMILY(
id WITH OPTIONS NOT NULL DEFAULT nextval('families_id_seq'),
name WITH OPTIONS NOT NULL
);
ALTER SEQUENCE families_id_seq OWNED BY FAMILIES.id;
您还必须手动创建序列families_id_seq
,如上所示。