简体   繁体   中英

unique value based on another column value?

Issue is I need to have a unique value in a column based on value from another column.

for ex : I have item_ID and product column

For every item_ID there should only be unique values in the product column.

item_ID    Product
1               1
1               2
1               4
2               1

but if I insert 1 in item_ID and 1 in product now, it should throw an error. I have no idea how to implement this. Any help appreciated.

您需要的是两个列的唯一索引

alter table yourtable add unique index(item_ID, Product);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM