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)

相关推荐
老邓计算机毕设20 分钟前
SSM智慧社区家政服务系统80q7o(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架
松涛和鸣1 小时前
72、IMX6ULL驱动实战:设备树(DTS/DTB)+ GPIO子系统+Platform总线
linux·服务器·arm开发·数据库·单片机
likangbinlxa2 小时前
【Oracle11g SQL详解】UPDATE 和 DELETE 操作的正确使用
数据库·sql
r i c k2 小时前
数据库系统学习笔记
数据库·笔记·学习
野犬寒鸦2 小时前
从零起步学习JVM || 第一章:类加载器与双亲委派机制模型详解
java·jvm·数据库·后端·学习
IvorySQL3 小时前
PostgreSQL 分区表的 ALTER TABLE 语句执行机制解析
数据库·postgresql·开源
·云扬·3 小时前
MySQL 8.0 Redo Log 归档与禁用实战指南
android·数据库·mysql
IT邦德3 小时前
Oracle 26ai DataGuard 搭建(RAC到单机)
数据库·oracle
惊讶的猫4 小时前
redis分片集群
数据库·redis·缓存·分片集群·海量数据存储·高并发写
不爱缺氧i4 小时前
完全卸载MariaDB
数据库·mariadb