MYSQL–第八次作业
一、备份与恢复
环境搭建:
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
);CREATE TABLE authors
(
auth_id INT NOT NULL PRIMARY KEY,
auth_name VARCHAR(20),
auth_gender CHAR(1)
);CREATE TABLE authorbook
(
auth_id INT NOT NULL,
bk_id INT NOT NULL
);-- 插入数据:
INSERT INTO booksVALUES (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);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');INSERT INTO authorbookVALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);
作业要求及解答:
1、使用mysqldump命令备份数据库中的所有表
C:\Windows\System32>mysqldump -uroot -p123456 booksDB authorbook authors books > E:\mysql-beifen\booksDB_all_tables.sql
2、备份booksDB数据库中的books表
C:\Windows\System32>mysqldump -uroot -p booksDB books > E:\mysql-beifen\booksDB_books_table.sql
-- 在这里-p后面没有加密码,需要在回车后再输入密码,这种方式更安全
3、使用mysqldump备份booksDB和test数据库(test数据库自行准备)
-- test数据库环境搭建:
-- 创建test数据库:
create database test;
-- 调用数据库:
use test;
-- 创建表并添加表内容:
create table dept(dept_id int primary key auto_increment comment '部门编号',dept_name char(20) comment '部门名称'
);insert into dept(dept_name) values('销售部'),('财务部'),('生产部'),('人事部');create table emp(emp_id int primary key auto_increment comment '员工号',emp_name char(20) not null default '' comment '员工姓名',gender char(2) not null default '男' comment '性别',birth datetime not null default '1990-1-1' comment '出生日期',salary decimal(10,2) not null default 0 comment '工资',address varchar(200) not null default '' comment '通讯地址',dept_id int comment '部门编号'
);create index idx_name on emp(emp_name);
create index idx_birth on emp(birth);
create index idx_deptid_name on emp(dept_id,emp_name);insert into emp(emp_name,gender,birth,salary,address,dept_id)
values('张晓红','女','1980-1-23',5800,'河南省郑州市中原路10号',1),
('张静静','女','1987-10-3',5400,'河南省新乡市平原路38号',1),
('王云飞','男','1992-11-15',5600,'河南省新乡市人民路28号',1),
('王鹏飞','男','1987-10-1',6800,'河南省新乡市东明大道12号',1),
('王大鹏','男','1989-2-11',5900,'河南省郑州市东风路15号',1),
('王萌萌','女','1986-12-30',5000,'河南省开封市五一路14号',2),
('王大光','男','1988-11-8',6200,'河南省开封市八一路124号',2),
('王小明','男','1998-1-3',4800,'河南省驻马店市雪松路128号',2),
('王娜娜','女','1994-3-5',5200,'河南省驻马店市车站路2号',2),
('刘云飞','男','1992-8-13',6800,'河南省南阳市民生路255号',3),
('张陆军','男','1991-9-6',6200,'河南省南阳市张仲景路14号',3);
备份数据库:
C:\Windows\System32>mysqldump -uroot -p -B booksDB test > E:\mysql-beifen\booksDB_and_test.sql
4、使用mysql命令还原第二题导出的books表
在使用导出命令时,为验证命令的正确性,可以先在Navicat中将books表先进行删除,再用命令对books表进行还原:
C:\Windows\System32>mysql -uroot -p booksDB < E:\mysql-beifen\booksDB_books_table.sql
5、进入数据库使用source命令还原第二题导出的books表
mysql> use booksdb;
mysql> source E:\mysql-beifen\booksDB_books_table.sql
二、索引
环境搭建
create table goods(goods_id int primary key auto_increment,goods_name varchar(20) not null,cat_id int not null default 0,brand_id int not null default 0,goods_sn char(12) not null,shop_price float(6,2) not null default 0.00,goods_desc text );create table category( cat_id int primary key auto_increment, cate_name varchar(20), parent_id int default 0 );
作业要求及解答:
1、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段:
mysql> alter table goods drop column goods_desc;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> alter table goods drop column goods_sn;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> alter table goods add column click_count int default 0;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
2、在 goods_name 列上加唯一性索引(用alter table方式):
mysql> alter table goods add unique index index_goods_name(goods_name);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0-- 查看自己创建的索引:
mysql> show index from goods \G;
3、在 shop_price 列上加普通索引(用create index方式)
mysql> create index index_shop_price on goods(shop_price);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show index from goods \G;
4、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
添加索引:
mysql> create index index_click_count on goods(click_count);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show index from goods \G;
删除索引:
-- 1、使用drop index删除索引
mysql> drop index index_click_count on goods;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
-- 2、使用alter table删除索引
mysql> alter table goods drop index index_click_count;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show index from goods \G;