MySQL数据库——索引练习

news/2024/7/8 7:56:15

一、练习题目

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;
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)


http://lihuaxi.xjx100.cn/news/1340713.html

相关文章

找工作前,是否要去培训班进行学习呢?

在学习IT技术的过程中,你是否也被安利过各种五花八门的技术培训班?这些培训班都是怎样向你宣传的,你又对此抱有着怎样的态度呢?在培训班里学技术,真的有用吗? 一、引入话题 一年又一年的毕业季有来临了&am…

Vector - CANoe - CAPL文件加密

目录 为什么会有CAPL文件加密需求? 加密文件介绍 “*.can”和“*.cin” 文件创建 <

微软亚洲研究院推出AI编译器界“工业重金属四部曲”

编者按&#xff1a;编译器在传统计算科学中一直是一个重要的研究课题。在人工智能技术快速发展和广泛应用的今天&#xff0c;人工智能模型需要部署在多样化的计算机硬件架构上。同时&#xff0c;训练和部署大型人工智能模型时又对硬件性能有着更高的要求&#xff0c;有时还需根…

2022 Robocom CAIP省赛 第五题 树与二分图

原题链接&#xff1a; PTA | 程序设计类实验辅助教学平台 题面&#xff1a; 设 G(V,E) 是一个无向图&#xff0c;如果顶点集合 V 可分割为两个互不相交的子集 (A,B)&#xff0c;并且每条边 (i,j)∈E 的两个端点 i 和 j 分别属于这两个不同的顶点子集&#xff0c;则称图 G 为一…

S32K系列MCU介绍和资料搜集

1. S32K系列微控制器概述 S32K系列微控制器&#xff0c;是NXP推出的专门面向汽车电子和工业应用场合的微控制器。基于ArmCortex-M系列的可扩展、低功耗微控制器&#xff0c;获得了AEC-Q100认证&#xff0c;具有高级功能安全、信息安全和软件支持&#xff0c;适用于工业和汽车A…

图像处理之高斯滤波

文章目录 高斯函数1.一维高斯函数2. 二维高斯函数 高斯滤波 高斯函数 高斯函数广泛应用于统计学领域&#xff0c;用于表述正态分布&#xff0c;在信号处理领域&#xff0c;用于定义高斯滤波器&#xff0c;在图像处理领域&#xff0c;二维高斯核函数常用于高斯模糊Gaussian Blu…

基于SpringBoot+vue的外卖点餐系统设计与实现

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

启动、关闭nacos

下载 进入官网 http://nacos.io/zh-cn/ 页面打开后&#xff0c;点击【前往Github】 进入Github页面&#xff0c;往下拖动&#xff0c;找到 latest stable release 选择当时最新的版本下载即可&#xff0c;这里选择.zip的文件下载 安装 Nacos 是免安装的&#xff0c;我们下…