mysql拷貝表的幾種方式

2021-06-27 11:28:08 字數 1827 閱讀 9501

mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。

假如我們有以下這樣乙個表:

id      username    password

-----------------------------------

1       admin       *************

2       sameer      *************

3       stewart     *************

create table if not exists `admin` (   

`id` int(6) unsigned not null auto_increment,   

`username` varchar(50) not null default '',   

`password` varchar(100) default null,   

primary key (`id`)   

) engine=myisam default charset=latin1

auto_increment=4 ;  

1. 下面這個語句會拷貝表結構到新錶newadmin中。 (不會拷貝表中的資料)

create table newadmin like admin  

2. 下面這個語句會拷貝資料到新錶中。 注意:這個語句其實只是把select語句的結果建乙個表。所以newadmin這個表不會有主鍵,索引。

create table newadmin as   

(   

select *   

from admin   

)

3. 如果你要真正的複製乙個表。可以用下面的語句。

create table newadmin like admin;   

insert into newadmin select * from admin;  

4. 我們可以操作不同的資料庫。

create table newadmin like shop.admin;   

create table newshop.newadmin like shop.admin;  

5. 我們也可以拷貝乙個表中其中的一些字段。

create table newadmin as   

(   

select username, password from admin   

)

6. 我們也可以講新建的表的字段改名。

create table newadmin as   

(   

select id, username as uname, password as pass from admin   

)

7. 我們也可以拷貝一部分資料。

create table newadmin as   

(   

select * from admin where left(username,1) = 's'   

)

8. 我們也可以在建立表的同時定義表中的字段資訊。

create table newadmin   

(   

id integer not null auto_increment primary key   

)   

as   

(   

select * from admin   

)

mysql拷貝表的幾種方式

mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。假如我們有以下這樣乙個表 id username password 1 admin 2 sameer 3 stewart create table if not exist...

mysql拷貝表的幾種方式

mysql拷貝表的幾種方式 在使用mysql資料庫的過程中,拷貝表使我們經常要用到的操作,下文就為您介紹幾種mysql拷貝表的方式,供您參考學習。mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。假如我們有以下這樣乙個表 ...

mysql拷貝表的幾種方式

mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。假如我們有以下這樣乙個表 id username password 1 admin 2 sameer 3 stewart create table if not exist...