【发布时间】:2013-04-05 18:23:56
【问题描述】:
当尝试使用WHERE NOT EXISTS 子句来防止在age 列中添加具有重复值的行时,我收到错误syntax error at or near "WHERE"。
为什么会抛出语法错误?我正在使用 Postgresql 9.1。
SQL
INSERT INTO live.users ("website", "age")
values ('abc', '123')
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);
错误
ERROR: syntax error at or near "WHERE"
LINE 6: WHERE NOT EXISTS (SELECT age FROM live.users W...
【问题讨论】:
-
如果要防止列中出现重复值,最好在该列中添加
unique constraint。 (ALTER TABLE live.users ADD CONSTRAINT age_unique UNIQUE(age) )
标签: sql postgresql postgresql-9.1