-- 创建一个包含 current_timestamp 的表
CREATE TABLE example (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- 插入一条记录,created_at 和 updated_at 会自动设置为当前时间
INSERT INTO example (name) VALUES ('Test');
-- 查询表中的数据
SELECT * FROM example;
-- 更新记录时,updated_at 会自动更新为当前时间
UPDATE example SET name = 'Updated Test' WHERE id = 1;
-- 再次查询表中的数据
SELECT * FROM example;
DEFAULT CURRENT_TIMESTAMP:表示在插入记录时,如果没有显式提供 created_at 和 updated_at 的值,则会自动使用当前时间。ON UPDATE CURRENT_TIMESTAMP:表示在更新记录时,updated_at 字段会自动更新为当前时间。上一篇:mysql uuid函数
下一篇:mysql grant
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站