我创建了一个独特的索引,如下所示:

ALTER TABLE items
ADD UNIQUE INDEX uni_item
(warehouse_id, width, height, depth, weight);

但我仍然可以添加重复的项目,为什么这不起作用?
注:
表本身也有一些列不包括在这里,warehouse\u id是一个外键,但是这些东西应该不重要,对吧?
谢谢!
@库拉里斯:下面是成功添加重复项的示例:
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
)

最佳答案

在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'

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

09-25 19:47