Sql 防止重复
Sql prevent duplicates
我有一个 MySql table 名为 "service":
*Table Service*
PK serv_ID Int(2)
Address Varchar(20)
Date Date
比我有一个 table 名字 "materials":
*Table Materials*
PK Mat_ID Int(2)
Stock_Qty Int(2)
Unit enum(m,u)
现在,对于每项服务,我花费了一些 material 并且我制作了 table:
Service_Material
FK serv_ID
Fk Mat_ID
Qty int(2)
如何防止每项服务不重复相同material?
使用唯一约束或索引:
create unique index unq_service_material_serv_mat on service_material(serv_id, mat_id);
您也可以将此添加为约束(而不是):
alter table service_material
add constraint unq_service_material_serv_mat
unique(serv_id, mat_id);
我有一个 MySql table 名为 "service":
*Table Service*
PK serv_ID Int(2)
Address Varchar(20)
Date Date
比我有一个 table 名字 "materials":
*Table Materials*
PK Mat_ID Int(2)
Stock_Qty Int(2)
Unit enum(m,u)
现在,对于每项服务,我花费了一些 material 并且我制作了 table:
Service_Material
FK serv_ID
Fk Mat_ID
Qty int(2)
如何防止每项服务不重复相同material?
使用唯一约束或索引:
create unique index unq_service_material_serv_mat on service_material(serv_id, mat_id);
您也可以将此添加为约束(而不是):
alter table service_material
add constraint unq_service_material_serv_mat
unique(serv_id, mat_id);