
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
Actually, MySQL NOT NULL constraint restricts a column of the table from having a NULL value. Once we applied NOT NULL constraint to a column, then we cannot pass a null value to that column. It cannot be declared on the whole table i.e., in other words, we can say that NOT NULL is a column level constraint.
For declaring a field NOT NULL, we have to use NOT NULL keyword while defining the column in CREATE TABLE statement.
Example
mysql> Create table Employee(ID Int NOT NULL, First_Name Varchar(20), Last_name Varchar(20), Designation Varchar(15)); Query OK, 0 rows affected (0.59 sec)
In the query above, we have applied NOT NULL constraint on the field ‘ID’ of ‘Employee’ table. Now, the column ‘ID’ cannot take NULL value. It can be also checked from DESCRIBE statement that ID filed cannot accept NULL values.
mysql> DESCRIBE Employee123\G *************************** 1. row *************************** Field: ID Type: int(11) Null: NO Key: Default: NULL Extra: *************************** 2. row *************************** Field: First_Name Type: varchar(20) Null: YES Key: Default: NULL Extra: *************************** 3. row *************************** Field: Last_name Type: varchar(20) Null: YES Key: Default: NULL Extra: *************************** 4. row *************************** Field: Designation Type: varchar(15) Null: YES Key: Default: NULL Extra: 4 rows in set (0.03 sec)
- Related Articles
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- What happens when we apply NOT NULL constraint, with ALTER TABLE statement, to a column contains NULL values?
- Change a MySQL column to have NOT NULL constraint
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- Enum with NOT NULL in a MySQL field?
- Difference Between MySql NULL and IS NOT NULL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- What is the benefit of MySQL ‘IS NULL’ and ‘IS NOT NULL’?
- How to add not null constraint to existing column in MySQL?
- Working with NULL and IS NOT NULL in MySQL
- Can we use {} while creating a MySQL table?
- In MySQL what is the difference between != NULL and IS NOT NULL?
- How to add NOT NULL constraint to an already created MySQL column?
- What is MySQL UNIQUE constraint and how can we apply it to the field of a table?

Advertisements