MySQL INT 型別可以為非零 NULL 嗎?


你可以設定 INT 列為 NULL 值。INT 型別列為可空列。語法如下

INSERT INTO yourTableName(yourIntColumnName) values(NULL);

為了理解上述語法,我們建立一個表。建立表的查詢如下

mysql> create table nullableIntDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Price int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.80 sec)

將 int 列‘Price’的記錄插入為 NULL。查詢如下

mysql> insert into nullableIntDemo(Price) values(NULL);
Query OK, 1 row affected (0.11 sec)
mysql> insert into nullableIntDemo(Price) values(100);
Query OK, 1 row affected (0.15 sec)
mysql> insert into nullableIntDemo(Price) values(200);
Query OK, 1 row affected (0.12 sec)
mysql> insert into nullableIntDemo(Price) values(NULL);
Query OK, 1 row affected (0.13 sec)
mysql> insert into nullableIntDemo(Price) values(NULL);
Query OK, 1 row affected (0.10 sec)

使用 select 語句顯示錶中的所有記錄。查詢如下

mysql> select *from nullableIntDemo;

以下是輸出

+----+-------+
| Id | Price |
+----+-------+
|  1 | NULL  |
|  2 | 100   |
|  3 | 200   |
|  4 | NULL  |
|  5 | NULL  |
+----+-------+
5 rows in set (0.00 sec)

檢視以上樣本輸出中的 MySQL int 列是可空的。

更新於: 2019-07-30

209 次瀏覽

開啟您的 職業生涯

完成課程可獲得認證

開始
廣告
© . All rights reserved.