当前位置:网站首页>【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;
边栏推荐
- 蓝色社交图标登录页面
- 4G无线模块 电力通信模块
- NFTScan and PANews jointly release multi-chain NFT data analysis report
- canvas随机生成树木js特效
- 阿里最新发布的《Alibaba分布式系统速成笔记》PDF版,供下载
- Learn to arthas, 3 years experience with 5 years work force you!
- The Huazhong Agricultural University team proposes: a heterogeneous network-based method that can automatically extract meta-paths and predict drug-target interactions
- 【回忆】奶奶的歌谣
- Interviewer: How does MySQL tune SQL statements based on execution plans?
- Six basic experiments of STC8h1k28
猜你喜欢
![[Code Hoof Set Novice Village 600 Questions] Detailed explanation of pow() function](/img/d7/c57caa31c16f2e8f7bcd0003cb2791.png)
[Code Hoof Set Novice Village 600 Questions] Detailed explanation of pow() function

【南瓜书ML】(task5)支持向量机的数学推导(更新ing)

Six basic experiments of STC8h1k28

要卖课、要带货,知识付费系统帮你一步搞定!

【Mysql系列】01_查询+排序

使用LIMIT分页

The Huazhong Agricultural University team proposes: a heterogeneous network-based method that can automatically extract meta-paths and predict drug-target interactions

NFTScan 与 PANews 联合发布多链 NFT 数据分析报告

MySQL数据库基础

xatlas源码解析(七)
随机推荐
【深度学习】使用yolov5对数据进行预标注
分析师:百度到2030年可能成为中国市值最高的公司
NFTScan and PANews jointly release multi-chain NFT data analysis report
【STM32CubeMX】STM32H743配置IAP升级
回帖免责声明-转载
剑指offer专项突击版第14天
【WSL】wsl pip NewConnectionError
[网络]WAN技术 PPP
flink cdc source支持redis 和es吗
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
How different DAOs are changing the world
浅谈智能家居应用及传输方式
In the Chinese ToB market, it is too difficult to choose the right supplier
字典树(Trie)
MySQL数据库基础
unity-shader-游戏渲染效果逆向分析
leetcode53 -- 最大数组和
P4775 [NOI2018] 情报中心(线段树合并)
The difference between firmware, driver and software
hihoCoder#1037 : 数字三角形(DP)