UNIQUE - way to have unique rows in table?

我们两清 提交于 2019-12-10 10:55:57

问题


I have problem with unique rows in db table, now it is posible to do that:

id  | Name  | LastName  | City
-------------------------------------
1   | John  | Moore     | London
2   | John  | Moore     | London

when i use UNIQUE attribute in all columns i have errors inserting second Moore even it is different Name :/

how use UNIQUE (or maybe INDEX?) to do something like that in my table in db:

id  | Name  | LastName  | City
-------------------------------------
1   | John  | Moore     | London
2   | Jake  | Moore     | London
3   | John  | Keen      | London
4   | John  | Moore     | London //but good error when inserting the same row

Sorry if question is easy, but i am beginner at sql, and have problems with find some good example with using a UNIQUE like a want:/ or maybe I must just before inserting a new row selecting a table from db and check if it exist?


回答1:


Remove the unique index on the individual column and make it on both columns together, like this:

CREATE UNIQUE INDEX ixFullName ON yourTable (LastName, Name);


来源:https://stackoverflow.com/questions/2152176/unique-way-to-have-unique-rows-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!