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;