MySQL 唯一复合约束不起作用
我创建了一个像这样的唯一索引:
ALTER TABLE items
ADD UNIQUE INDEX uni_item
(warehouse_id, width, height, depth, weight);
但我仍然可以添加重复的项目,为什么这不起作用?
注意:
表本身还有一些这里没有包含的列,而warehouse_id是外键,但这些东西应该不重要吧?
谢谢!
@ularis:这是成功添加重复项的示例:
item_id, warehouse_id, width, height, depth, weight, date
1 4 100 100 100 14 2011-08-07 07:01:26
item_id, warehouse_id, width, height, depth, weight, date
6 4 100 100 100 14 2011-08-07 07:01:32
编辑:
根据需要添加更多信息:
CREATE TABLE `items` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`warehouse_id` int(11) NOT NULL,
`width` decimal(3,2) NOT NULL,
`height` decimal(3,2) NOT NULL,
`depth` decimal(3,2) NOT NULL,
`weight` decimal(3,2) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `uni_item` (`warehouse_id`,`width`,`height`,`depth`,`weight`),
KEY `warehouse_id` (`warehouse_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`warehouse_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
INSERT INTO `main`.`items` (
`item_id` ,
`warehouse_id` ,
`width` ,
`height` ,
`depth` ,
`weight` ,
`date`
)
VALUES (
NULL , '4', 100, 100, 100, 14, CURRENT_TIMESTAMP
)
I've created a unique index like this:
ALTER TABLE items
ADD UNIQUE INDEX uni_item
(warehouse_id, width, height, depth, weight);
but I still can add duplicate items, why is this not working?
Note:
The table itself also has some columns that are not included here, and warehouse_id is a foreign key, but these things shouldn't matter, right?
Thanks!
@cularis: here is the example of successfully added duplicates:
item_id, warehouse_id, width, height, depth, weight, date
1 4 100 100 100 14 2011-08-07 07:01:26
item_id, warehouse_id, width, height, depth, weight, date
6 4 100 100 100 14 2011-08-07 07:01:32
EDIT:
Adding more info as required:
CREATE TABLE `items` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`warehouse_id` int(11) NOT NULL,
`width` decimal(3,2) NOT NULL,
`height` decimal(3,2) NOT NULL,
`depth` decimal(3,2) NOT NULL,
`weight` decimal(3,2) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `uni_item` (`warehouse_id`,`width`,`height`,`depth`,`weight`),
KEY `warehouse_id` (`warehouse_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`warehouse_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
INSERT INTO `main`.`items` (
`item_id` ,
`warehouse_id` ,
`width` ,
`height` ,
`depth` ,
`weight` ,
`date`
)
VALUES (
NULL , '4', 100, 100, 100, 14, CURRENT_TIMESTAMP
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 mysql 5.1.58 上无法重现
导致:
编辑:使用有问题中提供的表定义
Not reproducible on mysql 5.1.58
results in:
EDIT: use table definitions provided in question
非常感谢您的所有帮助。
我的安装似乎出了问题,我现在重新安装了整个服务器,并且约束工作得很好。
thanks very much for all the help.
Seems something was wrong with my installation, I've now reinstalled the whole server and the constraint works just fine.