{"id":2902,"date":"2016-09-25T18:25:36","date_gmt":"2016-09-25T10:25:36","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2902"},"modified":"2016-09-25T18:25:36","modified_gmt":"2016-09-25T10:25:36","slug":"mysql%e8%a1%a8%e7%bb%93%e6%9e%84%e5%86%85%e5%ae%b9%e7%9a%84%e5%a4%8d%e5%88%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2902.html","title":{"rendered":"MySQL\u8868\u7ed3\u6784\/\u5185\u5bb9\u7684\u590d\u5236"},"content":{"rendered":"<p>=Start=<\/p>\n<h5>\u7f18\u7531\uff1a<\/h5>\n<p>\u5907\u4efd\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u7ebf\u4e0a\u64cd\u4f5c\uff0c\u5907\u4efd\u4e3a\u5148\u3002<\/p>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>MySQL\u8868\u7ed3\u6784\u7684\u590d\u5236<\/h6>\n<pre class=\"lang:default decode:true\"># \u5b8c\u6574\u7684\u5c06 \u8868tbl_name1 \u4e2d\u7684\u5b57\u6bb5\u7ed3\u6784\u548c\u7d22\u5f15\u590d\u5236\u5230 \u8868tbl_name2 \u4e2d\ncreate table tbl_name2 like tbl_name1;\n\n# \u53ea\u590d\u5236 \u8868tbl_name1 \u4e2d\u7684\u5b57\u6bb5\u7ed3\u6784\u5230 \u8868tbl_name2 \u4e2d\uff08\u4e0d\u590d\u5236\u7d22\u5f15\uff0c\u597d\u5904\u5728\u4e8e\u6bd4\u8f83\u7075\u6d3b\uff0c\u53ef\u4ee5\u6307\u5b9a\u8981\u590d\u5236\u54ea\u4e9b\u5b57\u6bb5\uff09\ncreate table tbl_name2 as select col_a, col_b from tbl_name1 limit 0;\n\n# \u5148\u67e5\u770b \u8868tbl_name1 \u4e2d\u521b\u5efa\u8bed\u53e5\uff0c\u7136\u540e(\u6539\u4e00\u4e0b\u8868\u540d\u548cAUTO_INCREMENT)\u518d\u6267\u884c\u4e00\u904d\u4ee5\u521b\u5efa \u8868tbl_name2\nshow create table tbl_name1;<\/pre>\n<h6>MySQL\u8868\u6570\u636e\u7684\u590d\u5236<\/h6>\n<pre class=\"lang:default decode:true \">\u5148\u5efa\u8868\uff1a\nSHOW CREATE TABLE `table1`;\nCREATE TABLE `table2`(...); # \u6ce8\u610f\u4fee\u6539 \u8868\u540d &amp; AUTO_INCREMENT\n\u6216\nCREATE TABLE `table2` LIKE `table1`;\n\u7136\u540e\u590d\u5236\u6570\u636e\uff1a\nINSERT INTO `table2`(fields you want) SELECT fields you want FROM `table1`;\nINSERT INTO `table2` SELECT * FROM `table1`; #\u5c06`table1`\u4e2d\u7684\u6570\u636e\u590d\u5236\u5230`table2`\u4e2d\n\n\u5b9e\u4f8b\uff1a\nCREATE TABLE `product_backup` LIKE `product_id`;\nLOCK TABLES product_backup WRITE;\nLOCK TABLES product_id READ;\nINSERT INTO product_backup SELECT * FROM product ORDER BY product_id;\nUNLOCK TABLES;\n\n\n\u590d\u5236\u8868\u7ed3\u6784(\u4e0d\u5305\u62ec\u7d22\u5f15)+\u590d\u5236\u6570\u636e\uff1a\nCREATE TABLE `table2` [AS] SELECT * FROM `table1`;\n\n====\n\u5efa\u8bae\u5148\u5bfc\u51fa\uff0c\u518d\u5bfc\u5165\uff1a\nSELECT * FROM `table1` INTO OUTFILE '\/tmp\/tbl1.txt';\nLOAD DATA INFILE '\/tmp\/tbl1.txt' INTO TABLE `table2`;\n\n\u4e0d\u5efa\u8bae\u76f4\u63a5\u63d2\u5165\uff1a\nINSERT INTO `table2` SELECT * from `table1`;<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-table-select.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-table-select.html<\/a><\/li>\n<li><a href=\"http:\/\/leeon.me\/a\/mysql-create-table-like-vs-create-table-as-select\">http:\/\/leeon.me\/a\/mysql-create-table-like-vs-create-table-as-select<\/a><\/li>\n<li><a href=\"http:\/\/blog.51yip.com\/mysql\/1311.html\">http:\/\/blog.51yip.com\/mysql\/1311.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/6595252\/mysql-creating-a-new-table-with-information-from-a-query\">http:\/\/stackoverflow.com\/questions\/6595252\/mysql-creating-a-new-table-with-information-from-a-query<\/a><\/li>\n<li><a href=\"https:\/\/www.percona.com\/blog\/2006\/07\/12\/insert-into-select-performance-with-innodb-tables\/\">https:\/\/www.percona.com\/blog\/2006\/07\/12\/insert-into-select-performance-with-innodb-tables\/<\/a> #\u4e24\u79cd\u590d\u5236\u65b9\u6cd5\u7684\u6bd4\u8f83<\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5907\u4efd\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u7ebf\u4e0a\u64cd\u4f5c\uff0c\u5907\u4efd\u4e3a\u5148\u3002 \u53c2\u8003\u89e3\u7b54\uff1a MySQL\u8868\u7ed3\u6784\u7684\u590d\u5236 # \u5b8c\u6574\u7684 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23],"tags":[16],"class_list":["post-2902","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","tag-mysql"],"views":3431,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2902"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}