{"id":244,"date":"2023-02-24T06:03:19","date_gmt":"2023-02-23T22:03:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=244"},"modified":"2023-04-30T15:20:06","modified_gmt":"2023-04-30T07:20:06","slug":"mysql-replication-table","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-replication-table\/","title":{"rendered":"MySQL \u590d\u5236\u8868"},"content":{"rendered":"<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5b8c\u5168\u590d\u5236MySQL\u7684\u6570\u636e\u8868\uff0c\u5305\u62ec\u8868\u7684\u7ed3\u6784\uff0c\u7d22\u5f15\uff0c\u9ed8\u8ba4\u503c\u7b49\u3002\u4ec5\u4ec5\u4f7f\u7528<code>CREATE TABLE ... SELECT<\/code>\u547d\u4ee4\uff0c\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u5982\u4f55\u5b8c\u6574\u7684\u590d\u5236MySQL\u6570\u636e\u8868\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>SHOW CREATE TABLE<\/code>\u547d\u4ee4\u83b7\u53d6\u521b\u5efa\u6570\u636e\u8868(<code>CREATE TABLE<\/code>)\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u5305\u542b\u4e86\u539f\u6570\u636e\u8868\u7684\u7ed3\u6784\uff0c\u7d22\u5f15\u7b49<\/li>\n<li>\u590d\u5236\u547d\u4ee4\u663e\u793a\u7684SQL\u8bed\u53e5\uff0c\u4fee\u6539\u6570\u636e\u8868\u540d\uff0c\u5e76\u6267\u884cSQL\u8bed\u53e5\uff0c\u901a\u8fc7\u4ee5\u4e0a\u547d\u4ee4\u5c06\u5b8c\u5168\u7684\u590d\u5236\u6570\u636e\u8868\u7ed3\u6784<\/li>\n<li>\u5982\u679c\u60f3\u590d\u5236\u8868\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>INSERT INTO ... SELECT<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u64cd\u4f5c\u5b9e\u4f8b<\/h2>\n<p>\uff081\uff09\u83b7\u53d6\u6570\u636e\u8868\u7684\u5b8c\u6574\u7ed3\u6784<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SHOW CREATE TABLE appblog_tbl \\G;\n*************************** 1. row ***************************\n       Table: appblog_tbl\nCreate Table: CREATE TABLE `appblog_tbl` (\n  `appblog_id` int(11) NOT NULL auto_increment,\n  `appblog_title` varchar(100) NOT NULL default &#039;&#039;,\n  `appblog_author` varchar(40) NOT NULL default &#039;&#039;,\n  `submission_date` date default NULL,\n  PRIMARY KEY  (`appblog_id`),\n  UNIQUE KEY `AUTHOR_INDEX` (`appblog_author`)\n) ENGINE=InnoDB \n1 row in set (0.00 sec)\n\nERROR:\nNo query specified<\/code><\/pre>\n<p>\uff082\uff09\u4fee\u6539SQL\u8bed\u53e5\u7684\u6570\u636e\u8868\u540d\uff0c\u5e76\u6267\u884cSQL\u8bed\u53e5<\/p>\n<pre><code class=\"language-sql\">mysql&gt; CREATE TABLE `clone_tbl` (\n  -&gt; `appblog_id` int(11) NOT NULL auto_increment,\n  -&gt; `appblog_title` varchar(100) NOT NULL default &#039;&#039;,\n  -&gt; `appblog_author` varchar(40) NOT NULL default &#039;&#039;,\n  -&gt; `submission_date` date default NULL,\n  -&gt; PRIMARY KEY  (`appblog_id`),\n  -&gt; UNIQUE KEY `AUTHOR_INDEX` (`appblog_author`)\n  -&gt; ) ENGINE=InnoDB;\nQuery OK, 0 rows affected (1.80 sec)<\/code><\/pre>\n<p>\uff083\uff09\u6267\u884c\u5b8c\u7b2c\u4e8c\u6b65\u9aa4\u540e\uff0c\u5c06\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u65b0\u7684\u514b\u9686\u8868<code>clone_tbl<\/code>\u3002\u5982\u679c\u60f3\u62f7\u8d1d\u6570\u636e\u8868\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528<code>INSERT INTO... SELECT<\/code>\u8bed\u53e5\u5b9e\u73b0<\/p>\n<pre><code class=\"language-sql\">mysql&gt; INSERT INTO clone_tbl (appblog_id,\n    -&gt;                        appblog_title,\n    -&gt;                        appblog_author,\n    -&gt;                        submission_date)\n    -&gt; SELECT appblog_id,appblog_title,\n    -&gt;        appblog_author,submission_date\n    -&gt; FROM appblog_tbl;\nQuery OK, 3 rows affected (0.07 sec)\nRecords: 3  Duplicates: 0  Warnings: 0<\/code><\/pre>\n<p>\u6267\u884c\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u5c06\u5b8c\u6574\u7684\u590d\u5236\u8868\uff0c\u5305\u62ec\u8868\u7ed3\u6784\u53ca\u8868\u6570\u636e\u3002<\/p>\n<h2>\u53e6\u4e00\u79cd\u5b8c\u6574\u590d\u5236\u8868\u7684\u65b9\u6cd5<\/h2>\n<pre><code class=\"language-sql\">CREATE TABLE targetTable LIKE sourceTable;\nINSERT INTO targetTable SELECT * FROM sourceTable;<\/code><\/pre>\n<p>\u5176\u4ed6:<\/p>\n<p>\u53ef\u4ee5\u62f7\u8d1d\u4e00\u4e2a\u8868\u4e2d\u5176\u4e2d\u7684\u4e00\u4e9b\u5b57\u6bb5\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE newadmin AS\n(\n    SELECT username, password FROM admin\n)<\/code><\/pre>\n<p>\u53ef\u4ee5\u5c06\u65b0\u5efa\u7684\u8868\u7684\u5b57\u6bb5\u6539\u540d\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE newadmin AS\n(  \n    SELECT id, username AS uname, password AS pass FROM admin\n)<\/code><\/pre>\n<p>\u53ef\u4ee5\u62f7\u8d1d\u4e00\u90e8\u5206\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE newadmin AS\n(\n    SELECT * FROM admin WHERE LEFT(username,1) = &#039;s&#039;\n)<\/code><\/pre>\n<p>\u53ef\u4ee5\u5728\u521b\u5efa\u8868\u7684\u540c\u65f6\u5b9a\u4e49\u8868\u4e2d\u7684\u5b57\u6bb5\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE newadmin\n(\n    id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY\n)\nAS\n(\n    SELECT * FROM admin\n)  <\/code><\/pre>\n<p>\u533a\u5206\u4e0bMySql\u590d\u5236\u8868\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>\uff081\uff09\u53ea\u590d\u5236\u8868\u7ed3\u6784\u5230\u65b0\u8868<\/p>\n<pre><code class=\"language-sql\">create table \u65b0\u8868 select * from \u65e7\u8868 where 1=2<\/code><\/pre>\n<p>\u6216\u8005<\/p>\n<pre><code class=\"language-sql\">create table \u65b0\u8868 like \u65e7\u8868<\/code><\/pre>\n<p>\uff082\uff09\u590d\u5236\u8868\u7ed3\u6784\u53ca\u6570\u636e\u5230\u65b0\u8868<\/p>\n<pre><code class=\"language-sql\">create table \u65b0\u8868 select * from \u65e7\u8868<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5b8c\u5168\u590d\u5236MySQL\u7684\u6570\u636e\u8868\uff0c\u5305\u62ec\u8868\u7684\u7ed3\u6784\uff0c\u7d22\u5f15\uff0c\u9ed8\u8ba4\u503c\u7b49\u3002\u4ec5\u4ec5\u4f7f\u7528CREATE TABLE &#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}