当前位置:网站首页>mysql automatically adds creation time and update time
mysql automatically adds creation time and update time
2022-07-31 11:07:00 【m0_54853503】
A climber who is aiming at the top will not be intoxicated by a certain footprint along the way. In the world of code farmers, the beautiful application experience comes from the programmer's handling of details and the realm of self-requirement. Young peopleHe is also one of the busy code farmers. Every day and every week, he leaves some footprints. It is the content of these creations. There is a kind of persistence.
1 Description
In mysql, when the column type in the added table is the time type (timestamp), the default value can be set
Set the default value of the time column to automatically get the creation time:
default CURRENT_TIMESTAMP
Set the default value of the time column to get the update time automatically:
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
Add another value that cannot be null
#Create timenot null default CURRENT_TIMESTAMP# update timenot null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
2 full operational SQL
2.1 Modify the default value of the time column in the existing table to be obtained automatically
Modify the create_time column in the table t_user to set the current system time if the value is empty when inserting new data
#Modify the create_time column in the table t_user When inserting new data, if the value is empty, it will be set to the current system timeALTER TABLE t_user MODIFY create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create time';
Modify the update_time column in the table t_user and set the current system time if the value is empty when modifying the data
#Modify the update_time column in the table t_user When modifying the data, if the value is empty, it will be set to the current system timeALTER TABLE t_user MODIFY update_time timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Update time';
2.2 Add a time column to an existing table to set the default value
Add create_time column in table t_user
#Add create_time column in table t_userALTER TABLE t_user ADD create_time timestamp not null default CURRENT_TIMESTAMP COMMENT 'create time';
Add update_time column in table t_user
#Add update_time column in table t_userALTER TABLE t_user ADD update_time timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Update time';
2.3 Set the default value of the time column when creating a table
create table t_user(id integer not null auto_increment primary key,user_name varchar(20) not null ,update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Update time',create_time timestamp default CURRENT_TIMESTAMP COMMENT 'create time');
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- mpu9150(driverack pa简明教程)
- In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
- 透过开发抽奖小程序,体会创新与迭代
- 3D激光SLAM:LeGO-LOAM论文解读---完整篇
- 2022/7/28
- 【Web技术】1397- 深入浅出富文本编辑器
- [Go Affair] See through Go's collections and slices at a glance
- 初始JDBC 编程
- Can I find a Go job in 7 days?Learn Go with arrays and pointers
- 拥抱趋势!阿里这套微服务开源框架权威手册,实战到底层细致清晰
猜你喜欢
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
KVM virtualization job
新人学习小熊派华为iot介绍
sql力扣刷题八
FCN中制作自己的数据集并进行训练
初始JDBC 编程
Experience innovation and iteration through the development of a lucky draw applet
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
Insertion and deletion of doubly linked list
随机推荐
Unix知识:shell详细解读
pycharm汉化教程(碧蓝幻想汉化插件安装)
Can I find a Go job in 7 days?Learn Go with arrays and pointers
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
Deletion of the sequence table
准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
“chmod 777-R 文件名”什么意思?
redis-enterprise use
【LeetCode】36.有效的数独
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
sql力扣刷题八
拥抱趋势!阿里这套微服务开源框架权威手册,实战到底层细致清晰
双链表的插入和删除
【软件工程之美 - 专栏笔记】33 | 测试工具:为什么不应该通过QQ/微信/邮件报Bug?
Detailed explanation of SQL stored procedures
「MySQL」- 基础增删改查
5 个开源的 Rust Web 开发框架,你选择哪个?
SQL存储过程详解
AWS亚马逊云账号注册,免费申请12个月亚马逊云服务器详细教程
deeplab实现自己遥感地质分割数据集