I cannot understand the syntax error in creating a composite key. It may be a logic error, because I have tested many varieties.
How do you create composite keys in Postgres?
CREATE TABLE tags
(
(question_id, tag_id) NOT NULL,
question_id INTEGER NOT NULL,
tag_id SERIAL NOT NULL,
tag1 VARCHAR(20),
tag2 VARCHAR(20),
tag3 VARCHAR(20),
PRIMARY KEY(question_id, tag_id),
CONSTRAINT no_duplicate_tag UNIQUE (question_id, tag_id)
);
ERROR: syntax error at or near "("
LINE 3: (question_id, tag_id) NOT NULL,
^
The error you are getting is in line 3. i.e. it is not in
but earlier:
I have absolutely no idea why you did put it there - what is the purpose? what is the logic?
Anyway. Correct table definition is like pilcrow showed.
And if you want to add unique on tag1, tag2, tag3 (which sounds very suspicious), then the syntax is:
or, if you want to have the constraint named according to your wish:
Your compound
PRIMARY KEY
specification already does what you want. Omit the line that's giving you a syntax error, and omit the redundantCONSTRAINT
(already implied), too: