一、SQL基础语句
1. DDL 操作数据库数据库、表、列
| |
| show database; |
| |
| create database daName; |
| create database if not exists dbName; |
| |
| drop database daName; |
| drop database if exists dbName; |
| |
| select database(); #查询当前使用的数据库 |
| use dbName; #使用数据库 |
| |
| show tables; #查询当前数据库下所有表 |
| desc daName; #查询表结构 |
| create table tb_users ( |
| id int, |
| username varcahr(20), |
| password varchar(32) |
| ); |
| # 注意:最后一个字段不加逗号! |
2. DML 操作表中数据-增删改查
| |
| insert into tableName(name,age) values('小明',18); |
| |
| insert into tableName values('小明',18,'广州',...); |
| |
| insert into tableName(name,age) values('小明',18),('小聪',19),...; |
| insert into tableName values('小明',18,'广州',...),('小聪',19,'广州',...),...; |
3. DQL 对表中数据进行查询
| |
| select name,age from dbName; |
| select * from dbName; |
| |
| select distinct city from dbName; |
| |
| select city as 城市 from dbName; |
| select city 城市 from dbName;#可省略as |
| |
| select name from dbName where 条件列表; |
| |
| > < >= <= = != |
| between ... and ... |
| in(...) |
| like 占位符 |
| is null |
| is not null |
| and 或 && |
| or 或 || |
| not 或 ! |
| |
| select name from dbName order by 排序字段名1 [排序方式1],[排序方式2],[排序方式3]; |
| |
| ASC 默认,升序 |
| DESC 降序 |
| |
| count(列名) |
| max(列名) |
| min(列名) |
| sum(列名) |
| avg(列名) |
| |
| |
| select 聚合函数名(列名) from 表名; |
| |
| |
| select sex, avg(age), stuNum from studentTable [where 分组前条件限定] group by sex [having 分组后过滤]; |
| |
| |
| select sex, avg(math), count(*) from stu where math > 70 group by sex having count(*) > 2; |
| |
| |
| |
| select * from stu limito,3; |
| |
| |
| select * from stu limito,3; |
| |
| |
| select * from stu limit 3,3; |
| |
| |
| select * from stu limit 6,3; |
| |
| |
| 起始索引=(当前页码-1)*每页显示的条数 |
4. 数据类型

5. 约束
非空NOT NULL、唯一UNIQUE、主键PRIMARY KEY、默认DEFAULT、检查CHECK、外键FOREIGN KEY
二、数据库设计—多表关系实现
1. 数据库关系
2. 一对多关系实现
- 实现方式:在“多”的一方建立外键,指向“一”的一方的主键

3. 多对多关系实现
- 实现方式:建立中间表,中间表至少包含两个外键,分别关联两方的主键

三、多表查询
1. 连接查询
(1)隐式内连接
| select 字段列表 from 表1,表2... where 条件; |
| select stu.name, stu.age, class.className from stu, class where stu.class_id = class.stu_id; |
| select t1.name, t1.age, t2.className from stu t1, class t2 where stu.class_id = class.stu_id; |
(2)显示内连接
| select 字段列表 from 表1 [inner] join 表2 on 条件; |
| select * from stu join class on stu.class_id = class.stu_id; |
(1)左外连接
| select 字段列表 from 表1 left [outer] join 表2 on 条件; |
(2)右外连接
| select 字段列表 from 表1 right [outer] join 表2 on 条件; |
2. 子查询
| select 字段列表 from 表 where 字段名 = (子查询); |
| select * from stu where age >= (select age from stu where stu_id = 1); |
| select 字段列表 from 表 where 字段名 in (子查询); |
| select * from stu where city_id in (select city_id from city where cityName = '广州' or '深圳'); |
| select 字段列表 from (子查询) where 条件; |
| select * from (select * from stu where birthday > '2000-01-01') reTableName, city where reTableName.city_id = city.id; |
四、事务
1. 事务四大特性
- 原子性(Atomicity): 事务是不可分割的最小操作单位,要么同时成功,要么同时失败
- 一致性(Consistency): 事务完成时,必须使所有的数据都保持一致状态
- 隔离性(lsolation): 多个事务之间,操作的可见性
- 持久性(Durability): 事务一旦提交或回滚,它对数据库中的数据的改变就是永久的
2. 事务流程
| |
| start transaction; |
| |
| ... |
| ... |
| |
| commit; |
| ... |
| ... |
| |
| rollback; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)