一、备份与还原
创建数据库、表以及插入数据
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
);
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);
CREATE TABLE authors
(
auth_id INT NOT NULL PRIMARY KEY,
auth_name VARCHAR(20),
auth_gender CHAR(1)
);
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');
CREATE TABLE authorbook
(
auth_id INT NOT NULL,
bk_id INT NOT NULL,
PRIMARY KEY (auth_id, bk_id),
FOREIGN KEY (auth_id) REFERENCES authors (auth_id),
FOREIGN KEY (bk_id) REFERENCES books (bk_id)
);
INSERT INTO authorbook
VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);
1、使用mysqldump命令备份数据库中的所有表
[root@localhost ~]# mysqldump -uroot -p12345 booksDB > /root/booskDB.sql
2、备份booksDB数据库中的books表
[root@localhost ~]#mysqldump -uroot -p12345 booksDB books> /root/books.sql
3、使用mysqldump备份booksDB和test数据库
[root@localhost ~]# mysqldump -uroot -p12345 --databases booksDB test > /root/booksDBtest.sql
4、使用mysqldump备份服务器中的所有数据库
[root@localhost ~]# mysqldump -uroot -p12345 -A > /root/all.sql
5、使用mysql命令还原第二题导出的book表
[root@localhost ~]# mysql -uroot -p12345 booksDB < /root/books.sql
6、进入数据库使用source命令还原第二题导出的book表
mysql> use booksDB
mysql> source /root/books.sql;
二、索引
1、建立一个utf8编码的数据库test1
create database test1 character set utf8;
2、建立商品表goods和栏目表category(存储引擎engine myisam 字符集charset utf8)
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,
`shop_price` float(6) NOT NULL DEFAULT 0.00,
`goods_desc` text NULL,
PRIMARY KEY (`goods_id`)
) ENGINE = MyISAM CHARACTER SET = utf8;
CREATE TABLE category (
`cat_id` int(11) NOT NULL AUTO_INCREMENT,
`cate_name` varchar(20) NOT NULL,
`parent_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`cat_id`)
) ENGINE = MyISAM CHARACTER SET = utf8;
3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段
alter table goods drop goods_desc;
alter table goods drop brand_id
alter table goods add click_count int;
4、在 goods_name 列上加唯一性索引(用alter table方式)
alter table goods add unique index goods (goods_name);
5、在 shop_price 列上加普通索引(用create index方式)
create index shop on goods(shop_price);
6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
create index click on goods(click_count);
drop index click on goods;
alter table goods drop index click;
三、视图
创建Student表
CREATE TABLE Student (
`Sno` int(11) NOT NULL COMMENT '学号',
`Sname` varchar(255) NULL COMMENT '姓名',
`Ssex` varchar(255) NULL COMMENT '性别',
`Sage` int(5) NULL COMMENT '年龄',
`Sdept` varchar(255) NULL COMMENT '所在系',
PRIMARY KEY (`Sno`)
);
创建Course表
CREATE TABLE Course (
`Cno` int(5) NOT NULL COMMENT '课程号',
`Cname` varchar(255) NULL COMMENT '课程名',
PRIMARY KEY (`Cno`)
);
创建SC表
CREATE TABLE SC (
`Sno` int(5) NOT NULL COMMENT '学号',
`Cno` int(5) NOT NULL COMMENT '课程号',
`Score` varchar(255) NULL COMMENT '成绩',
PRIMARY KEY (`Sno`, `Cno`)
);
1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
create view stu_info(姓名,性别,课程名,成绩) as select s.Sname,s.Ssex,c.Cname,scCname,sc.Score from Student s,Course c,SC sc where s.Sno=sc.Sno and sc.Cno=c.Cnno;
2、删除视图 stu_info。
drop view stu_info;