当前位置:网站首页>数据库系统原理与应用教程(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)
边栏推荐
猜你喜欢
随机推荐
OpenCV形状检测
强烈推荐APP破解常用工具集合!
olap——入门ClickHouse
592. Fraction Addition and Subtraction
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
Promise入门到精通(1.5w字详解)
KDD‘21推荐系统离散特征表征无embedding table Learning to Embed Categorical Features without Embedding Tables for
Express框架连接MySQL及ORM框架
How Google earth engine realizes the arrangement and selection of our time list
leetcode:1488. 避免洪水泛滥【二分 + 贪心】
DLCM - 基于列表上下文信息的重排序模型
Tensorflow中实现正则化
Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
un7.30:linux——如何在docker容器中安装MySQL?
腾讯专家献上技术干货,带你一览腾讯广告召回系统的演进
C陷阱与缺陷 第7章 可移植性缺陷 7.4 字符是有符号数还是无符号数
C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
LeetCode318: Maximum product of word lengths