For my table I’ve defined a unique index on 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)
So, how can this data exist??
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)
Edit: here’s the output of 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)
because of the
NULLon end_date,technically NULL <> EMPTY, or any value, is just a placeholder where value is missing
so, change it to NOT NULL should fix
PS: when you doing alter
this will fail, because mysql will attempt to convert the NULL to
0000-00-00 00:00:00,in order to fix this, you can either assign some random value to it first,
or just simply remove one of the duplicate