mysql structure double unique
I am trying to make a mysql table and have got stuck with making it.
2 columns required:
aid
bid
The trick is aid is not unique and bid is not unique, but the combination of aid-bid IS unique.
EG:
a b ok?
0 1 y
0 2 y
0 7 y
1 1 y
1 7 y
8 3 y
0 7开发者_StackOverflow中文版 N - since 0 7 is already in table...
How can I make this table in sql so it works?
create table my_table (
a int,
b int,
unique key (a,b) );
You need to create a unique index for those columns:
CREATE UNIQUE INDEX id_index ON mytable (aid,bid);
Just add a unique constraint to the table
ALTER TABLE t ADD UNIQUE KEY (aid, bid);
Then change your code to trap the exception when you try to insert a duplicate key.
精彩评论