当前位置:网站首页>MySQL - 函数及约束命令
MySQL - 函数及约束命令
2022-07-26 17:52:00 【NO.-LL】
目录
函数
字符串函数

# 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);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');日期函数

# 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');
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 主表 (主表列名) ;
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;
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
边栏推荐
- Real passwords do not match during synchronization
- 探索式软件测试
- Module 8 job message data MySQL table design
- 项目中@RequestMapping的作用以及如何使用
- NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
- Still using xshell? Recommend this more modern terminal connection tool
- 数据安全知识体系
- NFT数字藏品系统开发:同道大叔首推祈福系列数字藏品开售即罄
- Vector CANape - How to Send Receive CAN Message in CANape
- 【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
猜你喜欢

【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引

Oracle day 2 (Views, indexes, PLSQL, cursors, stored procedures and stored functions, triggers, JDBC access stored procedures and stored functions)

NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately

Vector canoe menu plugin getting started

神经网络学习(2)前言介绍二

FTP协议

更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了

The class jointly built by famous oarsmen is new, and Professor qiuxipeng of Fudan University broadcast it live on Tuesday!

Have you ever encountered a deadlock problem in MySQL? How did you solve it?

NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
随机推荐
Brand new! Uncover the promotion route of Ali P5 Engineer ~p8 architect
神经网络学习(2)前言介绍二
Exploratory software testing
Arm China responded to the "disconnection of Huawei supply" incident! Ren Zhengfei said "no impact"!
Online stock trading, where to choose to open an account is safer?
任正非首度揭秘:华为差点100亿美元“卖身”摩托罗拉背后的故事!
MySQL exercises elementary 45 questions (Unified table)
[add conditional behavior in kotlin]
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
Netease game R & D Engineer Intern (client side)
Duplicate gallerycms character length limit short domain name bypass
如何成为一名优秀的测试/开发程序员?专注谋定而后动......
被控专利授权费过高!美法院判决高通违反反垄断法:高通股价大跌10.86%!
Sentinel 隔离与降级
自动化测试工具-Playwright(快速上手)
Arrangement of information security emergency plan
Interview summary of some large factories
SSM integration configuration
NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections
详细介绍@GetMapping和@PostMapping的区别