// 5、使用mysql命令还原第二题导出的book表
// 要先在MySQL数据库中删除表所含有的外键 然后删除表才能进行还原
mysql> use booksDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show create table authorbook \G;
*************************** 1. row ***************************
Table: authorbook
Create Table: CREATE TABLE `authorbook` (
`auth_id` int(11) NOT NULL,
`bk_id` int(11) NOT NULL,
PRIMARY KEY (`auth_id`,`bk_id`),
KEY `bk_id` (`bk_id`),
CONSTRAINT `authorbook_ibfk_1` FOREIGN KEY (`auth_id`) REFERENCES `authors` (`auth_id`),
CONSTRAINT `authorbook_ibfk_2` FOREIGN KEY (`bk_id`) REFERENCES `books` (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> alter table authorbook drop foreign key authorbook_ibfk_2;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> drop table books;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_booksDB |
+-------------------+
| authorbook |
| authors |
+-------------------+
2 rows in set (0.00 sec)
mysql> exit
Bye
[root@localhostjx ~]# mysql -uroot -p -D booksDB < /backup/db/booksdb_book.sql
Enter password:
[root@localhostjx ~]# ll /backup/db/booksdb_book.sql
-rw-r--r--. 1 root root 3912 7月 20 20:12 /backup/db/booksdb_book.sql
// 6、进入数据库使用source命令还原第二题导出的book表
mysql> use booksDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_booksDB |
+-------------------+
| authorbook |
| authors |
| books |
+-------------------+
3 rows in set (0.00 sec)
二、索引
1、建立一个utf8编码的数据库test1
2、建立商品表goods和栏目表category
按如下表结构创建表:存储引擎engine myisam 字符集charset utf8
mysql> 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删除)
//按要求创建数据表
mysql> create table goods(
-> goods_id int(11) primary key auto_increment,
-> goods_name varchar(20) not null,
-> cat_id int(11) not null,
-> brand_id int(11) not null,
-> goods_sn char(12) not null,
-> shop_price float(6,2) not null,
-> goods_desc text)
-> ENGINE = myisam CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Query OK, 0 rows affected (0.00 sec)
mysql> desc goods;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| goods_id | int(11) | NO | PRI | NULL | auto_increment |
| goods_name | varchar(20) | NO | | NULL | |
| cat_id | int(11) | NO | | NULL | |
| brand_id | int(11) | NO | | NULL | |
| goods_sn | char(12) | NO | | NULL | |
| shop_price | float(6,2) | NO | | NULL | |
| goods_desc | text | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> create table category(
-> cat_id int(11) primary key auto_increment,
-> cate_name varchar(20) not null,
-> parent_id int(11) not null)
-> ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Query OK, 0 rows affected (0.01 sec)
mysql> desc category;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment |
| cate_name | varchar(20) | NO | | NULL | |
| parent_id | int(11) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
// 3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段
mysql> alter table goods drop goods_desc;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table goods drop goods_sn;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc goods;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| goods_id | int(11) | NO | PRI | NULL | auto_increment |
| goods_name | varchar(20) | NO | | NULL | |
| cat_id | int(11) | NO | | NULL | |
| brand_id | int(11) | NO | | NULL | |
| shop_price | float(6,2) | NO | | NULL | |
| click_count | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
// 4、在 goods_name 列上加唯一性索引(用alter table方式)
mysql> alter table goods add unique index(goods_name);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table goods \G;
*************************** 1. row ***************************
Table: goods
Create Table: CREATE TABLE `goods` (
`goods_id` int(11) NOT NULL AUTO_INCREMENT,
`goods_name` varchar(20) NOT NULL,
`cat_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`shop_price` float(6,2) NOT NULL,
`click_count` int(11) DEFAULT NULL,
PRIMARY KEY (`goods_id`),
UNIQUE KEY `goods_name` (`goods_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
1 row in set (0.00 sec)
//5、在 shop_price 列上加普通索引(用create index方式)
mysql> create index shop_price on goods(shop_price);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table goods \G;
*************************** 1. row ***************************
Table: goods
Create Table: CREATE TABLE `goods` (
`goods_id` int(11) NOT NULL AUTO_INCREMENT,
`goods_name` varchar(20) NOT NULL,
`cat_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`shop_price` float(6,2) NOT NULL,
`click_count` int(11) DEFAULT NULL,
PRIMARY KEY (`goods_id`),
UNIQUE KEY `goods_name` (`goods_name`),
KEY `shop_price` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
1 row in set (0.00 sec)
// 6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除
mysql> create index count on goods(click_count);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> drop index count on goods;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table goods add index c_count(click_count);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table goods drop index c_count;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0