mysql表結構複製

shiri512003發表於2010-05-19

作者: 絃樂之花 | 可以轉載, 但必須以超連結形式標明文章原始出處和作者資訊及版權宣告

連結http://shiri512003.itpub.net/post/37713/500220

[@more@]

我們特別是oracle dbas常常會透過ctas來複製表結構,這樣複製出來的表實際上會丟掉表的一些屬性,索引就更不說了,在mysql中這樣來複製表其實得到的也只是表大體結構而已。

還好mysql提供了create table like命令方便進行表結構的複製,廢話不說了


02:55:17>use test
Database changed
03:25:22>create table t1(a int,b int,key (a)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)

03:25:49>show create table t1G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
KEY `a` (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

03:25:55>create table t2 as select * from t1 where 1=0;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

03:26:16>show create table t2G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

03:26:25>create table t3 like t1;
Query OK, 0 rows affected (0.00 sec)

03:26:44>show create table t3G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
KEY `a` (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7591490/viewspace-1033730/,如需轉載,請註明出處,否則將追究法律責任。

相關文章