1

I am working with PostgreSQL for the first time. I have this statement:

CREATE TABLE IF NOT EXISTS sequences (
  SEQU_NK   SERIAL PRIMARY KEY NOT NULL UNIQUE,
  NOM_SEQU varchar(30) NOT NULL,
  PROCHAIN bigint NOT NULL,
  PRIMARY KEY (SEQU_NK),
  UNIQUE KEY IDX_SEQU_NOM (NOM_SEQU)
)

When I run it I get:

ERROR:  syntax error at or near "KEY"
LINE 6:   UNIQUE KEY IDX_SEQU_NOM (NOM_SEQU)
                 ^
********** Error **********

ERROR: syntax error at or near "KEY"
1

2 Answers 2

1

Try like this:

CREATE TABLE IF NOT EXISTS sequences (
  SEQU_NK   SERIAL PRIMARY KEY NOT NULL UNIQUE,
  NOM_SEQU varchar(30) NOT NULL,
  PROCHAIN bigint NOT NULL,
  CONSTRAINT "IDX_SEQU_NOM" UNIQUE (NOM_SEQU)
) 

SQLFIDDLE DEMO

Sign up to request clarification or add additional context in comments.

8 Comments

Please explain a bit ?
@xavxav:- There are two problems in your query. 1) You have defined the Primary key twice which is not correct. (PRIMARY KEY (SEQU_NK) is not required) 2) The syntax to create the UNIQUE constraint was not correct. Hope this helps :)
Thanks Rahul..One more question: Can we write comments in PostGre like this : "PASSE varchar(40) COLLATE latin1_general_ci NOT NULL COMMENT 'md5',"
@xavxav: it's either Postgres or PostgreSQL but never "postgre". Regarding your question to specify comments: see the manual or this question
@xavxav: Please start a new question for your new question. Comments are not the place.
|
0
CREATE TABLE sequences (
  sequ_nk  serial PRIMARY KEY,
  nom_sequ varchar(30) NOT NULL UNIQUE,
  prochain bigint NOT NULL
);

It's UNIQUE , not UNIQUE KEY.
There can only be one PRIMARY KEY.
A PRIMARY KEY column is UNIQUE and NOT NULL automatically.
Read the manual.
Or, better yet, read the manual for your actual Postgres version 8.4 - which tells you that IF NOT EXISTS is not yet implemented in version 8.4 (wait for 9.1).
Or, better still, upgrade to a current version of Postgres. Version 8.4 has reached EOL last year.
Aside: Don't use quoted upper-case identifiers, or you have to keep quoting them, always.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.