🎊专栏【MySQL】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【如愿】 大一同学小吉,欢迎并且感谢大家指出我的问题🥰 文章目录 🍔
🎊专栏【MySQL】
🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。
🎆音乐分享【如愿】
大一同学小吉,欢迎并且感谢大家指出我的问题🥰

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

select concat('s1','s2'); 
select lower('str'); 
select upper('str'); 
select lpad('str',length,'-'); -- 在str左边用-进行填充,达到长度为n 
select rpad('str',length,'-'); -- 在str右边用-进行填充,达到长度为n 
select trim('str'); 
select substring('str',截取起始位置,截取长度); 
由于业务需求变化,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0
(比如1好员工的工号应该是00001)
update emp set worknumber = lpad(worknumber,5,'0'); -- 更新的字段(工号) 
select ceil(number); 
select floor(number); 
select mod(num1,num2); 
是0~1之间的随机数
select rand(); 
对number进行四舍五入,并且保留length位小数
select round(number,length); 
通过数据库的函数,生成一个六位数的随机验证码
select lpad(round(rand()*1000000,0),6,'0'); 
select curdate(); 
select curtime(); 
select now(); 
select YEAR(date); 
select MONTH(date); 
select DAY(date); 
select date_add(now(),INTERVAL 70 MONTH); 
select datediff('2021-12-01','2022-12-01'); 
查询所有员工的入职天数,并根据入职天数倒序排序
select name datediff(curdate(),entrydate) as 'entrydays' from emp order by entrydays desc; 解释:entrydays是函数的别名,这样子就不用写一串函数了,order by 后面的是排序方式

如果条件表达式的结果是true,那么返回OK,否则返回Error
select if(条件表达式,'OK','Error'); 
select ifnull('OK','Default'); 


select name, ( case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end ) from emp; 概念:约束是作用于表中字段上的规则,用于限制存储在表中的数据
目的:保证数据库中数据的正确,有效性和完整性
分类:

主键约束(Primary Key Constraint):主键约束用于定义一个唯一标识来标识表中的每一行。它要求主键列的值唯一且非空。主键可以由一个或多个列组成。
"column"是指表中的一个字段,"datatype"是数据类型
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... primary key (column1, column2, ...)); 唯一约束(Unique Constraint):唯一约束用于确保表中的某个列或一组列的值是唯一的。唯一约束允许空值(NULL),但对于非空值,要求其在列中是唯一的。
"column"是指表中的一个字段,"datatype"是数据类型
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... unique (column1, column2, ...)); 外键约束(Foreign Key Constraint):外键约束用于建立表与表之间的关联关系。用来让两张表之间建立连接,从而保证数据的一致性和完整性

"column"是指表中的一个字段,"datatype"是数据类型
情况1:表结构没有创建好(直接在表里面进行添加)
CREATE TABLE table_name2 ( column1 datatype primary key, column2 datatype, ... foreign key (column2) references table_name1(column1)); 情况2:表结构创建好了
alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表(主表列名) ; alter table 表名 drop foreign key 外键名称; 检查约束(Check Constraint):检查约束用于限制列中的值必须满足指定的条件。可以使用逻辑运算符、比较运算符和函数等来定义检查约束条件。
"column"是指表中的一个字段,"datatype"是数据类型
CREATE TABLE table_name ( column1 datatype, column2 datatype check (condition), ...); 非空约束(Not Null Constraint):非空约束用于确保表中的某个列不接受空值(NULL)。
"column"是指表中的一个字段,"datatype"是数据类型
CREATE TABLE table_name ( column1 datatype not null, column2 datatype, ...); create table user( id int primary key auto_increment comment '主键', name varchar(10) not null unique comment '姓名', age int check ( age > 0 && age < 30 ) comment '年龄', status char(1) default '1' comment '状态', gender char(1) comment '性别' ) comment '用户表'; 插入数据
insert into user(name,age,status,gender) values ('Tom1','19','1','男'),('Tom2','25','0','男'); 
🥰如果大家有不明白的地方,或者文章有问题,欢迎大家在评论区讨论,指正🥰
来源地址:https://blog.csdn.net/m0_72853403/article/details/131222460
--结束END--
本文标题: 【从删库到跑路】详细讲解MySQL的函数和约束作用
本文链接: https://www.lsjlt.com/news/372199.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0