问题描述
我对数据库表中的唯一行有问题,现在可以这样做:
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 :/
如何使用UNIQUE(或INDEX?)在db中的表格中执行类似操作:
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
对不起,如果问题很容易,在sql,并有问题找到一些很好的例子使用UNIQUE像一个想:/
或者也许我必须在插入新行之前从数据库选择一个表,并检查是否存在?
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?
推荐答案
删除单个列上的唯一索引,并将它们放在两个列上,如下所示:
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);
这篇关于UNIQUE - 在表中有唯一的行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!