alter ignore table add unique key

Mark

IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key, The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.

 

root@test 01:57:29>alter ignore table tmp_abc add unique key uk_b(b);

Query OK, 5 rows affected (0.65 sec)

Records: 5  Duplicates: 3  Warnings: 0

 

----BUT---- WHY --

alter ignore table table_bbb add unique key uk_tcm_sellerid(seller_id,service_date);

Error 1062: Duplicate entry '10688998-2011-04-11 00:00:00' for key 'uk_tcm_sellerid'

[2011-04-14 03:00:00] SQL執行失敗!

--- which variables?

 

 

##########

mysql刪除表中重複記錄的sql,只能搞個臨時表了

                   

create table tmp_xd_del_id as

select id from tt_stats a

 WHERE a.id != (SELECT MAX(id)

                     FROM tt_stats b

                    WHERE a.seller_id = b.seller_id

                      AND a.service_date = b.service_date);  

                      

delete from tt_stats where id in(select id from tmp_xd_del_id);




不希望數據表中有重複記錄的時候我們可以給表添加一個聯合唯一索引
例如,user表中有user_id,user_name兩個字段,如果不希望有兩條一摸一樣的的user_id和user_name,我們可以給user表添加兩個字段的聯合唯一索引:
alter table user add unique index(user_id,user_name);
這樣當向表中添加相同記錄的時候,會返回1062的添加失敗信息。
但是有一種情況是表中已經有n個重複的記錄,這時候我們纔想起來要添加唯一索引,再執行上面的操作時,數據庫會告訴你已經有重複的記錄了,建立索引失敗,這時候,我們可以用下面的操作:
alter ignore table user add unique index(user_id,user_name);
它會刪除重複的記錄(別怕,會保留一條),然後建立唯一索引,高效而且人性化。

尤其是綠色的那條sql,在建立索引的同時刪除了重複數據,相當有用。
查看索引  show index from 數據庫表名
alter table 數據庫add index 索引名稱(數據庫字段名稱)
PRIMARY KEY(主鍵索引)
ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 
UNIQUE(唯一索引)
ALTER TABLE `table_name` ADD UNIQUE (`column`) 

INDEX(普通索引)
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )


FULLTEXT(全文索引)
ALTER TABLE `table_name` ADD FULLTEXT ( `column` )

多列索引
ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )
1.普通索引。
這是最基本的索引,它沒有任何限制。它有以下幾種創建方式:
(1)創建索引:CREATE INDEX indexName ON tableName(tableColumns(length));如果是CHAR,VARCHAR類型,length可以小於字段實際長度;如果是 BLOB 和 TEXT 類型,必須指定length,下同。
(2)修改表結構:ALTER tableName ADD INDEX [indexName] ON (tableColumns(length)) 
(3)創建表的時候直接指定:CREATE TABLE tableName ( [...], INDEX [indexName] (tableColumns(length)) ;

2.唯一索引。
它與前面的"普通索引"類似,不同的就是:索引列的值必須唯一,但允許有空值。如果是組合索引,則列值的組合必須唯一。它有以下幾種創建方式:
(1)創建索引:CREATE UNIQUE INDEX indexName ON tableName(tableColumns(length))
(2)修改表結構:ALTER tableName ADD UNIQUE [indexName] ON (tableColumns(length))
(3)創建表的時候直接指定:CREATE TABLE tableName ( [...], UNIQUE [indexName] (tableColumns(length));

3.主鍵索引
它是一種特殊的唯一索引,不允許有空值。一般是在建表的時候同時創建主鍵索引:CREATE TABLE testIndex(i_testID INT NOT NULL AUTO_INCREMENT,vc_Name VARCHAR(16) NOT NULL,PRIMARY KEY(i_testID)); 當然也可以用ALTER命令。 

發佈了61 篇原創文章 · 獲贊 9 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章