MySQL数据库——索引练习

一、练习题目

1、建立一个utf8编码的数据库test1

2、建立商品表goods和栏目表category(要求:按如下表结构创建表,并且存储引擎engine myisam 字符集charset utf8)

 

3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段

4、在 goods_name 列上加唯一性索引(用alter table方式)

5、在 shop_price 列上加普通索引(用create index方式)

6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)

二、参考答案

(个人所写,仅供参考)

/* 1、建立一个utf8编码的数据库test1 */
mysql> create database test1 CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use test1;
Database changed


/* 2、建立商品表goods和栏目表category(要求:按如下表结构创建表,并且存储引擎engine myisam 字符集charset utf8)*/
--创建表goods
mysql> create table goods(
    -> goods_id int(11) 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 default '',
    -> shop_price float(6,2) not null default 0.00,
    -> goods_desc text
    -> ) engine=MyISAM charset=utf8;
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   |     | 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)

--创建表category
mysql> create table category(
    -> cat_id int(11) primary key auto_increment,
    -> cate_name varchar(20) not null default '',
    -> parent_id int(11) not null default 0
    -> ) engine=MyISAM charset=utf8;
Query OK, 0 rows affected (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 字段 */
mysql> alter table goods drop goods_desc;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table goods add click_count int;
Query OK, 0 rows affected (0.01 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   |     | 0       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| goods_sn    | char(12)    | NO   |     |         |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
| click_count | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)


/* 4、在 goods_name 列上加唯一性索引(用alter table方式) */
mysql> alter table goods add unique index UniqIdx(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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


/* 5、在 shop_price 列上加普通索引(用create index方式) */
mysql> create index NormalIdx 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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


/* 6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)*/

--方法一:使用drop index删除
            /* 先增加索引 */
mysql> create index NormalIdx2 on goods(click_count);
Query OK, 0 rows affected (0.01 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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`),
  KEY `NormalIdx2` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

          /* drop index删除 */
mysql> drop index NormalIdx2 on goods;
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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

--方法二:使用alter table删除
            /* 先增加索引 */
mysql> create index NormalIdx2 on goods(click_count);
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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`),
  KEY `NormalIdx2` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

           /* alter table删除 */
mysql> alter table goods drop index NormalIdx2;
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 DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `goods_sn` char(12) NOT NULL DEFAULT '',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `UniqIdx` (`goods_name`),
  KEY `NormalIdx` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值