子查询
子查询:select
查询语句中嵌套有其它 select
语句,嵌套在里面的 select
查询被称为子查询(或内查询),外面的查询被称为主查询(或外查询 )。
单行子查询
单行子查询,即子查询的结果只有一行。常用搭配操作符有> >= < <= = != <>
等。
-- 查询年龄大于 1002 号学生年龄的所有学生信息
select * from student
where age > (
select age from student where stuno = '1002'
)
多行子查询
多行子查询,即子查询的结果不止一行。常用搭配操作符有 IN ANY ALL
等。
- IN
IN 用于检查某个值是否在子查询返回的结果集中。
- ALL
ALL 用于比较一个值是否大于或小于子查询返回的所有值。
- ANY
ANY 用于比较一个值是否大于或小于子查询返回的任意一个值。
总结
使用 IN 时,检查某个值是否存在于结果集中。
使用 ALL 时,检查某个值是否大于(或小于)子查询返回的所有值。
使用 ANY 时,检查某个值是否大于(或小于)子查询返回的任意一个值。
-- 查询男生中,年龄与任一女生相同的所有男生
select * from student
where sex = '男' and age in (
select age from student where sex = '女'
)
-- 查询男生中,年龄与任一女生相同的所有男生
select * from student
where sex = '男' and age =any (
select age from student where sex = '女'
)
-- 查询男生中,年龄大于任一女生的所有男生(等价于查询比年龄最小的女生大的男生)
select * from student
where sex = '男' and age >any (
select age from student where sex = '女'
)
-- 查询男生中,年龄小于任一女生的所有男生(等价于查询比年龄最大的女生小的男生)
select * from student
where sex = '男' and age <any (
select age from student where sex = '女'
)
相关子查询 EXISTS
子查询的查询条件依赖于外层主查询的某个属性值,带 EXISTS
的子查询就称为相关子查询 。 EXISTS
表示存在,带有 EXISTS
的子查询不返回记录,只返回逻辑值 true
或 false
;即子查询能查到结果, EXISTS
子句返回true
,子查询查不到结果, EXISTS
子句返回 false
。
-- 查询参加过考试(有成绩)的学生
select * from student s
where EXISTS (
select * from score sc where sc.stuno = s.stuno
)
DML 和 TCL
DML
DML (Data Manipulation Language),数据操作语言,负责对数据进行增删改,关键字有 insert
、delete
、update
。
insert
-- 1.1 insert into values
-- 向表中插入 1 条记录
insert into student(stuno,name) values('6001','王五')
-- 向表中插入 多 条记录
insert into student(stuno,name)
values('6001','王五'),('6002','赵六')
-- 1.2 insert into value
-- 向表中插入 1 条记录
insert into student(stuno,name) value('5001','张三')
-- 向表中插入 多 条记录
insert into student(stuno,name)
value('5001','张三'),('5002','李四')
-- 1.3 insert into select
-- 将 student 表中 stuno 和 name 这两列数据,插入到 stu 表中。
insert into stu(stuno,name)
select stuno,name from student
delete
delete from 表
,从表中删除数据。如果没有where
条件限定,则删除表中所有数据。
-- 删除 1001 号学生
delete from student where stuno = 1001
update
update 表 set col1 = val1 ,...., colN = valN
,更新表中的列值。如果没有where
条件限定,则更新表中所有数据。
-- 更新 1002 号学生的姓名为“无名”,性别为“男”
update student set name = '无名',sex ='男' where stuno = '1002'
TCL
TCL ( Trasaction Control Language),事务控制语言,负责增删改操作的提交或撤销,关键字有 commit
和 rollback
。
-
事务:一次性完成的一组DML操作
-- 查看当前数据库事务是否设置了自动提交:1(启用自动提交),0(禁用自动提交) select @@autocommit -- 启用或禁用事务自动提交 set autocommit = 1 | 0
-
commit
-- 禁用事务自动提交 set autocommit = 0 -- 执行 DML 操作 delete from stu where stuno = 1003 -- 手动提交事务 commit
-
rollback
-- 禁用事务自动提交 set autocommit = 0 -- 执行 DML 操作 delete from stu where stuno = 1003 -- 手动撤销事务 rollback
-
savepoint
insert into temp(name,age) values('aa',20); -- 设置事务保存点 s1 SAVEPOINT s1; insert into temp(name,age) values('bb',21); -- 回顾当前事务到 保存点 s1 时的状态 rollback to SAVEPOINT s1; -- 提交事务后,数据库中新增了一条记录: aa 20 COMMIT;
-
事务四大特性(ACID):
-
事务的四大特性通常被称为ACID特性,这些特性确保数据库事务的可靠性和一致性。具体如下:
1. 原子性 (Atomicity)
原子性保证事务中的所有操作要么全部成功,要么全部失败。如果事务中的某个操作失败,整个事务将被撤销,数据库状态将恢复到事务开始之前的状态。
2. 一致性 (Consistency)
一致性确保事务在执行前后,数据库的完整性约束始终保持不变。即事务结束后,所有数据必须处于一种有效的状态,任何业务规则或约束都必须得到遵循。
3. 隔离性 (Isolation)
隔离性确保并发执行的事务彼此之间不会相互干扰。每个事务在执行时都应该是独立的,其他事务的操作对其不可见。不同的隔离级别(如读未提交、读已提交、可重复读、串行化)可以控制事务之间的隔离程度。
4. 持久性 (Durability)
持久性保证一旦事务被提交,其结果就是永久性的,即使系统崩溃或发生故障,已提交的数据也会被保留。这通常通过将数据写入持久存储(如磁盘)来实现。
总结
ACID特性共同确保了数据库系统在面对错误、崩溃和并发操作时能够保持数据的完整性和可靠性。
-
-
并发事务的隔离问题
-
脏读
-
不可重复读
-
幻读
-
-
事务的隔离级别
-
未提交读
-
提交读
-
可重复读
-
可串行化
-
DDL 表
DDL (Data Definition Language),数据定义语言,负责定义数据库中的对象(表、视图、序列等),关键字有 create
、alter
、drop
、truncate
等。
创建表 create
- 方式1
md-end-block
-- 创建一张名为 teacher 的表
create table teacher(
tno int PRIMARY KEY, -- 主键约束
id varchar(18) UNIQUE, -- 唯一约束
name varchar(10) NOT NULL, -- 非空约束
age int CHECK(age >= 18 and age <= 65), -- 检查约束
sex varchar(4) DEFAULT '男'
)
- 方式2
md-end-block
create table tea_log
as select * from teacher where 1!=1
表命名规则:
-
表名由字母、数字和下划线
---
构成。 -
字母需小写,禁止大写。
-
不能使用数据库关键字。(比如name、password、time、select、create 等)
约束:
-
主键约束
-
外键约束
-
唯一约束
-
非空约束
-
检查约束
-
默认值
重命名表
md-end-block
-- 将表 tea_log 重命名为 teacher_log
rename table tea_log to tlog
修改表 alter
md-end-block
-- 添加 列
alter table teacher
add salary int
-- 修改 列名和类型
alter table teacher
change salary sal double
-- 修改 列类型
alter table teacher
modify sal float
-- 删除 列
alter table teacher
drop sal
删除表 drop
md-end-block
drop table teacher_log
截断表 truncate
截断表,是指将表中的数据清空。
md-end-block
truncate table teacher
delete、 truncate、 drop 区别
-
delete 是 DML 操作,而 truncate 和 drop 是 DDL 操作。
-
delete 和 truncate 是删数据,而 drop 是删表。
-
delete 是删除指定条件的数据,而 truncate 是清空表。
-
delete 是 DML 操作,数据删除后未提交前可回滚,而 truncate 是 DDL 操作,数据一旦删除,无法撤销。
范式
关系型数据库中,表的设计有6大范式,通常表设计满足前三范式即可。
-
第一范式(1NF):数据库表的每一列都是不可分割的原子项。
有
学生表(学号,姓名,性别,年龄,学院专业)
,此时学院专业
这一项还可以继续分割为学院
和专业
两项,故表不符合1NF
,需将学院专业
进行拆分。 -
第二范式(2NF):在 1NF 的基础上,表必须有主码,非主属性码必须完全依赖于主码。
-
第三范式(3NF):在 2NF 的基础上,表不存在传递函数依赖。