mysql 建立日期列之timestamp

2021-09-03 10:45:55 字數 3367 閱讀 9293

mysql 有很多日期格式

這裡僅說明timestamp的應用

mysql> create table t1(id int,b timestamp default current_timestamp);

query ok, 0 rows affected (0.19 sec)

mysql> show create table t1\g

*************************** 1. row ***************************

table: t1

create table: create table `t1` (

`id` int(11) default null,

`b` timestamp not null default current_timestamp

) engine=innodb default charset=utf8

1 row in set (0.00 sec)

mysql> insert into t1(id) values(1);

query ok, 1 row affected (0.02 sec)

mysql> insert into t1(id) values(2);

query ok, 1 row affected (0.02 sec)

mysql> select * from t1;

+------+---------------------+

| id | b |

+------+---------------------+

| 1 | 2013-05-20 06:44:07 |

| 2 | 2013-05-20 06:44:08 |

+------+---------------------+

2 rows in set (0.00 sec)

2.更新id的同時更新時間  

mysql> create table t2(id int,b timestamp default current_timestamp on update current_timestamp)engine=innodb;

query ok, 0 rows affected (0.19 sec)

mysql> show create table t2\g

*************************** 1. row ***************************

table: t2

create table: create table `t2` (

`id` int(11) default null,

`b` timestamp not null default current_timestamp on update current_timestamp

) engine=innodb default charset=utf8

1 row in set (0.00 sec)

mysql> insert into t2(id) values(1);

query ok, 1 row affected (0.02 sec)

mysql> insert into t2(id) values(2);

query ok, 1 row affected (0.02 sec)

mysql> select * from t2;

+------+---------------------+

| id | b |

+------+---------------------+

| 1 | 2013-05-20 06:46:39 |

| 2 | 2013-05-20 06:46:43 |

+------+---------------------+

2 rows in set (0.00 sec)

mysql> update t2 set id=2 where id=1;

query ok, 1 row affected (0.02 sec)

rows matched: 1 changed: 1 warnings: 0

mysql> select * from t2;

+------+---------------------+

| id | b |

+------+---------------------+

| 2 | 2013-05-20 06:47:15 |

| 2 | 2013-05-20 06:46:43 |

+------+---------------------+

2 rows in set (0.00 sec)

mysql> update t2 set id=3 where b='2013-05-20 06:47:15';

query ok, 1 row affected (0.02 sec)

rows matched: 1 changed: 1 warnings: 0

mysql> select * from t2;

+------+---------------------+

| id | b |

+------+---------------------+

| 3 | 2013-05-20 06:47:59 |

| 2 | 2013-05-20 06:46:43 |

+------+---------------------+

2 rows in set (0.00 sec)

mysql> update t2 set id=3 where id=3;

query ok, 0 rows affected (0.00 sec)

rows matched: 1 changed: 0 warnings: 0

mysql> select * from t2;

+------+---------------------+

| id | b |

+------+---------------------+

| 3 | 2013-05-20 06:47:59 |

| 2 | 2013-05-20 06:46:43 |

+------+---------------------+

2 rows in set (0.00 sec)

插入預設當前時間,當對表字段進行修改的時候,自動更新時間,如果表字段的值更新之前與更新之後沒有變化,則時間也不會發生變更。

Mysql行轉列,列轉行,日期

學生表 student name class 小花1男 小明1女小王 1女小孫2 男小張2男 小李2那小紅 3女小草3 女查詢每個班級的男生和女生的數量 select class,sum case when 女 then 1else 0end 女,sum case when 男 then 1else...

mysql基礎 mysql列型別 時間和日期

mysql列型別 整型 mysql列型別 字串 date 日期。支援的範圍為 1000 01 01 到 9999 12 31 mysql以 yyyy mm dd 格式顯示date值,但允許使用字串或數字為date列分配值。datetime 日期和時間的組合。支援的範圍是 1000 01 01 00 ...

mysql 建立複製列 MySQL建立表的三大方式

1.table普通建立 mysql use web 選擇要使用的資料庫 mysql create table a1 id int name char 30 建立 a1 表,並新增 id 和 name 字段以及型別 mysql describe a1 檢視表結構 字段 2.複製建立 create ta...