MySQL备份/恢复、索引、视图简述与练习

MYSQL备份:

物理备份:

  1. 热备:
    数据库处于运行状态,依赖于数据库的日志文件,热备时可以进行读写。
  2. 温备:
    数据库会锁定表格,仅可以进行读。
  3. 冷备:
    是在关闭数据库的时候进行的,读写都不可以。

物理备份的优缺点: 物理备份的还原速度⾮常快。但是物理备份的最⼩粒度只能做到表。

逻辑备份:

  1. 完全备份:
    备份全部数据
  2. 增量备份:
    仅备份上次完全备份或增量备份以后变化的数据
  3. 差异备份:
    仅备份上次完全备份以来变化的数据

逻辑备份的优缺点:
逻辑备份有⾮常强的兼容性,⽽物理备份则对版本要求⾮常⾼ 逻辑备份也对保持数据的安全性有保证。
逻辑备份要对RDBMS产⽣额外的压⼒,且逻辑备份的结果可能要⽐源⽂件更⼤,逻辑备份还可能会丢失浮点数的精度信息。

索引:

原理:

把创建索引的列的内容进行排序并生成倒序表,然后在倒序表内容后拼接上数据地址链,在查询时先拿出倒序表内容再拿到数据链地址,最后拿到具体数据。

优缺点:

优点:索引的优点是可以提⾼检索数据的速度。
缺点:创建和维护索引需要耗费时间,耗费时间的数量随着数据量的增加⽽增加,且索引需要占用物理空间。

视图:

什么是视图:

视图通过以定制的⽅式显⽰来⾃⼀个或多个表的数据 视图是⼀种数据库对象,⽤⼾可以像查询普通表⼀样查询视图 视图内其实没有存储任何数据 ,它只是对表的⼀个查询 视图的定义保存在数据字典内,创建视图所基于对表称为“基表”。

视图是一个虚拟表,其内容由查询定义。数据库中只存放了视图的定义,而并没有存放视图中的数据,这些数据存放在原来的表中。

作用:

使操作简单化
增加数据的安全性
视图只能看到查询语句定义的相关内容,而其他的表结构及内容是看不到的。

优点:

提供了灵活⼀致级别安全性。
隐藏了数据的复杂性
简化了⽤⼾的SQL指令
通过重命名列,从另⼀个⻆度提供数据

备份与恢复练习题:

创库,建表:

CREATE DATABASE booksDB;
use booksDB;

CREATE TABLE books
(
bk_id  INT NOT NULL PRIMARY KEY,
bk_title VARCHAR(50) NOT NULL,
copyright YEAR NOT NULL
);

CREATE TABLE authors
(
auth_id     INT NOT NULL PRIMARY KEY,
auth_name  VARCHAR(20),
auth_gender CHAR(1)
);

CREATE TABLE authorbook
(
auth_id  INT NOT NULL,
bk_id   INT NOT NULL
);

插入数据:

INSERT INTO books
	VALUES (11078, 'Learning MySQL', 2010),
	(11033, 'Study Html', 2011),
	(11035, 'How to use php', 2003),
	(11072, 'Teach youself javascript', 2005),
	(11028, 'Learing C++', 2005),
	(11069, 'MySQL professional', 2009),
	(11026, 'Guide to MySQL 5.5', 2008),
	(11041, 'Inside VC++', 2011);
	
INSERT INTO authors  
	VALUES (1001, 'WriterX' ,'f'),
	(1002, 'WriterA' ,'f'),
	(1003, 'WriterB' ,'m'),
	(1004, 'WriterC' ,'f'),
	(1011, 'WriterD' ,'f'),
	(1012, 'WriterE' ,'m'),
	(1013, 'WriterF' ,'m'),
	(1014, 'WriterG' ,'f'),
	(1015, 'WriterH' ,'f');
	
INSERT INTO authorbook
	VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
	(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);

题目:

1、使用mysqldump命令备份数据库中的所有表

mysqldump  -B -uroot -proot booksDB > /root/mysql/all_tables.sql

2、备份booksDB数据库中的books表

mysqldump  -uroot -proot booksDB books > /root/mysql/table_books.sql

3、使用mysqldump备份booksDB和test数据库

mysqldump -uroot -p --databases booksDB test >/root/mysql/books_test_DB.sql
Enter password:

4、使用mysql命令还原第二题导出的book表

[root@node1 ~]# mysql -u root -p booksDB</root/mysql/table_books.sql
Enter password: 
mysql> select * from books;
+-------+--------------------------+-----------+
| bk_id | bk_title                 | copyright |
+-------+--------------------------+-----------+
| 11026 | Guide to MySQL 5.5       |      2008 |
| 11028 | Learing C++              |      2005 |
| 11033 | Study Html               |      2011 |
| 11035 | How to use php           |      2003 |
| 11041 | Inside VC++              |      2011 |
| 11069 | MySQL professional       |      2009 |
| 11072 | Teach youself javascript |      2005 |
| 11078 | Learning MySQL           |      2010 |
+-------+--------------------------+-----------+

5、进入数据库使用source命令还原第二题导出的book表

