MySQL 設定 建立時間 和 更新時間

2021-10-21 20:20:08 字數 1416 閱讀 6623

在實際應用中,我們時常會需要用到建立時間和更新時間這兩個字段,下面記錄一下:

create table `user_info`  (

`uid` varchar(255) character set utf8 collate utf8_general_ci not null,

`username` varchar(255) character set utf8 collate utf8_general_ci not null,

`password` varchar(255) character set utf8 collate utf8_general_ci not null,

`telephone` varchar(11) character set utf8 collate utf8_general_ci null default null,

`create_time` timestamp(0) not null default current_timestamp comment '建立時間',

`update_time` timestamp(0) null default null on update current_timestamp(0) comment '更新時間',

修改create_time 設定預設時間 current_timestamp 

alter table `user_info`

modify column  `create_time` datetime null default current_timestamp comment '建立時間' ;

新增update_time 字段

設定 預設時間 current_timestamp   設定更新時間為 on update current_timestamp 

alter table `user_info`

add column `update_time` datetime null default current_timestamp on update current_timestamp comment '更新時間' ;

Mysql 建立時間和更新時間

分三種情況 create table mytest text varchar 255 default comment 內容 直接指定建立時間和更新時間 create time timestamp not null default current timestamp comment 建立時間 upda...

mysql自動管理資料的建立時間和更新時間

首先在建立表的時候 create table testmysqltime id int primary key auto increment comment 主鍵id username varchar 10 not null comment 使用者名稱 password varchar 10 not...

MySql建立時間及更新時間的智慧型更新

一條記錄一般會有建立時間跟更新時間 建立時間為第一次寫資料時填入,更新時間則記錄資料的最後修改時間 我們在資料庫設定了唯一索引,想基於唯一索引來批量插入更新資料,一般可以用replace 或者 on duplicate key replace的原理是先根據唯一索引檢查有無資料,沒有的插入,有的先刪除...