一、备份与恢复
创库,建表:
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 books
VALUES (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 authorbook
VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);
1、使用mysqldump命令备份数据库中的所有表
mysqldump -u root -p123456 --all-databases > alldb_backup.sql
2、备份booksDB数据库中的books表
mysqldump -u [用户名] -p[密码] booksDB books > booksDB_books_backup.sql
3、使用mysqldump备份booksDB和test数据库(test数据库自行准备)
CREATE DATABASE test;
mysqldump -u [用户名] -p[密码] --databases booksDB test > booksDB_test_backup.sql
4、使用mysql命令还原第二题导出的book表
mysql -u [用户名] -p[密码] booksDB < booksDB_books_backup.sql
5、进入数据库使用source命令还原第二题导出的book表
mysql -u [用户名] -p[密码]
source booksDB_books_backup.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 字段
ALTER TABLE goods
DROP COLUMN goods_desc,
DROP COLUMN goods_sn,
ADD COLUMN click_count INT NOT NULL DEFAULT 0;
2、在 goods_name 列上加唯一性索引(用alter table方式)
ALTER TABLE goods
ADD UNIQUE INDEX (goods_name);
3、在 shop_price 列上加普通索引(用create index方式)
CREATE INDEX idx_shop_price ON goods(shop_price);
4、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
CREATE INDEX idx_click_count ON goods(click_count);
DROP INDEX idx_click_count ON goods;
ALTER TABLE goods
DROP INDEX idx_click_count;