mySQL 可以根据另一列将特定列定义为 UNIQUE 吗?
假设我有一张表:
CREATE TABLE `ml_vendor_refs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ml_id` int(11) NOT NULL,
`ven_id` int(11) NOT NULL,
`designator` int(4) NOT NULL,
`telco` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
如你所见,“id”是primary,AI。但是,我希望能够根据“ml_id”和“telco”的父级添加几行不重复的“指示符”。 例如:
"id" "ml_id" "ven_id" "designator" "telco"
"1" "5144" "3" "1" "0"
"2" "5144" "7" "2" "0"
"3" "5144" "44" "3" "0"
"4" "5144" "49" "4" "0"
对于“ml_id”和布尔值“telco”的每个实例,我希望能够拥有唯一且唯一的“指示符”。有道理吗?
谢谢
Suppose I have a table:
CREATE TABLE `ml_vendor_refs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ml_id` int(11) NOT NULL,
`ven_id` int(11) NOT NULL,
`designator` int(4) NOT NULL,
`telco` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
As you can see, "id" is primary and AI. However, I want to be able to add several rows of 'designator' that my not duplicate based on the parent of 'ml_id' and 'telco'.
For example:
"id" "ml_id" "ven_id" "designator" "telco"
"1" "5144" "3" "1" "0"
"2" "5144" "7" "2" "0"
"3" "5144" "44" "3" "0"
"4" "5144" "49" "4" "0"
for every instance of "ml_id" and the boolean "telco", I want to be able to have unique and ONLY unique "designators". Make sense?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以拥有一个跨越多个列的
UNIQUE
索引。结果将是一种限制,即相同的值可以在一列中多次出现,但相同的值组合不能出现在指定的列中。You can have a
UNIQUE
index than spans multiple columns. The result would be a restriction such that the same value can appear in one column multiple times but the same combination of values cannot appear in the specified columns.如果您在多个列上添加唯一索引,则可以定义唯一配对。例如,您可以在
ml_id, designator
上添加一个索引,强制配对是唯一的。据我所知,
NULL
值不受唯一约束。You can have a unique pairing defined if you add a unique index on more than one column. For instance, you could add an index on
ml_id, designator
that forces the pairings to be unique.As far as I know,
NULL
values are not subject to unique constraints.