基于Linux中的数据库操作——例题实操(3)

1.数据库常问问题

一、事务的四个特点

  1. **原子性(Atomicity)**事务是最小执行单位,要么全部成功,要么全部失败回滚,不可分割。
  2. **一致性(Consistency)**事务执行前后,数据库的完整性约束不被破坏,数据始终合法。
  3. **隔离性(Isolation)**多个并发事务之间相互隔离,互不干扰。
  4. **持久性(Durability)**事务一旦提交,对数据的修改就是永久的,断电、宕机也不会丢失。

二、事务的四个隔离级别(从低到高)

1. 读未提交(READ UNCOMMITTED)

  • 一个事务能读到另一个事务未提交的数据
  • 问题:脏读脏读:读到了别人没提交的数据(别人回滚就废了))

2. 读已提交(READ COMMITTED)

  • 只能读到其他事务已提交的数据
  • 解决:脏读
  • 问题:不可重复读

3. 可重复读(REPEATABLE READ)

  • 同一个事务内多次读取结果一致
  • 解决:脏读、不可重复读
  • 问题:幻读 (MySQL 通过间隙锁基本解决)(幻读 :同一事务内,两次查询的行数不一样)

4. 串行化(SERIALIZABLE)

  • 事务排队执行,完全串行
  • 解决:所有并发问题
  • 缺点:性能最差

三、索引

1. 索引是什么?

索引是数据库表中一列或多列的值排序后的数据结构 ,相当于书的目录 ,用于快速定位数据

2. 索引的作用

  1. 大幅提高查询速度(最核心)
  2. 加速 表连接(JOIN)
  3. 减少服务器需要扫描的数据量
  4. 帮助排序、分组操作提高效率

3. 索引的缺点

  1. 会占用额外磁盘空间
  2. 增、删、改操作变慢(因为索引也要同步更新)
  3. 过多索引会导致优化器选择困难,反而降低性能
  4. 维护成本高,不适合频繁变动的表

2.题目

TableX 有三个字段 Code、 Name、 Age、 其中 Code 为主键; TableY 有三个字段 Code、 Class、Score, 其中 Code + Class 为主键。两表记录如下:

TableX
Code Name Age
97001 张三 22
97002 赵四 21
97003 张飞 20
97004 李五 22
TableY
Code Class Score
97001 数学 80
97002 计算机 59
97003 计算机 60
97004 数学 55

1、请写出SQL,取出计算机科考成绩不及格的学生;

2、通过等值联接,取出Name、Class、Score,请写出SQL语句及输出结果

3、通过外联接,取出每个学生的Name、Class、Score,请写SQL语句及输出结果

4、李五的成绩记录错了,应该是数学=21,请写SQL,根据主键进行更新

5、请写SQL,删除TableX中没有考试成绩的学生记录

6、为鼓励学计算机,将年龄大于22岁的学生计算机成绩不及格的加上10分,如果加上后及格则按60分计算

2.1 创建表

bash 复制代码
mysql> create database student;

