{"id":4681,"date":"2020-01-28T19:31:18","date_gmt":"2020-01-28T11:31:18","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4681"},"modified":"2020-01-28T19:31:18","modified_gmt":"2020-01-28T11:31:18","slug":"mysql-create-table-like-%e5%92%8c-create-table-as-%e6%8b%b7%e8%b4%9d%e5%a4%8d%e5%88%b6%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4681","title":{"rendered":"MySQL create table like \u548c create table as \u62f7\u8d1d\u590d\u5236\u8868"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5927\u591a\u6570\u7a0b\u5e8f\u5458\u6240\u5728\u7684\u516c\u53f8\u53ef\u80fd\u90fd\u5ef6\u8fdf\u5f00\u5de5\u4e86\uff0c\u5728\u7a7a\u95f2\u4e0b\u6765\u7684\u65f6\u95f4\u91cc\u662f\u5b66\u4e60\u7684\u7edd\u4f73\u65f6\u673a\uff0c\u5343\u4e07\u4e0d\u80fd\u9519\u8fc7\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eca\u5929\uff0c\u6211\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e24\u79cd\uff0c\u4e0d\u5e38\u7528\u4e5f\u4e0d\u5e38\u89c1\u7684\u4e24\u79cd\u8868\u590d\u5236\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u770b\u770b\u6211\u7684\u6d4b\u8bd5\u8868\u7ed3\u6784\u548c\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `xttblog` (\n  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'test1',\n  `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'www.xttblog.com',\n  `age` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `index_name` (`name`)\n) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;\nBEGIN;\nINSERT INTO `xttblog` VALUES ('1', '1', '1'), ('2', 'a', '1');\nCOMMIT;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001create table like\u3002\u8fd9\u79cd\u8bed\u53e5\uff0cOracle \u4e2d\u6ca1\u6709\uff0c\u4f46\u662f pg \u7b49\u6570\u636e\u5e93\u90fd\u652f\u6301\u3002\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE new_table LIKE xttblog;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u5c06\u8868 xttblog \u590d\u5236\u5230 new_table \u65f6\u5019\u4f1a\u5c06\u8868 xttblog \u5b8c\u6574\u7684\u5b57\u6bb5\u7ed3\u6784\u548c\u7d22\u5f15\u590d\u5236\u5230\u8868 new_table \u4e2d\u6765\uff0c\u4f46\u662f\u4e0d\u4f1a\u590d\u5236\u6570\u636e\u3002\u4e0d\u590d\u5236\u6570\u636e\u4e5f\u5c31\u610f\u5473\u7740\u4e0d\u4f1a\u590d\u5236\u5916\u952e\u7ea6\u675f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u539f\u8bdd\u4e3a\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The LIKE form of CREATE TABLE allows you to copy an existing table definition exactly (without copying its data). In contrast to CTAS, the statement below creates a new empty_key_value_store table whose definition exactly matches the existing key_value_store in all particulars other than table name. The new table contains no rows.<\/p><cite>MySQL \u5b98\u7f51<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ffb\u8bd1\u8fc7\u6765\u7684\u5927\u81f4\u610f\u601d\u662f\u8bf4\uff1aCREATE TABLE \u521b\u5efa\u8868\u7684\u5f62\u5f0f\u5141\u8bb8\u60a8\u7cbe\u786e\u5730\u590d\u5236\u73b0\u6709\u8868\u5b9a\u4e49(\u4e0d\u590d\u5236\u5176\u6570\u636e)\uff0c\u5176\u521b\u5efa\u7684\u8868\u9664\u4e86\u8868\u540d\u548c\u6e90\u8868\u4e0d\u4e00\u6837\u5916\uff0c\u5176\u4f59\u6240\u6709\u7684\u7ec6\u8282\u90fd\u662f\u4e00\u6837\u7684\u3002\u4f46\u662f\u6ca1\u6709\u6e90\u8868\u7684\u6570\u636e\u3002\u6240\u4ee5\u8fd9\u79cd create\u00a0 table like \u7684\u5f62\u5f0f\u975e\u5e38\u9002\u5408\u5bf9\u6e90\u8868\u6a21\u5f0f\u7684\u590d\u5236\u3002\u5b9e\u9645\u5f00\u53d1\u4e2d\u5176\u5b9e\u7528\u7684\u4e5f\u4e0d\u662f\u592a\u591a\uff0c\u66f4\u591a\u7ec6\u8282\u53c2\u8003\u5b98\u7f51\uff1a<a rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-table-like.html\" target=\"_blank\">https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-table-like.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u60f3\u8981\u590d\u5236\u6570\u636e\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 create table as\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001create table as\u3002\u8fd9\u79cd\u8bed\u53e5\uff0c\u5f88\u591a\u6570\u636e\u5e93\u90fd\u652f\u6301\uff0c\u4e0b\u9762\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table new_xttblog as SELECT * from xttblog;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">create table as \u7684\u7f3a\u70b9\u662f\uff0c\u5b83\u53ea\u590d\u5236\u8868\u7ed3\u6784\u548c\u8868\u6570\u636e\uff0c\u4e0d\u4f1a\u590d\u5236\u8868\u7684\u4e3b\u952e\u548c\u7d22\u5f15\u3002\u4e3b\u952e\u4e5f\u662f\u4e00\u79cd\u7d22\u5f15\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u7edf\u4e00\u7684\u8bf4\uff0c\u5b83\u4e0d\u4f1a\u590d\u5236\u8868\u4e2d\u7684\u7d22\u5f15\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u7684\u7ec6\u8282\uff0c\u5efa\u8bae\u53c2\u8003\u5b98\u7f51\uff1a<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-table-select.html\">https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/create-table-select.html<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u5b8c\u6574\u7684\u590d\u5236\u8868\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u9762\u4e24\u79cd\u65b9\u5f0f\u90fd\u6709\u7f3a\u9677\uff0c\u5982\u679c\u60f3\u5b8c\u6574\u7684\u590d\u5236\u8868\uff0c\u5219\u53ef\u4ee5\u8fd9\u6837\u505a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE new_t LIKE xttblog;   \nINSERT INTO new_t SELECT * FROM xttblog;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001create table as \u7684\u53d8\u79cd\u7528\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\uff0ccreate table as \u53ea\u590d\u5236\u8868\u7ed3\u6784\u548c\u6570\u636e\uff0c\u4f46\u662f\u5b83\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7528\u6cd5\uff0c\u8ba9\u590d\u5236\u7684\u8868\u6570\u636e\u7684\u65f6\u5019\u5148\u521b\u5efa\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE xttblog_new\n(   \n\tid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY   \n)   \nAS   \n(   \n\tSELECT * FROM xttblog   \n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u8868\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u4e2d\u7684\u4e00\u4e9b\u5b57\u6bb5\u8bbe\u7f6e\u4e3b\u952e\uff0c\u7d22\u5f15\u7b49\u5185\u5bb9\u3002\u6ca1\u6709\u663e\u793a\u6307\u5b9a\u7684\u5b57\u6bb5\uff0c\u5c06\u76f4\u63a5\u590d\u5236\uff0c\u4f46\u4e0d\u4f1a\u590d\u5236\u7d22\u5f15\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>desc xttblog_new;\nshow index from xttblog_new;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4e24\u4e2a\u8bed\u53e5\u53ef\u4ee5\u67e5\u770b\u8868\u7ed3\u6784\u548c\u7d22\u5f15\u3002create table as \u7684\u53e6\u5916\u4e00\u4e2a\u7279\u70b9\u662f\uff0c\u53ef\u4ee5\u5728\u590d\u5236\u8868\u65f6\u65b0\u589e\u5b57\u6bb5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE taoge\n(   \n\tid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\tcard_no VARCHAR(18)\n)   \nAS   \n(   \n\tSELECT * FROM xttblog   \n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\uff0cINSERT INTO SELECT \u6211\u5c31\u4e0d\u8bf4\u4e86\uff0c\u800c SELECT INTO FROM \u663e\u7136 MySQL \u662f\u4e0d\u652f\u6301\u7684\uff0c\u5e76\u4e14\u5b83\u4e5f\u4e0d\u652f\u6301\u590d\u5236\u7ea6\u675f\uff0c\u7d22\u5f15\u4ee5\u53ca\u89e6\u53d1\u5668\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4681\">MySQL create table like \u548c create table as \u62f7\u8d1d\u590d\u5236\u8868<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5927\u591a\u6570\u7a0b\u5e8f\u5458\u6240\u5728\u7684\u516c\u53f8\u53ef\u80fd\u90fd\u5ef6\u8fdf\u5f00\u5de5\u4e86\uff0c\u5728\u7a7a\u95f2\u4e0b\u6765\u7684\u65f6\u95f4\u91cc\u662f\u5b66\u4e60\u7684\u7edd\u4f73\u65f6\u673a\uff0c\u5343\u4e07\u4e0d\u80fd\u9519\u8fc7\uff01 \u4eca\u5929\uff0c\u6211\u7ed9\u5927\u5bb6\u4ecb\u7ecd [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[83,580,392,393,447],"class_list":["post-4681","post","type-post","status-publish","format-standard","hentry","category-sql","tag-mysql","tag-mysql----dba"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4681"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4681\/revisions"}],"predecessor-version":[{"id":4682,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4681\/revisions\/4682"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}