MySQL 中以 SERIAL 作為列名是否已經包含“NOT NULL”約束?


在 MySQL 中,SERIAL 是 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE 的別名。因此,不要在 SERIAL 後使用 NOT NULL,因為它已經被包含在其定義中。

讓我們看一個示例並建立一個表。此處,我們有一個名為“serial”的列 −

mysql> create table DemoTable
(
   Id serial
);
Query OK, 0 rows affected (0.42 sec)

使用 insert 命令在表中插入一些記錄。我們在插入時不包含任何值 −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.06 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.07 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 −

+----+
| Id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
+----+
6 rows in set (0.00 sec)

更新於:2019 年 10 月 4 日

145 次瀏覽

開啟您的職業

完成課程獲得認證

開始學習
廣告
© . All rights reserved.