Description

The SQL keyword NOT NULL is a constraint, that enforces a column to not accept NULL values.

  • A column defined as NOT NULL doesn't allow a NULL value through an update or delete statement.
  • Recommended to define a column with NOT NULL if we always need data for a column.

CREATE TABLE With NOT NULL

The below SQL creates a new table with columns "user_id" and "user_name" defined as NOT NULL, which will not accept NULL values.

CREATE TABLE users (
    user_id INT NOT NULL,
    user_name VARCHAR(255) NOT NULL,
    age INT
);

ALTER TABLE With NOT NULL

The below SQL alters the table definition by changing the "age" column as NOT NULL.

ALTER TABLE users
MODIFY age INT NOT NULL;

Related Links