本文最后更新于103 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com
SQL 基础概念
- SQL 是结构化查询语言
- SQL 四大分类
- DDL:数据定义语言
- DCL:数据控制语言
- DML:数据操作语言
- DQL:数据查询语言
一、DDL 数据定义语言
DDL(Data Definition Language)用于定义数据库、表、结构,不操作数据本身。
核心关键字:
create:创建alter:修改drop:删除
1. 数据库操作
sql
-- 创建数据库
create database db01;
-- 创建数据库并指定字符集
create database db02 charset utf8mb4;
-- 查看所有数据库
show databases;
-- 查看数据库创建语句
show create database db01;
-- 修改数据库字符集
alter database db01 charset utf8mb4;
-- 删除数据库
drop database db02;
2. 数据表操作
sql
-- 创建表
create table student(
sid int not null primary key auto_increment comment '学号',
sname varchar(20) not null comment '学生姓名',
sage tinyint unsigned comment '年龄',
sgender enum('m','f') not null default 'm' comment '性别',
cometime datetime not null comment '入学时间'
) engine=innodb charset=utf8mb4;
-- 查看表
show tables;
-- 查看建表语句
show create table student;
-- 查看表结构
desc student;
3. 修改表结构
sql
-- 修改表名
alter table student rename stu;
-- 添加字段
alter table stu add age int;
-- 在指定字段后添加
alter table stu add phone int after age;
-- 删除字段
alter table stu drop qq;
-- 修改字段类型
alter table stu modify sid varchar(20);
-- 修改字段名
alter table stu change phone telphone char(20);
4. 删除表
sql
drop table student;
小项目:学生信息统计
sql
-- 1. 创建数据库
create database school_db charset utf8mb4;
-- 2. 使用数据库
use school_db;
-- 3. 创建学生表
create table student(
sid int not null primary key auto_increment comment '学号',
sname varchar(20) not null comment '姓名',
sage tinyint unsigned not null comment '年龄',
sgender enum('男','女') not null default '男' comment '性别',
class_name varchar(30) not null comment '班级',
enroll_time datetime not null comment '入学时间'
) engine=innodb charset=utf8mb4;
-- 4. 查看表结构
desc student;
-- 5. 添加手机号
alter table student add phone char(11) comment '手机号';
-- 6. 添加邮箱
alter table student add email varchar(50) comment '邮箱';
-- 7. 修改年龄默认值
alter table student modify sage tinyint unsigned not null default 18;
-- 8. 修改字段名
alter table student change class_name class_id varchar(30);
-- 9. 删除邮箱
alter table student drop email;
二、DCL 数据控制语言
DCL(Data Control Language)用于用户管理与权限控制。
核心关键字:
create user:创建用户grant:授权revoke:收权show grants:查看权限
sql
-- 创建用户
create user 'reader1'@'localhost' identified by '123456';
-- 授予查询权限
grant select on school_db.student to 'reader1'@'localhost';
-- 查看权限
show grants for 'reader1'@'localhost';
-- 授予插入权限
grant insert on school_db.student to 'reader1'@'localhost';
-- 收回插入权限
revoke insert on school_db.student from 'reader1'@'localhost';
三、DML 数据操作语言
DML(Data Manipulation Language)用于增、删、改数据。
核心关键字:
insert:插入update:修改delete:删除
1. 插入数据
sql
-- 单条插入
insert into student(sname,sage,sgender,class_id,enroll_time)
values('张三',18,'男','01',now());
-- 多条插入
insert into student(sname,sage,sgender,class_id,enroll_time)
values
('李四',19,'女','02',now()),
('王五',20,'男','03',now());
2. 修改数据
sql
-- 必须加 WHERE,否则全表修改!
update student set sage=21 where sid=2;
update student set class_id='05' where sid=3;
3. 删除数据
sql
-- 删除指定数据
delete from student where sid=1;
-- 清空全表(危险)
truncate table student;
四、DQL 数据查询语言
DQL(Data Query Language)用于查询数据,核心是 select。
基础查询语法
sql
select 字段
from 表
where 条件
order by 排序
limit 条数;
常用查询
sql
-- 查询所有
select * from student;
-- 查询指定字段
select sid,sname,sage from student;
-- 条件查询
select * from student where sgender='男';
-- 多条件 and/or
select * from student where sgender='男' and sage>18;
-- 模糊查询
select * from student where sname like '张%';
-- 范围查询
select * from student where sage between 18 and 22;
-- in 查询
select * from student where sid in (1,2,3);
-- 排序
select * from student order by sage desc;
-- 分页
select * from student limit 0,3;
聚合函数
sql
-- 总数
select count(*) from student;
-- 平均年龄
select avg(sage) from student;
-- 最大/最小年龄
select max(sage), min(sage) from student;
-- 求和
select sum(sage) from student;
分组查询
sql
select class_id, count(*)
from student
group by class_id
having count(*) > 1;
五、字符集与校对规则
1. 字符集(Charset)
决定数据如何存储。
常用:
utf8mb4(推荐,支持 emoji)gbklatin1
2. 校对规则(Collation)
决定比较与排序规则。
ci:大小写不敏感cs:大小写敏感bin:二进制比较
3. 查看命令
sql
show character set;
show collation;
4. 设置字符集
sql
-- 数据库
alter database school_db charset utf8mb4;
-- 表
alter table student charset utf8mb4;
六、SELECT 高级用法
1. 去重
sql
select distinct class_id from student;
2. 别名
sql
select sname as 姓名, sage as 年龄 from student;
3. 多表连接(内连接)
sql
select s.*, c.class_name
from student s
join class c on s.class_id = c.class_id;
4. 子查询
sql
select * from student
where class_id = (select class_id from class where class_name='计科1班');
5. 合并结果集
sql
select sname from student
union
select tname from teacher;