Database backup and restore, views and indexes

 1. Database backup and restore

1. Create relevant databases and data tables

/******************************Sample table********************** ********/
    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);

    /******************************Sample table************************ **********/

 2. Requirements and answers

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

[root@localhost ~]# mysqldump -u root -p booksDB > /backup/booksdb_v1.sql;
   
2、备份booksDB数据库中的books表
  
[root@localhost ~]# mysqldump -u root -p'RedHat@123' booksDB books > /backup/booksdb_v2.sql;

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

创建数据库text
mysql> create database test;
Query OK, 1 row affected (0.00 sec)

再进行备份
[root@localhost ~]# mysqldump -u root -p'RedHat@123' --databases booksDB test > /backup/booksb_v3.sql;

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

[root@localhost ~]# mysqldump -u root -p'RedHat@123' --all-databases > /backup/booksdb_v4.sql;

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

[root@localhost ~]# mysql -uroot -p'RedHat@123' booksDB < /backup/booksdb_v2.sql;

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

进入数据库

mysql> source /backup/booksdb_v2.sql;

2. Index

1. Create a utf8-encoded database test1

 2. Create the product table goods and column table category.
    Create the table according to the following table structure: storage engine engine myisam character set charset utf8

        mysql> desc goods;
        +------------+-------------+------+-----+---------+----------------+
        | Field      | Type        | Null | Key | Default | Extra          |
        +------------+-------------+------+-----+---------+----------------+
        | goods_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
        | goods_name | varchar(20) | NO   |     |         |                |
        | cat_id     | int(11)     | NO   |     | 0       |                |
        | brand_id   | int(11)     | NO   |     | 0       |                |
        | goods_sn   | char(12)    | NO   |     |         |                |
        | shop_price | float(6,2)  | NO   |     | 0.00    |                |
        | goods_desc | text        | YES  |     | NULL    |                |
        +------------+-------------+------+-----+---------+----------------+
        7 rows in set (0.00 sec)

        
        mysql> desc category;
        +-----------+-------------+------+-----+---------+----------------+
        | Field     | Type        | Null | Key | Default | Extra          |
        +-----------+-------------+------+-----+---------+----------------+
        | cat_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
        | cate_name | varchar(20) | NO   |     |         |                |
        | parent_id | int(11)     | NO   |     | 0       |                |
        +-----------+-------------+------+-----+---------+----------------+
        3 rows in set (0.00 sec)

3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段 
mysql> alter table goods drop goods_desc;
mysql> alter table goods drop brand_id;
mysql> alter table goods add click_count int;

mysql> desc goods;
+-------------+-------------+------+-----+---------+----------------+
| 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       |                |
| goods_sn    | char(12)    | NO   |     | NULL    |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
| click_count | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

4、在 goods_name 列上加唯一性索引(用alter table方式) 
mysql> alter table goods add unique index_name(goods_name);

5、在 shop_price 列上加普通索引(用create index方式)
mysql> create index index_price on goods(shop_price);

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

Three View

Student table: Student (Sno, Sname, Ssex, Sage, Sdept)
student number, name, gender, age, department Sno as the main key
Course table: Course (Cno, Cname,)
course number, course name Cno as the main key
Student course selection table :SC (Sno, Cno, Score)

Student number, course number, grade Sno, Cno as primary key

1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
mysql> create view stu_info(姓名,性别,课程名,成绩) 
        as select A.Sname,A.Ssex,B.Cname,B.Score from student A 
        left join (
        select C.Cno,Cname,Sno,Score from Course C 
        left join sc D on C.cno = D.Cno) B on A.Sno = B.Sno;

2、删除视图 stu_info。
mysql> drop view stu_info;

Guess you like

Origin blog.csdn.net/shenql_/article/details/131679259