mysql sql 備份表 SQL語句之備份表

2021-10-17 13:33:59 字數 1857 閱讀 9635

select into 語句:表示從乙個表中選取資料,然後把資料插入另乙個表中,常用來備份一張表

1.全表結構備份:

select * into new_table_name

from old_tablename;

示例:備份student表,備份表取名為student_backup

select * into student_backup

from student ;

則會生成一張與student表結構及資料一樣的備份表。

只是將表a表中的資料插入到b表中:(資料同步/遷移)

insert into table_2(欄位1,欄位2,欄位3) select 欄位1,欄位2,欄位3from table_1 -- table_1 相當於a表 table_2 相當於b表 字段型別對應

--insert語句中的values不能帶select語句

2.如果只備份表中的某些列:

select column_name1,column_name2...

into new_table_name

from old_tablename

示例:只備份student表中的sno,name列入新錶student_backup

select sno,name into student_backup

from student ;

3.如果需要將表中滿足一定條件的記錄進行備份,則可以使用where字句配套使用

示例:將所有性別為男的學生記錄備份到新錶student_backup

select * into student_backup

from student

where ***='男';

注:但是在mysql中使用select into語句是無法進行備份操作,執行命令時會提示新錶未定義

所以,我們應該使用下列語句進行資料表的備份操作。

1.只複製表結構到新錶 :(只有結構無資料)create table 新錶 select * from 舊表 where1=2

或create table 新錶 like 舊表

此兩種方法的區別:使用第一條語句,備份的新錶並沒有舊表的primary key 、auto_increment等屬性,需要重新對新錶進行設定

示例:create table newstudent select * from student where 1=2;

或者 create table newstudent like sutdent;

2.複製表結構及資料到新錶

create table 新錶 select * from 舊表;---這種方法會將oldtable中所有的內容都拷貝過來,同時也存在備份的新錶不具備舊表 primary key、auto_increment等屬性,需要對新錶再次設定。

示例:複製student表中所有資料到新錶student_backup1;

原文:

SQL備份表資料

1 情況說明 對某個表,需要進行某些刪除或修改操作測試,但也需要資料還原,所以需要備份表中資料。2 思路分析 一般操作,將表a所有的資料,備份到新建表b中 若有其他更屌的操作,請告訴我,萬分感謝 3 具體sql實現 庫型別說明 sql server2008 3.1 備份表資料 使用哪個庫說明 use...

SQL 建立表的備份

select into 語句從乙個表中選取資料,然後把資料插入另乙個表中。select into 語句常用於建立表的備份復件或者用於對記錄進行存檔 您可以把所有的列插入新錶 select into new table name in externaldatabase from old tablena...

mysql sql提示 MySQL 使用SQL提示

sql提示 sql hint 是優化資料庫的一種重要手段,簡單來說就是在sql語句中加入一些人為的提示來達到優化操作的目的。下面是乙個使用sql提示的例子 select sql buffer results from 這個語句將強制mysql 生成乙個臨時結果集。只要臨時結果集生成後,所有表上的鎖定...