设为首页 收藏本站
查看: 1139|回复: 0

[经验分享] MySQL Handling Duplicates

[复制链接]

尚未签到

发表于 2016-9-10 12:54:26 | 显示全部楼层 |阅读模式
  Tables or result sets sometimes contain duplicate records. Sometime
it is allowed but sometime it is required to stop duplicate records.
Sometime it is required to identify duplicate records and remove them
from the table. This chapter will describe how to prevent duplicate
records occurring in a table and how to remove already existing
duplicate records.

Preventing Duplicates from Occurring in a Table:
  You can use a PRIMARY KEY
or UNIQUE
Index on a table
with appropriate fields to stop duplicate records. Lets take one
example, The following table contains no such index or primary key, so
it would allow duplicate records for first_name and last_name


CREATE TABLE person_tbl
(
first_name CHAR(20),
last_name CHAR(20),
sex CHAR(10)
);

  To prevent multiple records with the same first and last name values
from being created in this table, add a PRIMARY KEY to its definition.
When you do this, it's also necessary to declare the indexed columns to
be NOT NULL, because a PRIMARY KEY does not allow NULL values:


CREATE TABLE person_tbl
(
first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
sex CHAR(10)
PRIMARY KEY (last_name, first_name)
);

  The presence of a unique index in a table normally causes an error
to occur if you insert a record into the table that duplicates an
existing record in the column or columns that define the index.
  Use INSERT IGNORE
rather than INSERT
. If a record
doesn't duplicate an existing record, MySQL inserts it as usual. If the
record is a duplicate, the IGNORE keyword tells MySQL to discard it
silently without generating an error.
  Following example does not error out and same time it will not insert duplicate records.


mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 0 rows affected (0.00 sec)

  Use REPLACE
rather than INSERT. If the record is new, it's
inserted just as with INSERT. If it's a duplicate, the new record
replaces the old one:


mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 1 row affected (0.00 sec)
mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 2 rows affected (0.00 sec)

  INSERT IGNORE and REPLACE should be chosen according to the
duplicate-handling behavior you want to effect. INSERT IGNORE keeps the
first of a set of duplicated records and discards the rest. REPLACE
keeps the last of a set of duplicates and erase out any earlier ones.
  Another way to enforce uniqueness is to add a UNIQUE index rather than a PRIMARY KEY to a table.


CREATE TABLE person_tbl
(
first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
sex CHAR(10)
UNIQUE (last_name, first_name)
);


Counting and Identifying Duplicates:
  Following is the query to count duplicate records with first_name and last_name in a table.


mysql> SELECT COUNT(*) as repetitions, last_name, first_name
-> FROM person_tbl
-> GROUP BY last_name, first_name
-> HAVING repetitions > 1;

  This query will return a list of all the duplicate records in
person_tbl table. In general, to identify sets of values that are
duplicated, do the following:


  •   Determine which columns contain the values that may be duplicated.

  •   List those columns in the column selection list, along with COUNT(*).

  •   List the columns in the GROUP BY clause as well.

  •   Add a HAVING clause that eliminates unique values by requiring group counts to be greater than one.


Eliminating Duplicates from a Query Result:
  You can use DISTINCT
along with SELECT statement to find out unique records available in a table.


mysql> SELECT DISTINCT last_name, first_name
-> FROM person_tbl
-> ORDER BY last_name;

  An alternative to DISTINCT is to add a GROUP BY clause that names
the columns you're selecting. This has the effect of removing
duplicates and selecting only the unique combinations of values in the
specified columns:


mysql> SELECT last_name, first_name
-> FROM person_tbl
-> GROUP BY (last_name, first_name);


Removing Duplicates Using Table Replacement:
  If you have duplicate records in a table and you want to remove all
the duplicate records from that table then here is the procedure.


mysql> CREATE TABLE tmp SELECT last_name, first_name, sex
->                  FROM person_tbl;
->                  GROUP BY (last_name, first_name);
mysql> DROP TABLE person_tbl;
mysql> ALTER TABLE tmp RENAME TO person_tbl;

  An easy way of removing duplicate records from a table is that add
an INDEX or PRIMAY KEY to that table. Even if this table is already
available you can use this technique to remove duplicate records and
you will be safe in future as well.

mysql> ALTER IGNORE TABLE person_tbl
-> ADD PRIMARY KEY (last_name, first_name);

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-270430-1-1.html 上篇帖子: mysql如何执行脚本文件 下篇帖子: mysql全文检索
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表