当前位置:网站首页>MySQL8. 0 learning record 08 - data type default
MySQL8. 0 learning record 08 - data type default
2022-04-22 13:13:00 【wisfy_ twenty-one】
How to define default values
Type defaults can be defined using literals and expressions , The expression needs to be enclosed in parentheses :
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|
The date type has a property that can be updated automatically ON UPDATE, When updating other column values , The column is automatically updated
update t_text set a = 1 where a=0;
SELECT * FROM t_text;
You can see the time e Updated :
a|b |c |d |e |f |
-+------------------------------------+----------+--------+-------------------+-----+
1|66f735d9-c17c-11ec-89b9-0242ac120003|2023-04-21|0.496599|2022-04-21 14:08:23|hello|
Expression requirements
- Literally 、 Built in functions and operators
- Cannot use subquery 、 Parameters 、 Variable 、 Storage function
- Column cannot have AUTO_INCREMENT attribute
- Expressions can reference other columns , But if you reference the generated column or the column with the default value of the expression , The referenced column must be defined first
The general list expression refers to other columns in an unlimited order :
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| */
as follows ,d The default value of is the expression ,c stay d The previous definition ,c Expression reference for d It's illegal :
create table t_text(
a int default (0),
b int default (a+10), -- correct
c varchar(10) default(concat(d,'_c')), -- error
d varchar(10) default ('d')
);
in addition :BLOB, TEXT, GEOMETRY and JSON You can only have default values with expressions
Implicit default processing
If not explicitly specified DEFAULT, And the type can be NULL, that MySQL It will also add DEFAULT NULL.
For definition NOT NULL The column of , If in INSERT、REPLACE、UPDATE In the sentence , Assign a value of... To this column NULL, So according to SQL The mode is processed accordingly :
- If it's a strict model , Will report a mistake , And roll back the transaction
- If it's a non strict model , Then the default value is determined according to the data type :
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();
give the result as follows :
a|b|c |d |e|f |
-+-+----+-------------------+-+----+
0| |0.00|0000-00-00 00:00:00| |null|
Note here f Not at all MySQL Of NULL type , It is JSON Of NULL.
版权声明
本文为[wisfy_ twenty-one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221311549361.html
边栏推荐
- OPLG:新一代云原生可观测最佳实践
- ROS2——什么是接口
- 分块——优雅的暴力
- The sales volume is cut by half, and there is no blueprint for lantu
- Ros2 - use of parameters
- no main manifest attribute / .jar中没有主清单属性
- Ros2 - teach you to write a topic hand in hand
- Redis advantages and disadvantages usage scenarios
- Sprint strategy for soft test preparation in the first half of 2022
- The R language uses the rowsums function to calculate the row data sum value of all data rows in the dataframe
猜你喜欢

ROS机器人学习——TF坐标变换

How to use colormaps and customize your favorite colorbar?

STM32CubeMX重定向printf输出至串口

【黑马早报】知乎今日在港上市;小红书回应裁员20%;王者荣耀被指控抄袭;刘畊宏直播收入10天涨10倍;“知网反垄断第一案”已立案...

Oracle NetSuite 客户说 | 让中影巴可流程控制更精细的“核心秘籍”

R语言绘制小提琴图geom_violin,如何添加额外的点geom_point?geom_violin + geom_boxplot + geom_point组合使用

Model based RL概述

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

学习笔记——数字化工厂 4.21

The importance of the production cost of promotional films of scientific and technological products!
随机推荐
Mpu6050-dmp cannot read data
CMD command and script learning notes_ Kali_ Station B
Redis如何查看单个key所占用的内存大小
Stm32cubemx redirects printf output to serial port
redis 更新升级版本
Get rid of the "small workshop" of AI production: how to build a cloud native AI platform based on kubernetes
Graph search of obstacles in far planner
数商云:数字化采购浪潮下,企业如何实现局部突破,小步快跑
The R language uses the qnorm function to generate the positive distribution quantile function data, and uses the plot function to visualize the positive distribution quantile function data (normal di
各省GTFP绿色全要素生产率面板数据(2004-2018年)
Drawing violin picture with R language geom_ Violin, how to add additional points geom_ point? geom_ violin + geom_ boxplot + geom_ Point combination
英语写作神器Quillbot---如何使用免费的Premium功能
Corners of enterprise mailbox
How to become an open source database developer?
Walking in the clouds - but there are books
浅学一下Shell脚本(5)--函数,随机数,正则表达式
Oracle NetSuite 客户说 | 让中影巴可流程控制更精细的“核心秘籍”
ROS2——什么是接口
ROS2——手把手教你编写一个话题
Explain in detail why the number of pixels with a gray value of 255 calculated by the opencv histogram calculation function calchist() is 0