基于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 |
+--------+-----+-------+
相关推荐
mygljx2 小时前
Redis 下载与安装 教程 windows版
数据库·windows·redis
奕成则成2 小时前
Redis 大 Key 问题排查与治理:原因、危害、实战方案
数据库·redis·缓存
dapeng28702 小时前
Python异步编程入门:Asyncio库的使用
jvm·数据库·python
2401_851272993 小时前
Python面向对象编程(OOP)终极指南
jvm·数据库·python
2401_831824963 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
551只玄猫3 小时前
【数据库原理 实验报告5】数据查询的应用(连接)
数据库·sql·mysql·课程设计·实验报告
Liu628883 小时前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
TDengine (老段)3 小时前
TDengine IDMP 组态面板 —— 图元库
大数据·数据库·时序数据库·tdengine·涛思数据
dgvri3 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot