MySQL--第6次作业

一、备份与还原

/*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)

相关推荐
张哈大1 小时前
【 Redis | 实战篇 秒杀实现 】
数据库·redis·缓存
weixin_472339461 小时前
Postgresql与openguass对比
数据库·postgresql
程序员曼布2 小时前
主从架构:技术原理与实现
redis·mysql·架构
惊起白鸽4506 小时前
MySQL全量,增量备份与恢复
数据库·mysql
暮雨疏桐7 小时前
MySQL SQL Mode及其说明
数据库·sql·mysql·sql mode
Tangcan-7 小时前
【MySQL】数据库基础
数据库·mysql
蔡蓝8 小时前
Mysql的索引,慢查询和数据库表的设计以及乐观锁和悲观锁
数据库·mysql
jstart千语8 小时前
【Redis】分布式锁的实现
数据库·redis·分布式
一把年纪学编程9 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
极小狐9 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven