Hai cách kiểm tra
Cách 1: Dùng DESCRIBE (hoặc DESC)
mysql> desc DemoTable;
Kết quả hiển thị cột Null với giá trị NO nghĩa là NOT NULL, YES nghĩa là cho phép NULL.
+--------------------+--------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------------------+------+-----+---------+----------------+
| StudentId | int(11) | NO | PRI | NULL | auto_increment |
| StudentName | varchar(40) | YES | | NULL | |
| StudentAge | int(11) | NO | | NULL | |
| IsActiveStudent | enum('ACTIVE','INACTIVE')| NO | | NULL | |
| StudentCountryName | varchar(40) | YES | | NULL | |
+--------------------+--------------------------+------+-----+---------+----------------+
Cách 2: Truy vấn information_schema.columns
mysql> select column_name, is_nullable
from information_schema.columns
where table_schema = 'web'
and table_name = 'DemoTable';
Kết quả:
+--------------------+-------------+ | COLUMN_NAME | IS_NULLABLE | +--------------------+-------------+ | StudentId | NO | | StudentName | YES | | StudentAge | NO | | IsActiveStudent | NO | | StudentCountryName | YES | +--------------------+-------------+ 5 rows in set (0.00 sec)
Giải thích: IS_NULLABLE = 'NO' tương đương với NOT NULL.