mysql> drop table books;
mysql> source /root/mysql/table_books.sql
mysql> select * from books;
+-------+--------------------------+-----------+
| bk_id | bk_title                 | copyright |
+-------+--------------------------+-----------+
| 11026 | Guide to MySQL 5.5       |      2008 |
| 11028 | Learing C++              |      2005 |
| 11033 | Study Html               |      2011 |
| 11035 | How to use php           |      2003 |
| 11041 | Inside VC++              |      2011 |
| 11069 | MySQL professional       |      2009 |
| 11072 | Teach youself javascript |      2005 |
| 11078 | Learning MySQL           |      2010 |
+-------+--------------------------+-----------+

索引练习题:

创库,建表:

mysql> create table goods(
    -> goods_id int primary key auto_increment,
    -> goods_name varchar(20) not null,
    -> cat_id int not null default 0,
    -> brand_id int not null default 0,
    -> goods_sn char(12) not null,
    -> shop_price float(6,2) not null default 0.00,
    -> goods_desc text 
    -> );
 
mysql> create table category( cat_id int primary key auto_increment, cate_name varchar(20), parent_id int default 0 );

题目:

1、修改存储引擎:

mysql> alter table goods engine=myisam;
mysql> alter table category engine=myisam;

2、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段

mysql> alter table goods drop column goods_desc;
mysql> alter table goods drop column goods_sn;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| goods_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(20) | NO   | UNI | NULL    |                |
| cat_id      | int(11)     | NO   |     | 0       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
+-------------+-------------+------+-----+---------+----------------+


mysql> alter table goods add click_count int;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| goods_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(20) | NO   |     | NULL    |                |
| cat_id      | int(11)     | NO   |     | 0       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| goods_sn    | char(12)    | NO   |     | NULL    |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
| click_count | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+

3、在 goods_name 列上加唯一性索引(用alter table方式):

mysql> alter table goods add unique index goods_name_index (goods_name(20));
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

4、在 shop_price 列上加普通索引(用create index方式)

mysql> create index g_price_index on goods(shop_price);
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

5、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)

mysql> alter table goods add index c_l_index (click_count);
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`),
  KEY `c_l_index` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

mysql> alter table goods drop index c_l_index;
mysql> drop index c_l_index on goods;

mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

视图练习题:

创库,建表:

#Student
mysql> create table Student(
    -> Sno int primary key auto_increment,
    -> Sname varchar(20) not null,
    -> Ssex char(1),
    -> Sage int,
    -> Sdept varchar(20)
    -> );
#Course
mysql> create table Course(
    -> Cno int primary key auto_increment,
    -> Cname varchar(50)
    -> );
#SC
mysql> create table SC( Sno int , Cno int primary key auto_increment ,Score float(6,2));

插入数据:

#Student
mysql> insert into Student values(
    -> 1,'bob','m',20,'人工智能');
mysql> insert into Student values( 2,'harry','m',22,'电气信息');
mysql> insert into Student values( 3,'natasha','f',23,'电气信息');
mysql> insert into Student values( 4,'sarah','m',21,'人文');
mysql> insert into Student values( 5,'manalo','f',21,'艺术');

+-----+---------+------+------+--------------+
| Sno | Sname   | Ssex | Sage | Sdept        |
+-----+---------+------+------+--------------+
|   1 | bob     | m    |   20 | 人工智能     |
|   2 | harry   | m    |   22 | 电气信息     |
|   3 | natasha | f    |   23 | 电气信息     |
|   4 | sarah   | m    |   21 | 人文         |
|   5 | manalo  | f    |   21 | 艺术         |
+-----+---------+------+------+--------------+
#Course
mysql> insert into Course values( 10,'JAVA');
mysql> insert into Course values( 11,'英语');
mysql> insert into Course values( 12,'近代史');
mysql> insert into Course values( 13,'C++');
mysql> insert into Course values( 14,'毛概');
+-----+-----------+
| Cno | Cname     |
+-----+-----------+
|  10 | JAVA      |
|  11 | 英语      |
|  12 | 近代史    |
|  13 | C++       |
|  14 | 毛概      |
+-----+-----------+
#SC
mysql> insert into SC values(1,10,78.5);
mysql> insert into SC values(2,11,82.5);
mysql> insert into SC values(3,14,90.0);
mysql> insert into SC values(4,13,98.5);
mysql> insert into SC values(5,12,75.5);
+-----+------+-------+
| Sno | Cno  | Score |
+-----+------+-------+
|   1 |   10 | 78.50 |
|   2 |   11 | 82.50 |
|   3 |   14 | 90.00 |
|   4 |   13 | 98.50 |
|   5 |   12 | 75.50 |
+-----+------+-------+

题目:

创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩:

create view stu_co_info as select s.Sname,s.Ssex,co.Cname,c.Score from Student s , SC c ,Course co where s.Sno=c.Sno and c.Cno=co.Cno;
#
mysql> select * from stu_co_info;
+---------+------+-----------+-------+
| Sname   | Ssex | Cname     | Score |
+---------+------+-----------+-------+
| bob     | m    | JAVA      | 78.50 |
| harry   | m    | 英语      | 82.50 |
| manalo  | f    | 近代史    | 75.50 |
| sarah   | m    | C++       | 98.50 |
| natasha | f    | 毛概      | 90.00 |
+---------+------+-----------+-------+

删除视图:

mysql> drop view stu_co_info;
mysql> select * from stu_co_info;
ERROR 1146 (42S02): Table 'test.stu_co_info' doesn't exist
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值