Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql syntax error when creating a table

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)
);
like image 289
alpal Avatar asked Feb 13 '14 22:02

alpal


People also ask

How do I create a table in PostgreSQL?

CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) ); CREATE TABLE is a keyword, telling the database system to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.

How do I create a table and column in PostgreSQL?

Creating a table using the PostgreSQL CREATE TABLE statement. The PostgreSQL CREATE TABLE statement basic syntax is as follows: CREATE TABLE [IF NOT EXISTS] table_name ( column1 datatype(length) column_contraint, column2 datatype(length) column_contraint, column3 datatype(length) column_contraint, table_constraints );

What is the maximum table size in PostgreSQL?

PostgreSQL normally stores its table data in chunks of 8KB. The number of these blocks is limited to a 32-bit signed integer (just over two billion), giving a maximum table size of 16TB.

What Is syntax in PostgreSQL?

The simplest form of the SELECT statement syntax is: SELECT expressions FROM tables WHERE conditions; The expressions are all the columns and fields you want in the result. The tables syntax is the table or tables from which you want to extract the results.


1 Answers

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) );
like image 149
Mark Giaconia Avatar answered Oct 14 '22 08:10

Mark Giaconia