对于我的表格,我在activity_id
上定义了一个唯一索引 - actor_id
- end_date
;
mysql> show keys from sg_activity_property;
+----------------------+------------+-------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------------------+------------+-------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+
| sg_activity_property | 0 | PRIMARY | 1 | activity_property_id | A | 506 | NULL | NULL | | BTREE | |
| sg_activity_property | 0 | activity_id | 1 | activity_id | A | NULL | NULL | NULL | | BTREE | |
| sg_activity_property | 0 | activity_id | 2 | actor_id | A | NULL | NULL | NULL | | BTREE | |
| sg_activity_property | 0 | activity_id | 3 | end_date | A | NULL | NULL | NULL | YES | BTREE | |
+----------------------+------------+-------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+
4 rows in set (0.00 sec)
那么,这个数据怎么存在?
mysql> SELECT activity_property_id, activity_id, actor_id, start_date, end_date FROM `sg_activity_property` WHERE `activity_id` =250;
+----------------------+-------------+----------+---------------------+----------+
| activity_property_id | activity_id | actor_id | start_date | end_date |
+----------------------+-------------+----------+---------------------+----------+
| 509 | 250 | 8 | 2011-09-02 11:10:50 | NULL |
| 510 | 250 | 8 | 2011-09-02 11:10:50 | NULL |
+----------------------+-------------+----------+---------------------+----------+
2 rows in set (0.00 sec)
修改:这是SHOW CREATE TABLE sg_activity_property
的输出:
mysql> SHOW CREATE TABLE sg_activity_property;
+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sg_activity_property | CREATE TABLE `sg_activity_property` (
`activity_property_id` int(10) unsigned NOT NULL auto_increment,
`activity_id` int(10) unsigned NOT NULL,
`actor_id` int(10) unsigned NOT NULL,
`importance` enum('very low','low','normal','high','very high') NOT NULL default 'normal',
`urgency` enum('!','!!') default NULL,
`completed` tinyint(1) NOT NULL,
`start_date` datetime NOT NULL,
`end_date` datetime default NULL,
`review_frequency` int(11) NOT NULL default '1',
`review_frequency_unit` enum('day','week','month','quarter','year') NOT NULL default 'week',
PRIMARY KEY (`activity_property_id`),
UNIQUE KEY `activity_id` (`activity_id`,`actor_id`,`end_date`)
) ENGINE=MyISAM AUTO_INCREMENT=511 DEFAULT CHARSET=latin1 |
+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.19 sec)
答案 0 :(得分:2)
这是预期的行为。检查MySQL文档: http://dev.mysql.com/doc/refman/5.0/en/create-table.html
UNIQUE索引创建一个约束,以便索引中的所有值 必须是截然不同的如果您尝试使用a添加新行,则会发生错误 与现有行匹配的键值。此约束不适用 除了BDB存储引擎之外的NULL值。对于其他引擎,a UNIQUE索引允许可以包含的列的多个NULL值 NULL。
答案 1 :(得分:1)
因为end_date上有NULL
,所以
技术上是NULL<> EMPTY或任何值只是缺少值的占位符
所以,将其更改为NOT NULL应修复
PS:当你改变
时alter table sg_activity_property
modify column end_date datetime not null
default '0000-00-00 00:00:00';
这会失败,因为mysql会尝试将NULL转换为0000-00-00 00:00:00
,
为了解决这个问题,你可以先为它分配一些随机值,
或者只是删除其中一个副本
答案 2 :(得分:0)
您在end_date中有NULL值。
NULL值是未定义的值,因此是两个 NULL值不相同。