MySQL--第6次作业

本文详细介绍了如何使用mysqldump命令进行数据库的备份与还原,包括备份单个表、多个数据库以及所有数据库。同时,展示了创建和删除视图的操作,以及在goods表上添加和删除索引的过程,包括唯一性和普通索引的创建与管理。
摘要由CSDN通过智能技术生成


一、备份与还原

/*1、使用mysqldump命令备份数据库中的所有表*/
[root@localhost ~]# mkdir /backup/db
[root@localhost ~]# mysqldump -uroot -p'#BenJM123' -A booksDB > /backup/db/booksDB.sql
 
/*2、备份booksDB数据库中的books表*/
[root@localhost ~]# mysqldump -uroot -p'#BenJM123' booksDB books > /backup/db/books.sql
 
/*3、使用mysqldump备份booksDB和test数据库*/
[root@localhost ~]# mysqldump -uroot -p'#BenJM123' --databases booksDB test > /backup/db/booksDB_test.sql
 
/*4、使用mysqldump备份服务器中的所有数据库*/
[root@localhost ~]# mysqldump -uroot -p'#BenJM123' -A > /backup/db/all_db.sql
 
/*5、使用mysql命令还原第二题导出的book表*/
--由于有外键约束,所以得先删除
mysql> alter table authorbook drop foreign key authorbook_ibfk_2;
--删除表book
[root@localhost ~]# mysql -uroot -p'#BenJM123' -e 'use bookDBs;drop table books'
--恢复表book
[root@localhost ~]# mysql -uroot -p'#BenJM123' -D booksDB -e 'source /backup/db/books.sql'
 
/*6、进入数据库使用source命令还原第二题导出的book表*/
--删除表book
mysql> drop table books;
--恢复表book
mysql> source /backup/db/books.sql

二、视图

--1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
mysql> create view View_Stu as select Sname,Ssex,Cname,Score
    -> from Student,Course,SC
    -> where Student.Sno=SC.Sno and Course.Cno=SC.Cno;
Query OK, 0 rows affected (0.00 sec)
 
mysql> select * from View_Stu;
+--------+------+--------------------------+-------+
| Sname  | Ssex | Cname                    | Score |
+--------+------+--------------------------+-------+
| 张三   | 男   | 如何获取富婆欢心         |   100 |
| 李四   | 女   | MySQL从入门到精通        |    99 |
| 王五   | 男   | 高等数学                 |    75 |
| 赵六   | 男   | 大学英语                 |    87 |
| 陈七   | 女   | 如何做白日梦             |    69 |
+--------+------+--------------------------+-------+
5 rows in set (0.00 sec)
 
 
--2、删除视图 stu_info。
mysql> show create view View_Stu \G
*************************** 1. row ***************************
                View: View_Stu
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `View_Stu` AS select `Student`.`Sname` AS `Sname`,`Student`.`Ssex` AS `Ssex`,`Course`.`Cname` AS `Cname`,`SC`.`Score` AS `Score` from ((`Student` join `Course`) join `SC`) where ((`Student`.`Sno` = `SC`.`Sno`) and (`Course`.`Cno` = `SC`.`Cno`))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
 
mysql> drop view View_Stu;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show create view View_Stu \G
ERROR 1146 (42S02): Table 'test7_13.View_Stu' doesn't exist 

三.索引

/* 1、建立一个utf8编码的数据库test1 */
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> use test1;
Database changed
 
 
/* 2、建立商品表goods和栏目表category(要求:按如下表结构创建表,并且存储引擎engine myisam 字符集charset utf8)*/
--创建表goods
mysql> create table goods(
    -> goods_id int(11) primary key auto_increment,
    -> 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 default '',
    -> shop_price float(6,2) not null default 0.00,
    -> goods_desc text
    -> ) engine=MyISAM charset=utf8;
Query OK, 0 rows affected (0.00 sec)
 
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       |                |
| 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)
 
--创建表category
mysql> create table category(
    -> cat_id int(11) primary key auto_increment,
    -> cate_name varchar(20) not null default '',
    -> parent_id int(11) not null default 0
    -> ) engine=MyISAM charset=utf8;
Query OK, 0 rows affected (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;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql> alter table goods add click_count int;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
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       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| goods_sn    | char(12)    | NO   |     |         |                |
| 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 UniqIdx(goods_name);
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` (
  `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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
 
 
/* 5、在 shop_price 列上加普通索引(用create index方式) */
mysql> create index NormalIdx 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` (
  `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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
 
 
/* 6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)*/
 
--方法一:使用drop index删除
            /* 先增加索引 */
mysql> create index NormalIdx2 on goods(click_count);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`),
  KEY `NormalIdx2` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
 
          /* drop index删除 */
mysql> drop index NormalIdx2 on goods;
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` (
  `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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
 
--方法二:使用alter table删除
            /* 先增加索引 */
mysql> create index NormalIdx2 on goods(click_count);
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` (
  `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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`),
  KEY `NormalIdx2` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
 
           /* alter table删除 */
mysql> alter table goods drop index NormalIdx2;
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` (
  `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',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值