MySQL 唯一复合约束不起作用

发布于 2024-11-28 05:02:56 字数 1441 浏览 2 评论 0原文

我创建了一个像这样的唯一索引:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-12-05 05:02:56

在 mysql 5.1.58 上无法重现

CREATE TABLE `items` (
 `item_id` int(11) NOT NULL,
 `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,
 PRIMARY KEY (`item_id`),
 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;

CREATE TABLE `warehouses` (
 `warehouse_id` int(11) NOT NULL,
 `data` text,
 PRIMARY KEY (`warehouse_id`)
) ENGINE=InnoDB;

insert into warehouses values (4, 'test');
Query OK, 1 row affected (0.01 sec)

导致:

mysql> insert into items values (1,4,100,100,100,14,'2011-08-07 07:01:26');
Query OK, 1 row affected (0.01 sec)
mysql> insert into items values (6,4,100,100,100,14,'2011-08-07 07:01:32');
ERROR 1062 (23000): Duplicate entry '4-9.99-9.99-9.99-9.99' for key 'uni_item'

编辑:使用有问题中提供的表定义

Not reproducible on mysql 5.1.58

CREATE TABLE `items` (
 `item_id` int(11) NOT NULL,
 `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,
 PRIMARY KEY (`item_id`),
 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;

CREATE TABLE `warehouses` (
 `warehouse_id` int(11) NOT NULL,
 `data` text,
 PRIMARY KEY (`warehouse_id`)
) ENGINE=InnoDB;

insert into warehouses values (4, 'test');
Query OK, 1 row affected (0.01 sec)

results in:

mysql> insert into items values (1,4,100,100,100,14,'2011-08-07 07:01:26');
Query OK, 1 row affected (0.01 sec)
mysql> insert into items values (6,4,100,100,100,14,'2011-08-07 07:01:32');
ERROR 1062 (23000): Duplicate entry '4-9.99-9.99-9.99-9.99' for key 'uni_item'

EDIT: use table definitions provided in question

卷耳 2024-12-05 05:02:56

非常感谢您的所有帮助。

我的安装似乎出了问题,我现在重新安装了整个服务器,并且约束工作得很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文