當試圖使用WHERE NOT EXISTS
子句來防止在列age
中添加具有重複值的行時,我得到錯誤syntax error at or near "WHERE"
。PostgreSQL中不存在的地方給出語法錯誤
它爲什麼會拋出語法錯誤?我正在使用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...
如果要防止列中出現重複值,最好爲該列添加一個「唯一約束」。 (ALTER TABLE live.users ADD CONSTRAINT age_unique UNIQUE(age)) – 2013-04-05 18:40:23