如何對MySQL資料表進行複製 表結構複製

2021-07-24 16:42:43 字數 1041 閱讀 3510

本篇文章為大家介紹如何對mysql進行複製、表結構複製,也可以分欄位進行複製。也可以將一張表中的資料複製到另一張表當中。

1、複製表結構(語法 creata table 舊表 select * from 新錶)

create table t1(

id int unsigned auto_increment primary key,

name varchar(32) not null default '',

pass int not null default 0 );

desc 檢視表結構

建立表 t2 同時複製表 t1 表結構create table t2 select * from t1;

desc t2 檢視表結構

注意:兩張的表字段結構一樣,但是 主鍵 primary key 和 自增 auto_increment 沒有了,所以這種方法不推薦大家使用,那如何才能建立出兩張完全一樣的表呢,辦法肯定有的,如下面語句。 

create  table  t2 like t1; 

這就可以建立一張 t2 和 t1 完全一樣的表了。

2、指定字段複製表結構

語法: create  table  新錶  select 欄位1,欄位2 … from 舊表

3、複製表中資料

假設要把表 t1 中的資料全部複製到表 t2中

insert  into  t2 select * from  t1;

如果只想複製某個字段 

insert  into  t2(欄位1,欄位2) select 欄位1,欄位2 from  t1;

MySQL複製資料表

主題 下面是我在複製表結構以及資料時最常使用的搭配 table new 新錶 table old 原表 create table new like table old 完整複製原表的建表語句以建立新錶 insert into table new select from table old 完整複製原...

MySQL 複製資料表

假設現在有張資料表 users create table users userid int 10 unsigned not null username varchar 100 unique passwd varchar 100 default 123456 primary key userid en...

如何對Datatable型別的資料表進行行列轉換

最近有個需求需要對查詢到的datatable進行行列轉換的處理,這裡記錄下自己的處理方式。行列轉換 private datatable swaptable datatable tabledata 下標對換 string arrswap newstring intcolumns,introws for...