创建表时出现postgresql语法错误

时间:2014-02-13 22:21:06

标签: sql postgresql create-table

嘿大家我需要帮助创建表格。我有下面的脚本,它创建了几个表。当我尝试运行脚本时,它会给我这个错误:

psql:script.sql:10: ERROR:  syntax error at or near "Group"
LINE 6: CREATE TABLE Group(

有谁能告诉我发生了什么事?

CREATE TABLE Group(
    name        varchar(40) PRIMARY KEY    NOT NULL
);

CREATE TABLE Artist(
    name        varchar(30) PRIMARY KEY    NOT NULL,
        birthplace  varchar(20)                NOT NULL,
        age     int                        NOT NULL   CHECK (age > 0),
        style       varchar(20)                NOT NULL
);

CREATE TABLE Artwork(
    title      varchar(40) PRIMARY KEY     NOT NULL,
        artist     varchar(30)                 NOT NULL   references Artist(name),
        group_name varchar(40)                 NOT NULL   references Group(name),
        year       int                         NOT NULL   CHECK (year > 0),
        type       varchar(30)                 NOT NULL,
        price      money                       NOT NULL,
);

CREATE TABLE Customer(
    cust_id      int PRIMARY KEY   NOT NULL,
    name         varchar(40)       NOT NULL,
        address      varcahr(60)       NOT NULL,
        amount       money             NOT NULL    CHECK(amount > 0),
        like_artist  varchar(30)       NOT NULL    references Artist(name),
        like_group   varchar(40)       NOT NULL    references Group(name)
);

1 个答案:

答案 0 :(得分:12)

它有很多问题 这对我有用。在postgres中,不是全部小写的名称需要加双引号。还有一些你的表名是保留字,金钱不能>和int,并且有一个逗号不合适。

CREATE TABLE "group"( name varchar(40) PRIMARY KEY NOT NULL );

CREATE TABLE artist( name varchar(30) PRIMARY KEY NOT NULL, birthplace varchar(20) NOT NULL, age int NOT NULL CHECK (age > 0), style varchar(20) NOT NULL );

CREATE TABLE artwork( title varchar(40) PRIMARY KEY NOT NULL, artist varchar(30) NOT NULL references artist(name), 
group_name varchar(40) NOT NULL references "group"(name), year int NOT NULL CHECK (year > 0), type varchar(30) NOT NULL, price money NOT NULL );

CREATE TABLE customer( cust_id int PRIMARY KEY NOT NULL, name varchar(40) NOT NULL, address varchar(60) NOT NULL, 
amount money NOT NULL CHECK(amount > cast(0.0 as money)), like_artist varchar(30) NOT NULL references artist(name), like_group varchar(40) NOT NULL references "group"(name) );