Database Backup and Restoration
Create Database and Tables
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 Data
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);
Backup Operations
-
Back up all tables in the
booksDBdatabase usingmysqldump. -
Back up only the
bookstable from thebooksDBdaatbase. -
Back up both
booksDBandtestdatabases. -
Restore the
bookstable using themysqlcommand. -
Restore the
bookstable using thesourcecommend.
mysqldump -u root -p booksdb > D:\booksdb.sql
mysqldump -u root -p --databases booksDB test > D:\booksdb_test.sql
mysqldump -u root -p booksDB books > \booksdb-books.sql
mysql -u root -p booksdb < \booksdb-books.sql
source \booksdb-books.sql
Index Management
Create Tables
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
);
Indexing Requirements
-
Remove the
goods_descandgoods_sncolumns from thegoodstable, and add a new columnclick_count. -
Add a unique index on the
goods_namecolumn usingALTER TABLE. -
Add a regular index on the
shop_pricecolumn usingCREATE INDEX. -
Add a regular index on
click_count, then remove it using bothDROP INDEXandALTER TABLE.
ALTER TABLE goods DROP COLUMN goods_desc;
ALTER TABLE goods ADD COLUMN click_count INT;
ALTER TABLE goods ADD UNIQUE INDEX UniqIdx(goods_name);
CREATE INDEX NormalIdx ON goods(shop_price);
CREATE INDEX NormalIdx2 ON goods(click_count);
DROP INDEX NormalIdx2 ON goods;
ALTER TABLE goods DROP INDEX NormalIdx2;