当前位置:网站首页>MySQL - Function and Constraint Commands
MySQL - Function and Constraint Commands
2022-07-30 06:46:00 【m0_67402564】
目录
函数
字符串函数

# A. concat : 字符串拼接
select concat('Hello' , ' MySQL');
# B. lower : 全部转小写
select lower('Hello');
# C. upper : 全部转大写
select upper('Hello');
# D. lpad : 左填充
select lpad('01', 5, '-');
# E. rpad : 右填充
select rpad('01', 5, '-');
# F. trim : 去除空格
select trim(' Hello MySQL ');
# G. substring : 截取子字符串
select substring('Hello MySQL',1,5);
由于业务需求变更,企业员工的工号,统一为 5 位数,目前不足 5 位数的全部在前面补 0 .比如: 1 号员工的工号应该为00001
update emp set workno = lpad(workno, 5, '0');
数值函数

# A. ceil:向上取整
select ceil(1.1);
# B. floor:向下取整
select floor(1.9);
# C. mod:取模
select mod(7,4);
# D. rand:获取随机数
select rand();
# E. round:四舍五入
select round(2.344,2);
通过数据库的函数,生成一个六位数的随机验证码
select lpad(round(rand()*1000000 , 0), 6, '0');
思路:获取随机数可以通过rand() 函数,但是获取出来的随机数是在 0-1 之间的,所以可以在其基础
上乘以 1000000 ,然后舍弃小数部分,如果长度不足 6 位,补 0
日期函数

# A. curdate:当前日期
select curdate();
# B. curtime:当前时间
select curtime();
# C. now:当前日期和时间
select now();
# D. YEAR , MONTH , DAY:当前年、月、日
select YEAR(now());
select MONTH(now());
select DAY(now());
# E. date_add:增加指定的时间间隔
select date_add(now(), INTERVAL 70 YEAR );
# F. datediff:获取两个日期相差的天数
select datediff('2021-10-01', '2021-12-01');
查询所有员工的入职天数,并根据入职天数倒序排序
思路: 入职天数,就是通过当前日期 - 入职日期,所以需要使用 datediff 函数来完成
select name, datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays
desc;
流程函数

# A. if
select if(false, 'Ok', 'Error');
# B. ifnull
select ifnull('Ok','Default');
select ifnull('','Default');
select ifnull(null,'Default');
# C. case when then else end
select
name,
( case workaddress when '北京' or '上海' then '一线城市'else
'二线城市' end ) as '工作地址'
from emp;
约束
概念:约束是作用于表中字段上的规则,用于限制存储在表中的数据
目的:保证数据库中数据的正确、有效性和完整性
主键约束

建表时提供约束
CREATE TABLE tb_user(
id int AUTO_INCREMENT PRIMARY KEY COMMENT 'ID唯一标识',
name varchar(10) NOT NULL UNIQUE COMMENT '姓名' ,
age int check (age > 0 && age <= 120) COMMENT '年龄' ,
status char(1) default '1' COMMENT '状态',
gender char(1) COMMENT '性别'
);

外键约束
创建两表


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 empp(
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 empp (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). 添加外键
CREATE TABLE 表名 (
字段名 数据类型 ,
…
[CONSTRAINT] [ 外键名称 ] FOREIGN KEY ( 外键字段名 ) REFERENCES 主表 ( 主表列名 )
);
ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY ( 外键字段名 )
REFERENCES 主表 ( 主表列名 ) ;
为 empp 表的 dept_id 字段添加外键约束 , 关联 dept 表的主键 id
alter table empp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
2). 删除外键
ALTER TABLE 表名 DROP FOREIGN KEY 外键名称 ;
alter table emp drop foreign key fk_emp_dept_id;
删除**/**更新行为

语法
ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY ( 外键字段 ) REFERENCES
主表名 ( 主表字段名 ) ON UPDATE CASCADE ON DELETE CASCADE;
由于 NO ACTION 是默认行为,我们前面语法演示的时候,已经测试过了,就不再演示了,这里我们再演示其他的两种行为:CASCADE 、 SET NULL .
1.CASCADE
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on
update cascade on delete cascade ;
现象是主副表同步
2.SET NULL
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on
update set null on delete set null ;
我们删除id为1的数据,发现父表的记录是可以正常的删除的,父表的数据删除之后,再打开子表 empp,我们发现子表empp 的dept_id字段,原来dept_id为1的数据,现在都被置为NULL了.
over
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- [MATLAB] Image Processing - Recognition of Traffic Signs
- Flink PostgreSQL CDC configuration and FAQ
- Flink CDC implements Postgres to MySQL streaming processing transmission case
- Misc-traffic analysis of CTF
- Competition WP in May
- A Spark task tuning 】 【 one day suddenly slow down how to solve
- POI工具类
- 【数仓】数据仓库高频面试题题英文版(1)
- C#中default关键字用法简介
- FastAPI 快速入门
猜你喜欢

Remember a Mailpress plugin RCE vulnerability recurrence

在线sql编辑查询工具sql-editor
文件上传漏洞的绕过

Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer
![[Net Ding Cup 2020 Qinglong Group] AreUSerialz](/img/f2/9aef8b8317eff31af2979b3a45b54c.png)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz

Communication middleware Fast DDS basic concepts and communication examples
![[MATLAB] Image Processing - Recognition of Traffic Signs](/img/45/1a5797a17ebf6db965a64c85e0f037.png)
[MATLAB] Image Processing - Recognition of Traffic Signs

npm run serve启动报错npm ERR Missing script “serve“

SSTI range
CTF之misc-文件隐写
随机推荐
通信中间件 Fast DDS 基础概念简述与通信示例
torch distributed training
Arrays工具类的使用
【十年网络安全工程师整理】—100渗透测试工具使用方法介绍
CTF之misc-日志分析
Dcat Admin 安装
批量自动归集
uni-app: about custom components, easycom specs, uni_modules, etc.
Using custom annotations, statistical method execution time
【MySQL功法】第5话 · SQL单表查询
Using PyQt5 to add an interface to YoloV5 (1)
C#预定义数据类型简介
uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error
Flink PostgreSQL CDC configuration and FAQ
MySQL storage engine
JDBC programming of MySQL database
FastAPI 快速入门
Operators and Interaction Basics
vulnhub-XXE ctf security question
C# WPF中监听窗口大小变化事件