检查表的字段是否在SQL中设置了NOT NULL属性?
要检查表的字段是否具有NOTNULL属性,可以使用两种语法中的任何一种。第一种语法如下-
desc yourTableName;
以下是第二种语法-
select column_name, is_nullable from information_schema.columns where table_schema = ‘yourDatabaseName’ and table_name = 'yourTableName’;
我们首先来看一个示例并创建一个表-
mysql> create table DemoTable
(
StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StudentName varchar(40),
StudentAge int NOT NULL,
IsActiveStudent ENUM('ACTIVE",INACTIVE') NOT NULL,
StudentCountryName varchar(40)
);第一种语法如下,检查表的字段是否设置了NOTNULL属性-
mysql> desc DemoTable;
这将产生以下输出-
+--------------------+--------------------------+------+-----+---------+----------------+
| 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 | |
+--------------------+--------------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)第二种语法如下检查表的字段是否设置了NOTNULL属性-
mysql> select column_name, is_nullable from information_schema.columns where table_schema = 'web' and table_name = 'DemoTable';
这将产生以下输出-
+--------------------+-------------+ | COLUMN_NAME | IS_NULLABLE | +--------------------+-------------+ | StudentId | NO | | StudentName | YES | | StudentAge | NO | | IsActiveStudent | NO | | StudentCountryName | YES | +--------------------+-------------+ 5 rows in set (0.00 sec)
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短