如果我們向一列型別設為 TIMESTAMP CURRENT_TIMESTAMP 的表中插入空值,會發生什麼情況?


如果我們不在 INSERT 語句中插入任何內容,那麼對於時間戳型別,它將插入當前日期時間。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserLoginDate timestamp default current_timestamp
   -> );
Query OK, 0 rows affected (0.61 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable(UserId) values(null);
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+--------+---------------------+
| UserId | UserLoginDate       |
+--------+---------------------+
|      1 | 2019-11-03 12:55:44 |
|      2 | 2019-11-03 12:55:45 |
|      3 | 2019-11-03 12:55:46 |
|      4 | 2019-11-03 12:56:06 |
+--------+---------------------+
4 rows in set (0.00 sec)

更新於: 2019 年 12 月 17 日

227 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.