MySQL之函数与约束

MySQL内置函数

  • 字符串函数
    concat(S1,S2…,Sn)
    lower(str)
    upper(str)
    l/rpad(str,n,pad) #左/右填充
    trim(str) #去掉头部和尾部的空格
    substring(str,start,len) #返回str从start开始的len长度的字符串
  • 数值函数
    ceil(x)
    float(x)
    mod(x,y) #x/y的模
    rand() #返回(0,1)的随机数
    round(x,y) #求x的四舍五入值,保留y位小数
  • 日期函数
    curdate()
    curtime()
    year(date)
    month(date)
    day(date)
    date_add(date,interval_expr type)
    datediff(date1,date2)
  • 流程函数
    if(value,t,f) #value为真返回t
    ifnull(value1,value2) #如果value1不为空,返回value1,否则返回value2
    case when [val1] then [res1] … else [default] end #val为真,返回res1 …否则返回default
    case [expr] when [val1] then [res1] … else [default] #expr的值等于val,返回res1 …否则返回default
    案例:
select 
	id,
	name,
	(case when math>=85 then '优秀' when math>=60 then '及格' else '不及格' end) as '数学'
from score;

约束(常见约束)

  • not null:非空
  • unique :唯一
  • primary key:主键约束,非空且唯一
  • default:默认
  • check:检查约束,保证字段值满足某一条件
  • foreign key:外键约束,建立两张表的连接,有外键的称为子表
--添加外键  cascade级联  set null设置子表外键为null 
alter table 表名 add constraint 外键名称 foreign key(外键字段名) references 主表(主表列名) on update cascade on delete cascade;
--删除外键
alter table 表名 drop foreign key 外键名称
  • auto_increment:自动增长
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值