MySQL自動修改資料的插入和修改時間

2021-10-06 22:55:03 字數 1728 閱讀 9910

建立時指定 

-- 建立表

create table `t_user`

( `id` int(10) unsigned not null auto_increment comment '主鍵id',

`username` varchar(20) not null comment '使用者名稱',

`password` varchar(64) not null comment '密碼',

`name` varchar(20) default null comment '姓名',

`age` tinyint unsigned default null comment '年齡',

`email` varchar(50) default null comment '郵箱',

`create_time` timestamp default current_timestamp comment '插入時間',

`update_time` timestamp default current_timestamp on update current_timestamp comment '修改時間',

primary key `id` (`id`),

unique key `uk_user_username` (`username`)

) default charset = utf8mb4 comment = '使用者表';

後期新增

-- 修改create_time 設定預設時間 current_timestamp 

alter table `t_user` add column `create_time` timestamp default current_timestamp comment '建立時間';

-- 新增update_time 設定預設時間 current_timestamp   設定更新時間為 on update current_timestamp 

alter table `t_user` add column `update_time` timestamp default current_timestamp on update current_timestamp comment '更新時間';

測試

-- 插入資料

insert into `t_user` (`id`, `username`, `password`, `name`, `age`, `email`)

values ('1', 'zhangsan', '123456', '張三', '18', '[email protected]'),

('2', 'lisi', '123456', '李四', '20', '[email protected]');

-- 查詢

select * from t_user;

-- 修改

update t_user set age = age + 1 where username = 'zhangsan';

Mysql 學習筆記( 》插入修改資料二)

use db 建立學生資訊表 create table student sno int unsigned not null auto increment,sname varchar 20 not null,sage tinyint unsigned not null,sbirthday dateti...

《學習 》2插入修改資料

use db 建立學生資訊表 create table student sno int unsigned not null auto increment,sname varchar 20 not null,sage tinyint unsigned not null,sbirthday dateti...

在PB中插入 刪除和修改資料

1 插入資料 在資料庫中插入一條資料使用insert語句,格式如下 insert into 表名 字段列表 values 值列表 不同的字段使用逗號 分隔,並且不包含blob型別的字段 值列表中不同的值之間用逗號分隔,和字段列表中字段的型別對應相容 最好型別相同 並且字元型和日期型取值用引號引起來。...