SQL常见问题整理(五)

8.处理重复数据

(1)防止表中出现重复数据
使用primary key索引
create table person(last_name char(20) not null,first_name char(20) not null,address char(40),primary key(last_name,first_name));
使用unique索引
create table person(last_name char(20) not null,first_name char(20) not null,address char(40),unique(last_name,first_name));
(2)处理重复的加载数据
忽略这些错误
使用insert ignore,replace或insert…on duplicate key update语句,这些语句可以改变MySQL处理重复数据的行为:
插入新行:insert ignore into person(last_name,first_name) values(‘X2’,‘Y2’);
替换原有的行:replace into person (last_name,first_name) values(‘X3’,‘Y3’);
(3)识别与统计重复数据的数量
使用可以显示重复值的计数汇总方式,如果想查看包含重复值的行,那么可以通过连接汇总信息与原来的表来显示重复的行
一般情况想,如果想识别重复的值,那么可以按照以下操作:确定哪些列包含可能重复的值,在输出中显示这些列以及count(),在group by子句中指定这些列,添加一个having子句,通过要求每组的计数大于1来去除唯一的值
select count(
),last_name,last_name from catalog_list group by last_name,first_name having count(*)>1;
(4)移除表中的重复数据
从表中选择具有唯一性的行到第二个表,然后用这个表来替换原来的表:创建一个与原表结构形同的新表,你可以使用create table…like,create table tmp like catalog_list;使用insert into… select distinct,从原始表中选择具有唯一性的行放到新表中,insert into tmp select distinct * from catalog_list;选择tmp表中的行,验证这个新表中没有重复数据,select * from tmp order by last_name,first_name;在创建好只包含唯一数据的tmp表后,用它替换原来的catalog_list表,drop table catalog_list;rename table tmp to catalog_list;这个过程最后的结果是catalog_list不再包含重复数据
使用delete…limit n删除一组特定重复行的所有行,只留下一行delete from catalog_list where last_name=‘Baxter’ and firt_name=‘Wallace’ limit 2;delete from catalog_list where last_name=‘Pinter’ and first_name=‘Marlene’ limit1;select * from catalog_list;

猜你喜欢

转载自blog.csdn.net/weixin_47970003/article/details/123764073