SELECT INTO 語句:表示從一個表中選取數據,而後把數據插入另外一個表中,經常使用來備份一張表mysql
1.全表結構備份:sql
SELECT * INTO new_table_namespa
FROM old_tablename;blog
示例:備份student表,備份表取名爲student_backuprem
select * into student_backuptable
from student ;select
則會生成一張與student表結構及數據同樣的備份表。方法
2.若是隻備份表中的某些列:im
SELECT column_name1,column_name2... INTO new_table_name FROM old_tablename
示例:只備份student表中的sno,name列入新表student_backup
select sno,name into student_backupd3
from student ;
3.若是須要將表中知足必定條件的記錄進行備份,則可使用where字句配套使用
示例:將全部性別爲男的學生記錄備份到新表student_backup
select * into student_backup
from student
where sex='男';
注:可是在mysql中使用SELECT INTO語句是沒法進行備份操做,執行命令時會提示新表未定義
![](http://static.javashuo.com/static/loading.gif)
因此,咱們應該使用下列語句進行數據表的備份操做。
1.只複製表結構到新表 :(只有結構無數據)
create table 新表 select * from 舊錶 where1=2
或create table 新表 like 舊錶
此兩種方法的區別:使用第一條語句,備份的新表並無舊錶的primary key 、auto_increment等屬性,須要從新對新表進行設置
示例:create table newstudent select * from student where 1=2;
![](http://static.javashuo.com/static/loading.gif)
或者 create table newstudent like sutdent;
2.複製表結構及數據到新表
create table 新表 select * from 舊錶;---這種方法會將oldtable中全部的內容都拷貝過來,同時也存在備份的新表不具有舊錶 primary key、auto_increment等屬性,須要對新表再次設置。
示例:複製student表中全部數據到新表student_backup1;
create table student_backup1 select * from student;