SQL语句之备份表

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语句是没法进行备份操做,执行命令时会提示新表未定义

 

因此,咱们应该使用下列语句进行数据表的备份操做。

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;
 create table student_backup1 select * from student;

相关文章
相关标签/搜索