alter ignore table add unique key
来源:互联网 发布:电脑桌面远程控制软件 编辑:程序博客网 时间:2024/06/03 02:09
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命令。
- alter ignore table add unique key
- alter ignore table 添加unique key
- alter ignore table 添加unique key不生效的问题和临时解决方案
- ALTER TABLE ADD UNIQUE (b); INSERT INTO ON DUPLICATE KEY UPDATE
- mysql 在修改添加字段(alter table 表名 add column 或者 modify column)且带unique时提示duplicate entry for key的原因以及解决方案
- alter table *** add constraint
- alter table tb_name change add add int(10) not null auto_increment primary key;
- alter table *** add constraint *** 用法
- alter table *** add constraint *** 用法
- alter table *** add constraint *** 用法
- EJB3.x异常:Unsuccessful: alter table ORDER add constraint FK47F8F2EFC558D0A foreign key (ORDER_USR_ID) references CUSTOMER
- MySQL如何创建主键?Alter table add PRIMARY KEY的用法和例子
- MySQL如何创建主键?Alter table add PRIMARY KEY的用法和例子
- MySQL如何创建主键?Alter table add PRIMARY KEY的用法和例子
- create unique index与alter .. add constraint .. unique的差异
- Oracle alter table add column example
- mysql建立索引的优缺点|创建索引alter或create索引分类(PRIMARY KEY,UNIQUE KEY,FULLTEXT,INDEX)作用查看索引show index from table
- SQL: Add Unique Constraint on existing table
- 【PHP扩展开发必备】在PHP扩展中通过C语言调用PHP函数与方法
- 网页中播放PPT 幻灯片
- 性能测试知多少---系统架构分析
- 温故而知新(一): “Incremental Backups in Oracle 10.2.0.5” Translation & Notes
- 使用SQL BAK Reader 小工具查看SQL Server备份文件内容(不需要打开SQL Server)
- alter ignore table add unique key
- SVN状态
- Delphi编写Win7屏幕保护唤醒工具
- Sql server 事务的两种用法
- JAVA中this用法小结
- 性能测试vs负载测试vs压力测试
- Travel备忘录
- raid基本知识
- 计数排序