当前位置:网站首页>MySQL8.0学习记录08 - 数据类型默认值
MySQL8.0学习记录08 - 数据类型默认值
2022-04-22 13:12:00 【wisfy_21】
定义默认值的方式
类型默认值可以使用字面量和表达式来定义,表达式需要用括号括起来:
create table t_text(
a int default 0,
b varchar(64) DEFAULT (UUID()),
c DATE DEFAULT (CURRENT_DATE + INTERVAL 1 YEAR),
d float default (RAND() * RAND()),
e DATETIME default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
f BLOB default ('hello')
);
insert into t_text() values();
SELECT * FROM t_text;
a|b |c |d |e |f |
-+------------------------------------+----------+--------+-------------------+-----+
0|66f735d9-c17c-11ec-89b9-0242ac120003|2023-04-21|0.496599|2022-04-21 14:07:38|hello|
日期类型有个可以自动更新的属性 ON UPDATE,在更新其他列值的时候,该列自动更新
update t_text set a = 1 where a=0;
SELECT * FROM t_text;
可以看到时间e更新了:
a|b |c |d |e |f |
-+------------------------------------+----------+--------+-------------------+-----+
1|66f735d9-c17c-11ec-89b9-0242ac120003|2023-04-21|0.496599|2022-04-21 14:08:23|hello|
表达式的要求
- 可以使用字面量、内置函数以及操作符
- 不能使用子查询、参数、变量、存储函数
- 列不能拥有AUTO_INCREMENT属性
- 表达式可以引用其他列,但是如果引用生成列或者带有表达式默认值的列,被引用的列要先定义
一般列表达式引用其他列的顺序无限制:
create table t_text(
a int default 0,
b int DEFAULT (a + 2),
c varchar(10) default(concat(d,'_c')),
d varchar(10) default 'd'
);
insert into t_text() values();
/* a|b|c |d| 0|2|d_c|d| */
insert into t_text(a,d) values(100,'hello');
/* a |b |c |d | 100|102|hello_c|hello| */
如下,d的默认值是表达式,c在d前面定义,c的表达式引用d是不合法的:
create table t_text(
a int default (0),
b int default (a+10), --正确
c varchar(10) default(concat(d,'_c')), -- 错误
d varchar(10) default ('d')
);
另外:BLOB, TEXT, GEOMETRY和 JSON只能拥有带表达式的默认值
隐式默认处理
如果未显式指定DEFAULT,而且类型可以为NULL,那么MySQL也会加上DEFAULT NULL。
对于定义NOT NULL的列,如果在INSERT、REPLACE、UPDATE语句中,给该列赋值为NULL,那么根据SQL模式进行相应的处理:
- 如果是严格模式,会报错,并回滚事务
- 如果是非严格模式,那么根据数据类型来决定其默认值:
create table t_text(
a int not null,
b varchar(10) not null,
c decimal(4,2) not null,
d datetime not null,
e BLOB not null,
f JSON not null
);
SET sql_mode = '';
insert into t_text() values();
结果如下:
a|b|c |d |e|f |
-+-+----+-------------------+-+----+
0| |0.00|0000-00-00 00:00:00| |null|
这里注意f 并不是MySQL的NULL类型,而是JSON的NULL。
版权声明
本文为[wisfy_21]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wisfy_21/article/details/124333093
边栏推荐
- Redis advantages and disadvantages usage scenarios
- mysql数据库已经成功启动,可是show不是内部或外部命令,该如何解决呢?
- Leetcode 1678. Design goal parser
- General steps for exporting Gerber files from Altium Designer
- R language uses rnbinom function to generate random numbers conforming to negative binomial distribution, and uses plot function to visualize random numbers conforming to negative binomial distributio
- There are four ways to traverse the mat class matrix elements of OpenCV
- HDU 2544 Dijkstra(模板)
- Ros2 - teach you how to write a service
- The difference between let, const and VaR
- From construction to governance, the industry's first white paper on microservice governance technology was officially released (including a free download link)
猜你喜欢

Scratch编程入门

Redis如何查看单个key所占用的内存大小

The data in MATLAB is forcibly converted from double type to uint8, which is rounded, and the basic data type of MATLAB is attached

数商云集中采购系统 :集中采购内外协同, 最大程度减少异常支出

CubeMX配置SPI-Flash(W25Q256)

Model based RL概述

ROS机器人学习——麦克纳姆轮运动学解算

Ros2 - what is an interface

Use opencv's function threshold () to threshold the image based on Otsu - and attach a good blog link to introduce the principle of Otsu

Oracle netsuite customers say | the "core secret script" for more detailed process control of China Film Barco
随机推荐
OPLG:新一代云原生可观测最佳实践
学习笔记——数字化工厂 4.21
Array and string offset access syntax with curly braces is deprecated
redis配置数据库数量
浅学一下Shell脚本(5)--函数,随机数,正则表达式
Day code 300 lines learning notes day 47
Download and installation of redis
树莓派压缩备份
When doing correlation analysis, how to exclude singular value outliers to increase the accuracy of correlation analysis
生态 | 万里数据库与溢信科技完成兼容认证
做相关性分析时,如何排除奇异值Outliers,以增加相关分析的准确性
C whether the administrator has the authority to run the software
Redis local connection to view data
How to add pictures as program resources in C #
MPU6050-DMP读不出数据
Wong Kwong Yu Kwai tiktok is unable to sleep soundly.
最大匹配数,最小路径覆盖数,最大独立数,最小点覆盖数 定理总结
R language uses dhyper function to generate hypergeometric distribution density data and plot function to visualize hypergeometric distribution density data
Sprint strategy for soft test preparation in the first half of 2022
Excel表格中如何批量删除工作表