Postgres:如何做复合键?
我无法理解创建组合键时的语法错误。 可能是逻辑错误,因为我测试过很多品种。
如何在 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,
^
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,
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的复合
PRIMARY KEY
规范已经满足您的要求。 省略给您带来语法错误的行,并省略多余的 CONSTRAINT(已经隐含):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:您收到的错误位于第 3 行。即它不在
但更早:
正确的表定义就像 pilcrow 所示。
如果你想在 tag1、tag2、tag3 上添加唯一(这听起来很可疑),那么语法是:
或者,如果你想根据你的意愿命名约束:
The error you are getting is in line 3. i.e. it is not in
but earlier:
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: