MySQL NOT NULL Constraint

In mysql, the column's default value is null. So even if we didn't provide any value to the column it will be considered as null and executes the query.
If we need to make any column value to act as mandatory value then we can use the NOT NULL constraint.

Syntax for NOT NULL Constraint

CREATE TABLE table_name (
    column_1 datatype NOT NULL,
    column_2 datatype NOT NULL,
    column_3 datatype,
   ....
);

Example

create table employee(empno int primary key AUTO_INCREMENT, 
name varchar(50) NOT NULL,
age numeric, 
role varchar(50) NOT NULL, 
location varchar(50) NOT NULL, 
salary decimal NOT NULL);

If we need to alter a column in an existing table as NOT NULL we can use the syntax

Example

alter table employees
modify column role varchar(50) NOT NULL;

Most Read