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)

相关推荐
凡人的AI工具箱13 分钟前
每天40分玩转Django:Django类视图
数据库·人工智能·后端·python·django·sqlite
知识的宝藏17 分钟前
Django models中的增删改查与MySQL SQL的对应关系
sql·mysql·django·django models
路在脚下@17 分钟前
MySQL的索引失效的原因有那些
数据库·mysql
凡人的AI工具箱23 分钟前
每天40分玩转Django:实操图片分享社区
数据库·人工智能·后端·python·django
CT随28 分钟前
MongoDB
数据库·mongodb
新子-存在了44 分钟前
linux中 mysql备份
linux·运维·mysql
Fool丶玄浅1 小时前
【数据库系统概论】—— 关系数据库
数据库·数据库系统
DashVector2 小时前
如何通过HTTP API检索Doc
数据库·人工智能·http·阿里云·数据库开发·向量检索
vvw&2 小时前
如何在 Ubuntu 22.04 上安装 phpMyAdmin
linux·运维·服务器·mysql·ubuntu·php·phpmyadmin
SEO-狼术2 小时前
Enhance Security in Software Crack
数据库