当前位置:网站首页>数据库系统原理与应用教程(068)—— MySQL 练习题:操作题 90-94(十二):DML 语句练习
数据库系统原理与应用教程(068)—— MySQL 练习题:操作题 90-94(十二):DML 语句练习
2022-07-30 17:26:00 【睿思达DBA_WGX】
数据库系统原理与应用教程(068)—— MySQL 练习题:操作题 90-94(十二):DML 语句练习
90、创建表
现有一张用户信息表(user_info),表结构如下:
| Filed | Type | Null | Key | Default | Extra | Comment |
|---|---|---|---|---|---|---|
| id | int(11) | NO | PRI | (NULL) | auto_increment | 自增ID |
| uid | int(11) | NO | UNI | (NULL) | 用户ID | |
| nick_name | varchar(64) | YES | (NULL) | 昵称 | ||
| achievement | int(11) | YES | 0 | 成就值 | ||
| level | int(11) | YES | (NULL) | 用户等级 | ||
| job | varchar(32) | YES | (NULL) | 职业方向 | ||
| register_time | datetime | YES | CURRENT_TIMESTAMP | 注册时间 |
【问题】,请创建一张 vip 用户信息表(user_info_vip),表结构和用户信息表一致。
| Filed | Type | Null | Key | Default | Extra | Comment |
|---|---|---|---|---|---|---|
| id | int(11) | NO | PRI | auto_increment | 自增ID | |
| uid | int(11) | NO | UNI | 用户ID | ||
| nick_name | varchar(64) | YES | 昵称 | |||
| achievement | int(11) | YES | 0 | 成就值 | ||
| level | int(11) | YES | 用户等级 | |||
| job | varchar(32) | YES | 职业方向 | |||
| register_time | datetime | YES | CURRENT_TIMESTAMP | 注册时间 |
解答:
/* create table user_info_vip( id int primary key auto_increment comment '自增ID', uid int not null comment '用户ID', nick_name varchar(64) default 0 comment '昵称', achievement int comment '成就值', level int comment '用户等级', job varchar(32) comment '职业方向', register_time datetime default CURRENT_TIMESTAMP comment '注册时间', unique(uid) ) CHARACTER SET utf8 COLLATE utf8_general_ci;; */
mysql> mysql> SHOW FULL FIELDS FROM user_info_vip;
+---------------+-------------+-----------------+------+-----+-------------------+----------------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+---------------+-------------+-----------------+------+-----+-------------------+----------------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | 自增ID |
| uid | int(11) | NULL | NO | UNI | NULL | | select,insert,update,references | 用户ID |
| nick_name | varchar(64) | utf8_general_ci | YES | | 0 | | select,insert,update,references | 昵称 |
| achievement | int(11) | NULL | YES | | NULL | | select,insert,update,references | 成就值 |
| level | int(11) | NULL | YES | | NULL | | select,insert,update,references | 用户等级 |
| job | varchar(32) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 职业方向 |
| register_time | datetime | NULL | YES | | CURRENT_TIMESTAMP | | select,insert,update,references | 注册时间 |
+---------------+-------------+-----------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
91、修改表结构
用户信息表user_info,表结构如下:
| Filed | Type | Null | Key | Default | Extra | Comment |
|---|---|---|---|---|---|---|
| id | int(11) | NO | PRI | (NULL) | auto_increment | 自增ID |
| uid | int(11) | NO | UNI | (NULL) | 用户ID | |
| nick_name | varchar(64) | YES | (NULL) | 昵称 | ||
| achievement | int(11) | YES | 0 | 成就值 | ||
| level | int(11) | YES | (NULL) | 用户等级 | ||
| job | varchar(32) | YES | (NULL) | 职业方向 | ||
| register_time | datetime | YES | CURRENT_TIMESTAMP | 注册时间 |
【问题】请在用户信息表 level 列的后面增加一列,列名为:school,最多可保存 15 个汉字;并将 job 列的列名修改为 profession,同时 varchar 字段长度变为 10;achievement 列的默认值设置为 0。修改之后的结果如下:
| Filed | Type | Null | Key | Default | Extra | Comment |
|---|---|---|---|---|---|---|
| id | int(11) | NO | PRI | auto_increment | 自增ID | |
| uid | int(11) | NO | UNI | 用户ID | ||
| nick_name | varchar(64) | YES | 昵称 | |||
| achievement | int(11) | YES | 0 | |||
| level | int(11) | YES | 用户等级 | |||
| school | varchar(15) | |||||
| profession | varchar(10) | YES | ||||
| register_time | datetime | YES | CURRENT_TIMESTAMP | 注册时间 |
表结构如下:
/* drop table if exists user_info; CREATE TABLE IF NOT EXISTS user_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int UNIQUE NOT NULL COMMENT '用户ID', `nick_name` varchar(64) COMMENT '昵称', achievement int COMMENT '成就值', level int COMMENT '用户等级', job varchar(10) COMMENT '职业方向', register_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; */
解答:
/* alter table user_info add school varchar(15) after level; alter table user_info change job profession varchar(10); alter table user_info modify achievement int default 0; */
mysql> alter table user_info add school varchar(15) after level;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user_info change job profession varchar(10);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user_info modify achievement int default 0;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user_info;
+---------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| uid | int(11) | NO | UNI | NULL | |
| nick_name | varchar(64) | YES | | NULL | |
| achievement | int(11) | YES | | 0 | |
| level | int(11) | YES | | NULL | |
| school | varchar(15) | YES | | NULL | |
| profession | varchar(10) | YES | | NULL | |
| register_time | datetime | YES | | CURRENT_TIMESTAMP | |
+---------------+-------------+------+-----+-------------------+----------------+
8 rows in set (0.00 sec)
92、删除表
现有一张试卷答题记录表:exam_record,一般每年都会为 exam_record 表建立一张备份表 exam_record_{YEAR},{YEAR}为对应年份。请把很久前的(2011到2014年)备份表都删掉(如果存在的话)。
表结构及数据如下:
/* drop table if EXISTS exam_record; CREATE TABLE IF NOT EXISTS exam_record ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int NOT NULL COMMENT '用户ID', exam_id int NOT NULL COMMENT '试卷ID', start_time datetime NOT NULL COMMENT '开始时间', submit_time datetime COMMENT '提交时间', score tinyint COMMENT '得分' )CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE TABLE IF NOT EXISTS exam_record_2010 (LIKE exam_record); CREATE TABLE IF NOT EXISTS exam_record_2012 (LIKE exam_record); CREATE TABLE IF NOT EXISTS exam_record_2013 (LIKE exam_record); CREATE TABLE IF NOT EXISTS exam_record_2014 (LIKE exam_record); CREATE TABLE IF NOT EXISTS exam_record_2015 (LIKE exam_record); */
解答:
mysql> SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'exam_record_201_';
+------------------+
| TABLE_NAME |
+------------------+
| exam_record_2010 |
| exam_record_2012 |
| exam_record_2013 |
| exam_record_2014 |
| exam_record_2015 |
+------------------+
5 rows in set (0.05 sec)
/* drop table if exists exam_record_2011; drop table if exists exam_record_2012; drop table if exists exam_record_2013; drop table if exists exam_record_2014; */
mysql> drop table if exists exam_record_2011;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> drop table if exists exam_record_2012;
Query OK, 0 rows affected (0.00 sec)
mysql> drop table if exists exam_record_2013;
Query OK, 0 rows affected (0.01 sec)
mysql> drop table if exists exam_record_2014;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'exam_record_201_';
+------------------+
| TABLE_NAME |
+------------------+
| exam_record_2010 |
| exam_record_2015 |
+------------------+
2 rows in set (0.00 sec)
93、创建索引
现有一张试卷信息表:examination_info,表结构如下:
| Filed | Type | Null | Key | Default | Extra | Comment |
|---|---|---|---|---|---|---|
| id | int(11) | NO | PRI | NULL | auto_increment | 自增ID |
| exam_id | int(11) | NO | UNI | NULL | 试卷ID | |
| tag | varchar(32) | YES | NULL | 类别标签 | ||
| difficulty | varchar(8) | YES | NULL | 难度 | ||
| duration | int(11) | NO | NULL | 时长 | ||
| release_time | datetime | YES | NULL | 发布时间 |
为了对表更方便快捷地查询,需要在 examination_info 表创建以下索引,规则如下:在 duration 列创建普通索引idx_duration,在 exam_id 列创建唯一性索引 uniq_idx_exam_id、在 tag 列创建全文索引 full_idx_tag。结果如下:
| examination_info | 0 | PRIMARY | 1 | id | A | 0 | BTREE | |||
|---|---|---|---|---|---|---|---|---|---|---|
| examination_info | 0 | uniq_idx_exam_id | 1 | exam_id | A | 0 | YES | BTREE | ||
| examination_info | 1 | idx_duration | 1 | duration | A | 0 | BTREE | |||
| examination_info | 1 | full_idx_tag | 1 | tag | 0 | YES | FULLTEXT |
表结构及数据如下:
/* drop table if exists examination_info; CREATE TABLE IF NOT EXISTS examination_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', exam_id int UNIQUE NOT NULL COMMENT '试卷ID', tag varchar(32) COMMENT '类别标签', difficulty varchar(8) COMMENT '难度', duration int NOT NULL COMMENT '时长', release_time datetime COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_bin; */
解答:
mysql> SHOW INDEX FROM examination_info;
+------------------+------------+----------+--------------+-------------+-----------+-------------
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality
+------------------+------------+----------+--------------+-------------+-----------+-------------+
| examination_info | 0 | PRIMARY | 1 | id | A | 0 |
| examination_info | 0 | exam_id | 1 | exam_id | A | 0 |
+------------------+------------+----------+--------------+-------------+-----------+-------------+
2 rows in set (0.01 sec)
/* create index dx_duration on examination_info(duration); create unique index uniq_idx_exam_id on examination_info(exam_id); create fulltext index full_idx_tag on examination_info(tag); */
mysql> SHOW INDEX FROM examination_info;
+------------------+------------+------------------+--------------+-------------+-----------+
+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
|+------------------+------------+------------------+--------------+-------------+-----------+
+| examination_info | 0 | PRIMARY | 1 | id | A |
|| examination_info | 0 | exam_id | 1 | exam_id | A |
|| examination_info | 0 | uniq_idx_exam_id | 1 | exam_id | A |
|| examination_info | 1 | dx_duration | 1 | duration | A |
|| examination_info | 1 | full_idx_tag | 1 | tag | NULL |
|+------------------+------------+------------------+--------------+-------------+-----------+
+5 rows in set (0.00 sec)
94、删除索引
【问题】请删除 examination_info 表上的唯一索引 uniq_idx_exam_id 和全文索引 full_idx_tag。
解答:
mysql> SHOW INDEX FROM examination_info;
+------------------+------------+------------------+--------------+-------------+-----------+
+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
|+------------------+------------+------------------+--------------+-------------+-----------+
+| examination_info | 0 | PRIMARY | 1 | id | A |
|| examination_info | 0 | exam_id | 1 | exam_id | A |
|| examination_info | 0 | uniq_idx_exam_id | 1 | exam_id | A |
|| examination_info | 1 | dx_duration | 1 | duration | A |
|| examination_info | 1 | full_idx_tag | 1 | tag | NULL |
|+------------------+------------+------------------+--------------+-------------+-----------+
+5 rows in set (0.00 sec)
/* drop index uniq_idx_exam_id on examination_info; drop index full_idx_tag on examination_info; */
mysql> drop index uniq_idx_exam_id on examination_info;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> drop index full_idx_tag on examination_info;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW INDEX FROM examination_info;
+------------------+------------+-------------+--------------+-------------+-----------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
+------------------+------------+-------------+--------------+-------------+-----------+
| examination_info | 0 | PRIMARY | 1 | id | A |
| examination_info | 0 | exam_id | 1 | exam_id | A |
| examination_info | 1 | dx_duration | 1 | duration | A |
+------------------+------------+-------------+--------------+-------------+-----------+
3 rows in set (0.00 sec)
边栏推荐
- Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
- FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商
- C陷阱与缺陷 第6章 预处理器 6.3 宏并不是语句
- 每日一题:两数之和
- Error occurred while trying to proxy request项目突然起不来了
- Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
- ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
- LeetCode318:单词长度的最大乘积
- 实现web实时消息推送的7种方案
- 代码越写越乱?那是因为你没用责任链
猜你喜欢

592. Fraction Addition and Subtraction

Excel导入和导出

SLIM: Sparse Linear Methods (TopN推荐)

真正懂经营管理的CIO具备哪些特质

torch.optim.Adam() function usage

论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》

说几个大厂分库分表的那点破事。

从零开始的Multi-armed Bandit

基于模糊PID的液压舵机伺服系统

Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
随机推荐
C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
Wanhua chemical fine chemical industry innovation product assembly
每日一题:两数之和
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
Express框架连接MySQL及ORM框架
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
mysql进制安装与mysql密码破解
如何让 JOIN 跑得更快?
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
Py程序员的七夕情人节
你是这样的volatile,出乎意料
(18)[系统调用]追踪系统调用(服务表)
阿里巴巴CAN:Embedding前置的特征交互新思路
esp32系列(5):esp32 蓝牙架构学习
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
【AAAI2020】阿里DMR:融合Matching思想的深度排序模型
浅谈在线编辑器中增量编译技术的应用
获得抖音商品详情 API
Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
有效的括号字符串[贪心练习]