如何正确使用UNIQUE索引? [英] how to use UNIQUE index correctly?
本文介绍了如何正确使用UNIQUE索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DB上有4个字段,我将它们设置为不能重复的条目.它们是:
i have 4 fields at DB.i set them become cant duplicate entry.They are:
1. Model Varchar(14) Unique
2. Serial varchar(8) Unique
3. Lot varchar(5) Unique
4. Line char(5) Unique
Model Serial Lot Line
First data remocon x0001 033a fa 01
如果输入的数据相同,则无法记录.
and if i have inputed same data it can't recorded.
remocon x0001 033a fa 01
但是如果我像这样输入,如何使该数据成功输入:
but how to make this data success to input if i type like:
remocon x0002 033a fa 01
我想要的结果是:
Model Serial Lot Line
remocon x0001 033a fa 01
remocon x0002 033a fa 01
推荐答案
您需要为所有字段(而不是每个字段)添加唯一约束,即
You need add unique constraint for all fields, not for each, i.e.
UNIQUE(型号,序列号,批次,行)
UNIQUE(Model, Serial, Lot, Line)
解决方案:
CREATE TABLE YourTable
(
Model Varchar(14) NOT NULL,
Serial varchar(8) NOT NULL,
Lot varchar(5) NOT NULL,
Line char(5) NOT NULL,
unique (model, serial, lot, line)
)
对于现有表:
alter table YourTableName drop index model;
alter table YourTableName drop index serial;
alter table YourTableName drop index lot;
alter table YourTableName drop index line;
alter table YourTableName add unique (model, serial, lot, line);
这篇关于如何正确使用UNIQUE索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文