当前位置:网站首页>【MySQL】如何向mysql数据库插入当前时间
【MySQL】如何向mysql数据库插入当前时间
2022-07-29 17:26:00 【Cappuccino-jay】
-- Mysql中写法:
-- 直接调用NOW()函数就能获取当前时间,然后直接执行插入即可
-- 例: 2022-07-31 12:45:34
insert into table (id,create_time) values(1,NOW());
-- 或者使用CURDATE(),获取的是当前"年-月-日"时间
-- 例: 2022-07-31
insert into table (id,create_time) values(2,CURDATE());
-- 或者使用CURTIME(),获取的是当前"时:分:秒"时间
-- 例: 12:45:34
insert into table (id,create_time) values(3,CURTIME());
-- Oracle中写法:
-- 使用SYSDATE,获取的是当前的“年-月-日 时:分:秒”时间
--例: 2022-7-30 12:11:11
insert into table (id,create_time) values(4,SYSDATE);
MySQL中的CURRENT_TIMESTAMP
- DEFAULT CURRENT_TIMESTAMP
表示当插入数据的时候,该字段默认值为当前时间 - ON UPDATE CURRENT_TIMESTAMP
表示每次更新这条数据的时候,该字段都会更新成当前时间
CREATE TABLE `jsontest` (
`id` bigint NOT NULL AUTO_INCREMENT,
`comments` varchar(255) DEFAULT '' COMMENT '内容',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
//如果想设置一个具体的默认时间可以这样:
CREATE TABLE `jsontest2` (
`id` bigint NOT NULL AUTO_INCREMENT,
`comments` varchar(255) DEFAULT '' COMMENT '内容',
`create_time` timestamp DEFAULT '2022-7-30 12:11:11' COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
边栏推荐
- leetcode53 -- 最大数组和
- leetcode94 -- 二叉树的中序遍历
- 阿里最新发布的《Alibaba分布式系统速成笔记》PDF版,供下载
- ng组件注册及使用
- The difference between firmware, driver and software
- Interviewer: How does MySQL tune SQL statements based on execution plans?
- 【英语考研词汇训练营】Day 17 —— espresso,ultimate,gradually,detect,dimension
- [STM32CubeMX] STM32H743 configuration IAP upgrade
- 周末分享-关于微信生态变化和5G
- Pagination with LIMIT
猜你喜欢
随机推荐
【英语考研词汇训练营】Day 17 —— espresso,ultimate,gradually,detect,dimension
Learn to arthas, 3 years experience with 5 years work force you!
华中农大团队提出:一种基于异构网络的方法,可自动提取元路径,预测药物-靶标相互作用
浅谈智能家居应用及传输方式
【WSL】wsl pip NewConnectionError
蓝色社交图标登录页面
UNIX环境高级编程第三章
Interviewer: How does MySQL tune SQL statements based on execution plans?
leetcode141 -- 环形链表
机器学习:知道模型评估中的SSE、“肘”部法、SC系数和CH系数的实现原理
Dialogue with Academician Yu Fei of the Canadian Academy of Engineering: Looking for "Shannon's Theorem" in the field of AI
传统渲染农场和云渲染农场选择哪个好?
hihoCoder #1143 : 骨牌覆盖问题·一
多线程并发Callable
脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
The problem that crontab executes scheduled tasks and reports errors
IDEA远程调试
The structure of the earth's over 200 million proteins is fully predicted, and AlphaFold detonates the "protein universe"
解决 @RefreshScope 导致定时任务注解 @Scheduled 失效
One's deceased father grind English vocabulary training camp Day 17 】 -- espresso, ultimate, gradually, detect, dimension









