- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a field of table has NOT NULL property set in SQL?
To check if field of a table has NOT NULL property, you can use any of the two syntaxes. The first syntax is as follows −
desc yourTableName;
Following is the second syntax −
select column_name, is_nullable from information_schema.columns where table_schema = ‘yourDatabaseName’ and table_name = 'yourTableName’;
Let us first see an example and create a table −
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) ); Query OK, 0 rows affected (1.53 sec)
The first syntax is as follows to check if field of table has NOT NULL property set −
mysql> desc DemoTable;
This will produce the following output −
+--------------------+--------------------------+------+-----+---------+----------------+ | 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)
The second syntax is as follows to check if field of table has NOT NULL property set −
mysql> select column_name, is_nullable from information_schema.columns where table_schema = 'web' and table_name = 'DemoTable';
This will produce the following output −
+--------------------+-------------+ | COLUMN_NAME | IS_NULLABLE | +--------------------+-------------+ | StudentId | NO | | StudentName | YES | | StudentAge | NO | | IsActiveStudent | NO | | StudentCountryName | YES | +--------------------+-------------+ 5 rows in set (0.00 sec)
- Related Articles
- How do you check if a field is NOT NULL with Eloquent?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- Check if a String is not empty ("") and not null in Java
- How to check if field is null or empty in MySQL?
- Select different fields in MySQL even if a field is set to null?
- Performing Null check using HANA SQL Script
- Enum with NOT NULL in a MySQL field?
- MongoDB query to select one field if the other is null and the first field if both are not null?
- How to query for records where field is null or not set in MongoDB?
- Check if a number has two adjacent set bits in C++
- Create a column on my table that allows null but is set by default to empty (not null)?
- Check whether a field is empty or null in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- How to validate aninput field if the value is not NULL in Laravel?
- How to check if any value is Null in a MySQL table single row?

Advertisements