函数
函数是指一段可以直接被另一段程序调用的程序或代码

以上均有函数来解决
字符串函数、数值函数、日期函数、流程函数
字符串函数

select concat('Hello','MySQL');
HelloMySQL
select lower('Hello');
hello
select upper('Hello');
HELLO
select lpad('01',5,'-');
---01
select rpad('01',5,'-');
01---
select trim(' Hello MySQL ');
Hello MySQL
select substring('Hello MySQL',1,5);
Hello
由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0,比如:1号员工的工号应该为00001
update emp set workno = lpad(workno,5,'0');

数值函数

select ceil(1.5);
2
select floor(1.9);
1
select mod(3,4);
3
select rand();
0.9153951191486746
select round(2.345,2);
2.35
通过数据库的函数,生成一个六位数的随机验证码
select round(rand()*1000000,0);
17025,错误×,因为rand() = 0.017025,也在0~1之间
select lpad(round(rand()*1000000,0),6,'0');
024255
select rpad(round(rand()*1000000,0),6,'0');
999880
日期函数

select curdate();
2025-11-25
select curtime();
12:00:15
select now();
2025-11-25 12:00:28
select year(now());
2025
select month(now());
11
select day(now());
25
select date_add(now(), interval 70 day);
2026-02-03 12:03:20
select date_add(now(), interval 70 month);
2031-09-25 12:03:50
select date_add(now(), interval 70 year);
2095-11-25 12:04:28
select datediff('2025-11-25','2026-11-25');
-365
查询所有员工的入职天数,并根据入职天数倒序排序
select name,datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays desc;

流程函数

select if(true,'Ok','Error');
Ok
select ifnull('Ok','Default');
Ok
select ifnull('','Default');
select ifnull(null,'Default');
Default
查询emp表的员工姓名和工作地址(北京/上海 -- 一线城市,其他 -- 二线城市
select
name,
(case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作地址'
from emp;

统计班级各个学员的成绩,展示规则为:>=85:展示优秀;>=60:展示及格;否则,展示不及格
create table score(
id int comment 'ID',
name varchar(20) comment '姓名',
math int comment '数学',
english int comment '英语',
chinese int comment '语文'
) comment '学员成绩表';
insert into score(id, name, math, english, chinese) VALUES (1, 'Tom', 67, 88, 95), (2, 'Rose', 23, 66, 98),(3, 'Jack', 56, 98, 70);

select
id,
name,
(case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end)'数学',
(case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end)'英语',
(case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end)'语文'
from score;

小结

约束
概述、约束演示、外键约束
概述
约束是作用于表中字段上的规则,用于限制存储在表中的数据
目的:保证数据库中数据的正确、有效性和完整性

注意:约束是作用于表中字段上的,可以在创建表/修改表的时候添加约束
约束演示
根据需求,完成表结构的创建


create table user(
id int primary key auto_increment comment '主键',
name varchar(10) not null unique comment '姓名',
age int check(age>0 and age <= 120) comment '年龄',
status char(1) default '1' comment '状态',
gender char(1) comment '性别'
) comment '用户表';

插入数据
id,主键且自增长
insert into user(name, age, status, gender) values ('Tom1', 19, '1', '男'),('Tom2', 25, '0','男');
insert into user(name, age, status, gender) values ('Tom3', 19, '1', '男');

错误×,name非空
insert into user(name, age, status, gender) values (null, 19, '1', '男');
错误×,name唯一
insert into user(name, age, status, gender) values ('Tom3', 19, '1', '男');
insert into user(name, age, status, gender) values ('Tom4', 80, '1', '男');

id为4虽然没有插入成功,但申请了id为4的位置
错误×,age检查约束
insert into user(name, age, status, gender) values ('Tom5', 121, '1', '男');
status默认为1
insert into user(name, age, gender) values ('Tom5', 120, '男');

或者直接用图形化界面创建表
外键约束
外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性


员工表是子表(从表),子表是具有外键的表
部门表是父表(主表),父表是外键所关联的表
注意:目前上述的两张表,在数据库层面,并未建立外键关联,所以是无法保证数据的一致性和完整性的
创建部门表
create table dept(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment '部门名称'
)comment '部门表';
插入部门数据
INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'),(3, '财务部'), (4, '销售部'), (5, '总经办');
创建员工表
create table emp(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment '姓名',
age int comment '年龄',
job varchar(20) comment '职位',
salary int comment '薪资',
entrydate date comment '入职时间',
managerid int comment '直属领导ID',
dept_id int comment '部门ID'
)comment '员工表';
插入员工数据
INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES
(1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),
(2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),
(3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),
(4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),
(5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),
(6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1);
员工表:

部门表:

若直接删除1号部门,但员工表中1号部门的员工不会被删除,所以要建立外键关联

alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);

关联后不能直接删除研发部

alter table emp drop foreign key fk_emp_dept_id;
外键删除更新行为
如果关联存在则不能直接删除

alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade;




alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update set null on delete set null;


图形化界面修改更简单:

小结
