当前位置:网站首页>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
边栏推荐
- SQLSERVER merges subquery data into one field
- 【LeetCode】383.赎金信
- oracle优化:instr做join条件很慢「建议收藏」
- Threading(in thread main)
- 2022/7/30
- xmind使用指南(XMind具有下列哪些功能)
- Usage of exists in sql
- Yarn安装配置(vsftpd安装配置)
- [Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
- 结构化查询语言SQL-关系数据库标准语言
猜你喜欢
随机推荐
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
"JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
【LeetCode】383.赎金信
透过开发抽奖小程序,体会创新与迭代
Three ways of single sign-on
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
Android studio连接MySQL并完成简单的登录注册功能
矩形脉冲波形的占空比及脉冲和瞬态特征的测量
CoCube群机器人预览→资讯剧透←
Windows安装mysql详细步骤(通俗易懂,简单上手)
逆置问题--重点
实现弹框组件
Make your own dataset in FCN and train it
unity computeshader的可读写buffer
KVM virtualization job
Insertion and deletion of doubly linked list
《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
[ 图 论 ]二分图判定及其匹配(基础+提高)
Acwing-考研机试题