目录
一、备份与还原
二、索引
三、视图
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命令备份数据库中的所有表
2、备份booksDB数据库中的books表
3、使用mysqldump备份booksDB和test数据库
4、使用mysqldump备份服务器中的所有数据库
5、使用mysql命令还原第二题导出的books表
6、进入数据库使用source命令还原第二题导出的book表
1、
mysqldump -uroot -p --databases booksDB >booksDB.sql
2、
3、
我没有test数据库就用lianxi1数据库代替了。
mysqldump -uroot -p --databases booksDB lianxi1>booksDB_lianxi1.sql
4、
mysqldump -uroot -p --all-databases>all_databases.sql
5、
mysqldump -uroot -p booksDB
6、
use booksDB;
source /root/books.sql;
=========================================================================
1、建立一个utf8编码的数据库test1
2、建立商品表goods和栏目表category
按如下表结构创建表:存储引擎engine myisam 字符集charset utf8mysql> desc goods;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| goods_id | int(11) | NO | PRI | NULL | auto_increment |
| goods_name | varchar(20) | NO | | | |
| cat_id | int(11) | NO | | 0 | |
| brand_id | int(11) | NO | | 0 | |
| goods_sn | char(12) | NO | | | |
| shop_price | float(6,2) | NO | | 0.00 | |
| goods_desc | text | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc category;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment |
| cate_name | varchar(20) | NO | | | |
| parent_id | int(11) | NO | | 0 | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段
4、在 goods_name 列上加唯一性索引(用alter table方式)
5、在 shop_price 列上加普通索引(用create index方式)
6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
1、
create database test1 character set utf8;
2、
create table goods(
goods_id int(11) not null primary key 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,2) not null default 0.0,
goods_desc text
)engine=myisam default charset=utf8;
create table category(
cat_id int(11) not null primary key auto_increment,
cate_name varchar(20) not null,
parent_id int(11) not null default 0
)engine=myisam default charset=utf8;
3、
alter table goods drop goods_id;
alter table goods drop goods_desc;
alter table goods add click_count int(10);
4、
alter table goods add constraint name_index unique(goods_name);
5、
create index price_index on goods(shop_price);
6、
create index click_index on goods(click_count);
drop index click_index on goods;
alter table goods drop index click_index;
=========================================================================
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno为主键,Cno为外键
1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
2、删除视图 stu_info。
create table student(
Sno int(10) primary key,
Sname varchar(10),
Ssex enum("男","女"),
Sage int(3),
Sdept int(5)
);
create table Course(
Cno int(10) primary key,
Cname varchar(10)
);
create table SC(
Sno int(10) primary key,
Cno int(10),
Score int(3),
foreign key (Cno) references Course(Cno)
);
1、
create view stu_info as
select student.Sname, student.Ssex, Course.Cname, SC.Score
from student
join SC on student.Sno=SC.Sno
join Course on SC.Cno=Course.Cno;
2、
drop view stu_info;