mysql> use student;
bash 复制代码
mysql> create table tablex (
    -> code varchar(10) not null primary key,
    -> name varchar(20) not null ,
    -> age int not null
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tablex(code, name, age)
    -> values
    -> ('97001','张三',22),
    -> ('97002','赵四',21),
    -> ('97003','张飞',20),
    -> ('97004','李五',22);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> create table tabley (
    -> code varchar(10) not null,
    -> class varchar(20) not null,
    -> score int not null,
    -> primary key (code,class)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tabley (code, class, score)
    -> values
    -> ('97001','数学',80),
    -> ('97002','计算机',59),
    -> ('97003','计算机',60),
    -> ('97004','数学',55);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

查看创建好后的表:

bash 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| student            |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| tablex            |
| tabley            |
+-------------------+
2 rows in set (0.00 sec)

mysql> select * from tablex;
+-------+--------+-----+
| code  | name   | age |
+-------+--------+-----+
| 97001 | 张三   |  22 |
| 97002 | 赵四   |  21 |
| 97003 | 张飞   |  20 |
| 97004 | 李五   |  22 |
+-------+--------+-----+
4 rows in set (0.00 sec)

mysql> select * from tabley;
+-------+-----------+-------+
| code  | class     | score |
+-------+-----------+-------+
| 97001 | 数学      |    80 |
| 97002 | 计算机    |    59 |
| 97003 | 计算机    |    60 |
| 97004 | 数学      |    55 |
+-------+-----------+-------+
4 rows in set (0.00 sec)

mysql> desc tablex;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| code  | varchar(10) | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | int         | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc tabley;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| code  | varchar(10) | NO   | PRI | NULL    |       |
| class | varchar(20) | NO   | PRI | NULL    |       |
| score | int         | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

2.2 请写出SQL,取出计算机科考成绩不及格的学生

bash 复制代码
mysql> select x.code, x.name, y.class, y.score
    -> from tablex x
    -> join tabley y on x.code = y.code
    -> where y.class = '计算机' and y.score < 60;
+-------+--------+-----------+-------+
| code  | name   | class     | score |
+-------+--------+-----------+-------+
| 97002 | 赵四   | 计算机    |    59 |
+-------+--------+-----------+-------+
1 row in set (0.01 sec)

2.3 通过等值联接,取出Name、Class、Score,请写出SQL语句及输出结果

bash 复制代码
mysql> select x.name, y.class, y.score
    -> from tablex x, tabley y
    -> where x.code = y.code;
+--------+-----------+-------+
| name   | class     | score |
+--------+-----------+-------+
| 张三   | 数学      |    80 |
| 赵四   | 计算机    |    59 |
| 张飞   | 计算机    |    60 |
| 李五   | 数学      |    55 |
+--------+-----------+-------+

2.4 通过外联接,取出每个学生的Name、Class、Score,请写SQL语句及输出结果

bash 复制代码
mysql> select x.name, y.class, y.score
    -> from tablex x
    -> left join tabley y on x.code = y.code
    -> ;
+--------+-----------+-------+
| name   | class     | score |
+--------+-----------+-------+
| 张三   | 数学      |    80 |
| 赵四   | 计算机    |    59 |
| 张飞   | 计算机    |    60 |
| 李五   | 数学      |    55 |
+--------+-----------+------

2.5 李五的成绩记录错了,应该是数学=21,请写SQL,根据主键进行更新

bash 复制代码
mysql> update tabley
    -> set score = 21
    -> where code='97004';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tabley;
+-------+-----------+-------+
| code  | class     | score |
+-------+-----------+-------+
| 97001 | 数学      |    80 |
| 97002 | 计算机    |    59 |
| 97003 | 计算机    |    60 |
| 97004 | 数学      |    21 |
+-------+-----------+-------+

2.6 请写SQL,删除TableX中没有考试成绩的学生记录

bash 复制代码
mysql> delete from tablex
    -> where code not in(
    -> select code from tabley);
Query OK, 0 rows affected (0.00 sec)
bash 复制代码
mysql> select * from tablex;
+-------+--------+-----+
| code  | name   | age |
+-------+--------+-----+
| 97001 | 张三   |  22 |
| 97002 | 赵四   |  21 |
| 97003 | 张飞   |  20 |
| 97004 | 李五   |  22 |
+-------+--------+-----+
4 rows in set (0.00 sec)

2.7 为鼓励学计算机,将年龄大于22岁的学生计算机成绩不及格的加上10分,如果加上后及格则按60分计算

bash 复制代码
mysql> update tabley y
    -> join tablex x on y.code = x.code
    -> set y.score =
    -> case
    -> when y.score + 10 >= 60 then 60
    -> else y.score + 10
    -> end 
    -> where x.age > 22
    -> and y.class='计算机'
    -> and y.score < 60;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
bash 复制代码
mysql> select x.name, x.age, y.score
    -> from tablex x join tabley y on x.code=y.code
    -> where y.class='计算机';
+--------+-----+-------+
| name   | age | score |
+--------+-----+-------+
| 赵四   |  21 |    59 |
| 张飞   |  20 |    60 |
+--------+-----+-------+
相关推荐
小猿姐13 小时前
实测对比:哪款开源 Kubernetes MySQL Operator 最值得用?(2026 深度评测)
数据库·mysql·云原生
倔强的石头_15 小时前
从 “存得下” 到 “算得快”:工业物联网需要新一代时序数据平台
数据库
TDengine (老段)16 小时前
TDengine IDMP 可视化 —— 分享
大数据·数据库·人工智能·时序数据库·tdengine·涛思数据·时序数据
GottdesKrieges17 小时前
OceanBase数据库备份配置
数据库·oceanbase
SPC的存折17 小时前
MySQL 8组复制完全指南
linux·运维·服务器·数据库·mysql
运维行者_17 小时前
OpManager MSP NetFlow Analyzer集成解决方案,应对多客户端网络流量监控挑战
大数据·运维·服务器·网络·数据库·自动化·运维开发
炸炸鱼.19 小时前
Python 操作 MySQL 数据库
android·数据库·python·adb
softshow102619 小时前
Etsy 把 1000 个 MySQL 分片迁进 Vitess
数据库·mysql
Ronaldinho Gaúch19 小时前
MySQL基础
数据库·mysql
不剪发的Tony老师20 小时前
Noir:一款键盘驱动的现代化数据库管理工具
数据库·sql