2011-09-02 59 views
1

對於我的表,我已經定義了一個唯一索引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

我們可以得到'SHOW CREATE TABLE sg_activity_property'的輸出嗎? – Bobby

+0

添加輸出到帖子! – Rijk

回答

1

因爲NULL上END_DATE的,
技術上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
爲了解決這個問題,你可以一些隨機值首先分配給它,
或者只是簡單地刪除其中一個副本

+0

不幸的是,我在這裏需要'NULL',因爲我用它來查找'當前'條目('WHERE end_date IS NULL') – Rijk

+0

您可以隨時將其轉換爲'WHERE end_date = 0' – ajreal

+0

這樣的表現會同樣出色嗎? – Rijk

0

您在end_date中有NULL值。

NULL值是未定義的值,因此兩個 NULL值不相同。

+0

兩個NULL值是不一樣的??這完全讓我感到困惑 – Rijk

2

這是預期的行爲。檢查MySQL文檔: http://dev.mysql.com/doc/refman/5.0/en/create-table.html

唯一索引創建,使得該指數 所有的值必須是不同的約束。如果嘗試使用與現有行匹配的鍵值 添加新行,則會發生錯誤。除了BDB存儲引擎之外,此限制不適用於 空值。對於其他引擎, UNIQUE索引允許針對可包含空值的列的多個NULL值。

+0

謝謝你這個有用的評論我會永遠不會期望NULL被'跳過' MySQL索引! – Rijk