ytu*_*ytu 5 postgresql create-table
我正在按照CREATE TABLE 中的示例进行操作:
CREATE TABLE distributors (
did integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar(40) NOT NULL CHECK (name <> '')
);
Run Code Online (Sandbox Code Playgroud)
但是,它给了我ERROR: syntax error at or near "GENERATED"。为什么会这样,我该如何解决?
\! psql -V返回psql (PostgreSQL) 10.5 (Ubuntu 10.5-1.pgdg14.04+1)SELECT version();在 x86_64-pc-linux-gnu (Ubuntu 9.4.19-1.pgdg14.04+1) 上返回PostgreSQL 9.4.19,由 gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4 编译,64 位编辑:
感谢 @muistooshort,我检查了 9.4 文档。所以我执行:
CREATE TABLE distributors (
did integer PRIMARY KEY DEFAULT nextval('serial'),
name varchar(40) NOT NULL CHECK (name <> '')
);
Run Code Online (Sandbox Code Playgroud)
尽管如此,它现在给了我错误:关系“串行”不存在......
SQL 标准IDENTITY是在 PostgreSQL 10 中添加的,但您的服务器(完成所有实际工作)是 9.4。在 10 之前,您必须使用serial或bigserial键入:
CREATE TABLE distributors (
did serial not null primary key,
name varchar(40) NOT NULL CHECK (name <> '')
);
Run Code Online (Sandbox Code Playgroud)
该serial类型将创建一个序列来提供值,将序列附加到表,并连接一个默认值did以从序列中获取值。
| 归档时间: |
|
| 查看次数: |
4824 次 |
| 最近记录: |