MYSQL语句大全:表复制及备份还原

2020-07-2817:42:13数据库教程Comments1,398 views字数 632阅读模式

假设现在有表books:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

1.复制表结构文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

  1.1 含有主键等信息的完整表结构文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

  CREATE table 新表名 LIKE book;

  1.2 只有表结构,没有主键等信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

  create table 新表名 select * from books;
  或
  create table  新表名 as (select * from book);
  或
  create table 新表名 select * from books where 1=2;

2.将旧表中的数据灌入新表文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

INSERT INTO 新表 SELECT * FROM 旧表;

注:新表必须已经存在文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

3.输入创建表的DDL语句文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

show create table 表名;

4.清空表数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

truncate table 表名;

5.备份数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

比如备份library数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

进去Mysql的bin目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

C:\Program Files\MySQL\MySQL Server 5.5\bin>

利用“mysqldump  -u 用户名 -p 数据库名 >备份名字”导出数据库到文件

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqldump -u root -p library >library.sql

Enter password: ***

即可.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

6.还原数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

还原library数据库为例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

先create database library文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

然后 下mysql>下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

输入“source 路径”文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/19902.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/sjk/19902.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定