postgresql syntax error when creating a table

alpal picture alpal · Feb 13, 2014 · Viewed 33.4k times · Source

Hey everyone I need some help with creating tables. I have the script below and it creates a few tables. When i try to run the script it give me this error:

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

Can anyone tell me what is going on?

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)
);

Answer

markgiaconia picture markgiaconia · Feb 13, 2014

it had a lot of problems this worked for me. In postgres, names that are not all lowercase need to be double quoted. also some of your table names are reserved words, money can't be > and int, and there was a comma out of place.

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) );