在 MySQL 中如何建立一個帶有 TIMESTAMP 欄位的表?


您可就此使用 MySQL 的 TIMESTAMP 關鍵字。

讓我們建立一個表 −

mysql> create table demo50
−> (
−> id int not null auto_increment primary key,
−> start_date timestamp default current_timestamp not null,
−> end_date timestamp default current_timestamp not null
−> );
Query OK, 0 rows affected (1.35 sec)

使用 insert 命令向表中插入一些記錄 −

mysql> insert into demo50 values();
Query OK, 1 row affected (0.15 sec)

mysql> insert into demo50(end_date) values('2020−12−21');
Query OK, 1 row affected (0.07 sec)

mysql> insert into demo50(start_date) values('2020−01−01');
Query OK, 1 row affected (0.14 sec)

使用 select 語句從表中顯示記錄 −

mysql> select *from demo50;

這將產生以下輸出 −

+----+---------------------+---------------------+
| id | start_date          | end_date            |
+----+---------------------+---------------------+
|  1 | 2020−11−04 20:54:31 | 2020−11−04 20:54:31 |
|  2 | 2020−11−04 20:54:53 | 2020−12−21 00:00:00 |
|  3 | 2020−01−01 00:00:00 | 2020−11−04 20:55:04 |
+----+---------------------+---------------------+
3 rows in set (0.00 sec)

上次更新於:19-Nov-2020

7K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得證書

開始
廣告