表結構操作

2022-03-09 23:13:35 字數 789 閱讀 7128

1、複製表結構及資料到新錶

create table 新錶select * from 舊表

這種方法會將oldtable中所有的內容都拷貝過來,當然我們可以用delete from newtable;來刪除。

不過這種方法的乙個最不好的地方就是新錶中沒有了舊表的primary key、extra(auto_increment)等屬性。需要自己用"alter"新增,而且容易搞錯。

2、只複製表結構到新錶

create table 新錶select * from 舊表where 1=2

或create table 新錶like 舊表

3、複製舊表的資料到新錶(假設兩個表結構一樣)

insert into 新錶select * from 舊表

4、複製舊表的資料到新錶(假設兩個表結構不一樣)

insert into 新錶(欄位1,欄位2,.......) select 欄位1,欄位2,...... from 舊表

5、可以將表1結構複製到表2

select * into 表2 from 表1 where 1=2

6、可以將表1內容全部複製到表2

select * into 表2 from 表1

7、 show create table 舊表;

這樣會將舊表的建立命令列出。我們只需要將該命令拷貝出來,更改table的名字,就可以建立乙個完全一樣的表

8、mysql

dump

用mysqldump將表dump出來,改名字後再導回去或者直接在命令列中執行

通過 為知筆記 發布

表結構操作

查詢使用者 scott 下所有的表名 select table name from all tables where owner scott 查詢表 emp所有欄位的資訊 select from all tab columns where table name emp 表中的索引列 select f...

sql 表結構操作

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...

MySql 操作表結構

mysql 一些簡單對錶操作的語句 一 建立表 和 建立臨時表 建立表 create table table name column one int,column two varchar 20 建立臨時表 createtemporarytabletable name column one int,c...