MySQL的数据备份与还原--练习题

MySQLdump

MySQLdump是MySQL提供的一个非常有用的数据库备份工具。MySQLdump命令执行时,可以将数据库备份成一个文本文件,该文件中实际上包含了多个CREATE 和 INSERT语句,使用这些语句可以重新创建表和插入数据。

 看题:

第一题:

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
    );


    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);

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


    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');

    CREATE TABLE authorbook
    (
      auth_id  INT NOT NULL,
      bk_id   INT NOT NULL,
      PRIMARY KEY (auth_id, bk_id),
      FOREIGN KEY (auth_id) REFERENCES authors (auth_id),
      FOREIGN KEY (bk_id) REFERENCES books (bk_id)
    );

    INSERT INTO authorbook
    VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
    (1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);

首先创建一个文件夹,如我创建的是在  /mysql/dump文件夹下面:备份的内容就往文件夹里面怼,利用输出重定向。

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

[root@redhat dump]# mysqldump -uroot -p123 -a booksDB > /mysql/dump/booksDB.sql
  

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

[root@redhat dump]# mysqldump -uroot -p123 -a booksDB books > /mysql/dump/books_tab_books.sql
  

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

[root@redhat dump]# mysqldump -uroot -p123 --databases booksDB test1 > /mysql/dump/booksDB_and_test1DB
.sql
 

   4、使用mysqldump备份服务器中的所有数据库

[root@redhat dump]# mysqldump -uroot -p --all-databases  > /mysql/dump/all_DB.sql


    5、使用mysql命令还原第二题导出的book表(利用输入重定向)

[root@redhat dump]# mysqldump -uroot -p booksDB  < /mysql/dump/books_tab_books.sql
Enter password:
-- MySQL dump 10.13  Distrib 5.7.41, for Linux (x86_64)
--
-- Host: localhost    Database: booksDB
-- ------------------------------------------------------
-- Server version       5.7.41

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

[root@redhat dump]# mysql -u root -p -e 'source /mysql/dump/books_tab_books.sql'

第二: 索引

1.索引的优势

  1. 提高查询率(降低IO使用率)
  2. 降低cpu使用率

2.索引的弊端

        1. 索引本身很大,可以存放在内存或硬盘上,通常存储在硬盘上

        2.索引不是所有情况都使用

        3.索引会降低增删改的效率 

3.索引的分类

        1.单值索引

        2.唯一索引

        3.联合索引

        4.主键索引

唯一索引和主键索引的区别:主键索引不能为NULL

题目:

1、建立一个utf8编码的数据库test1
    2、建立商品表goods和栏目表category
    按如下表结构创建表:存储引擎engine myisam 字符集charset utf8

mysql> create table goods(
    -> good_id int(11)  primary key,
    -> goods_name varchar(20) not null,
    -> cat_id int(11) not null default 0,
    -> brand_id int(11) not null default 0,
    -> goods_sn char(12) not null,
    -> shop_price float(6,2) not null default 0.00,
    -> goods_desc text default NULL
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc  goods;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| good_id    | int(11)     | NO   | PRI | NULL    |       |
| 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    |       |
| goods_desc | text        | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

 修改索引存储引擎

mysql> alter table goods engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

删除:

mysql> alter table goods drop goods_desc;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

添加:
mysql> alter table goods add click_count int;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看

mysql> desc goods;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| good_id     | int(11)     | NO   | PRI | NULL    |       |
| 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    |       |
+-------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

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

mysql> alter table goods add unique index (goods_name(20));
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

ERROR:
No query specified


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

mysql> create index g_price_index on goods(shop_price);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

ERROR:
No query specified


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

mysql> alter table goods add index c_l_index (click_count);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table goods drop index c_l_index;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index c_l_index on goods;

第三题:视图

使用视图的原因


1)简单:使用视图的用户完全不需要关心后面对应的表的结构、关联条件和筛选条件,对用户来说已经是过滤好的复合条件的结果集。

2)安全:使用视图的用户只能访问他们被允许查询的结果集,对表的权限管理并不能限制到某个行某个列,但是通过视图就可以简单的实现。

3)数据独立:一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响,源表增加列对视图没有影响;源表修改列名,则可以通过修改视图来解决,不会造成对访问者的影响。

总而言之,使用视图的大部分情况是为了保障数据安全性,提高查询效率。

基本格式:

CREATE VIEW <视图名> AS

视图练习题:

创建表:

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)
    -> );
mysql> create table SC( Sno int , Cno int primary key auto_increment ,Score float(6,2));
 

 插入数据
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 | 艺术         |
+-----+---------+------+------+--------------+

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 | 毛概      |
+-----+-----------+

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

你可能感兴趣的:(MySQL,mysql,数据库)