MySql 数据导出/导入一些Tips
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySql 数据导出/导入一些Tips,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3546字,纯文字阅读大概需要6分钟。
内容图文
![MySql 数据导出/导入一些Tips](/upload/InfoBanner/zyjiaocheng/494/5a0b026d46ec4f3f9b408f7102c72ce8.jpg)
2.插入不重复数据(insert if not exist)时,可以用 INSERT IGNORE或者REPLACE
INSERT IGNORE: 插入重复则失败,但会忽略,继续执行下一条.
REPLACE: 插入重复会直接覆盖,没有则新增.
以上两条均会导致自增ID不连续问题,INSERT失败会自增,REPLACE是先删除后INSERT.所以自增ID都会不连续
ID不连续参考:http://blog.csdn.net/liyong199012/article/details/21516817
引用https://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql
18th October 2007
To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.
There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT … ON DUPLICATE KEY UPDATE.
Imagine we have a table:
CREATE TABLE `transcripts` ( `ensembl_transcript_id` varchar(20) NOT NULL, `transcript_chrom_start` int(10) unsigned NOT NULL, `transcript_chrom_end` int(10) unsigned NOT NULL, PRIMARY KEY (`ensembl_transcript_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Now imagine that we have an automatic pipeline importing transcripts meta-data from Ensembl, and that due to various reasons the pipeline might be broken at any step of execution. Thus, we need to ensure two things: 1) repeated executions of the pipeline will not destroy our database, and 2) repeated executions will not die due to ‘duplicate primary key’ errors.
Method 1: using REPLACE
It’s very simple:
REPLACE INTO `transcripts` SET `ensembl_transcript_id` = ‘ENSORGT00000000001′, `transcript_chrom_start` = 12345, `transcript_chrom_end` = 12678;
If the record exists, it will be overwritten; if it does not yet exist, it will be created. However, using this method isn’t efficient for our case: we do not need to overwrite existing records, it’s fine just to skip them.
Method 2: using INSERT IGNORE Also very simple:
INSERT IGNORE INTO `transcripts` SET `ensembl_transcript_id` = ‘ENSORGT00000000001′, `transcript_chrom_start` = 12345, `transcript_chrom_end` = 12678;
Here, if the ‘ensembl_transcript_id’ is already present in the database, it will be silently skipped (ignored). (To be more precise, here’s a quote from MySQL reference manual: “If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted.”.) If the record doesn’t yet exist, it will be created.
This second method has several potential weaknesses, including non-abortion of the query in case any other problem occurs (see the manual). Thus it should be used if previously tested without the IGNORE keyword.
There is one more option: to use INSERT … ON DUPLICATE KEY UPDATE syntax, and in the UPDATE part just do nothing do some meaningless (empty) operation, like calculating 0+0 (Geoffray suggests doing the id=id assignment for the MySQL optimization engine to ignore this operation). Advantage of this method is that it only ignores duplicate key events, and still aborts on other errors.
As a final notice: this post was inspired by Xaprb. I’d also advise to consult his other post on writing flexible SQL queries.
MySql 数据导出/导入一些Tips
标签:nal date 知识 line 转义 cat read was fray
本文系统来源:http://www.cnblogs.com/lqopr/p/7814104.html
内容总结
以上是互联网集市为您收集整理的MySql 数据导出/导入一些Tips全部内容,希望文章能够帮你解决MySql 数据导出/导入一些Tips所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。