The Not Null constraint specifies the column may not contain NULL values.

Details

For more information about NULL, see Null Handling.

Syntax

CREATE TABLE table_name ( column_name column_type NOT NULL column_constraints , column_def table_constraints ) )
Parameter Description
table_name The name of the table you’re creating.
column_name The name of the constrained column.
column_type The constrained column’s data type.
column_constraints Any other column-level constraints you want to apply.
column_def Definitions for any other columns in the table.
table_constraints Any table-level constraints you want to apply.

Example

> CREATE TABLE customers (
    customer_id INT         PRIMARY KEY,
    cust_name   STRING(30)  NULL,
    cust_email  STRING(100) NOT NULL
  );
You cannot apply the Not Null constraint to multiple columns (i.e. at the table level).

Usage Example

> CREATE TABLE IF NOT EXISTS customers (
    customer_id INT         PRIMARY KEY,
    cust_name   STRING(30)  NULL,
    cust_email  STRING(100) NOT NULL
  );

> INSERT INTO customers (customer_id, cust_name, cust_email) VALUES (1, 'Smith', NULL);
pq: null value in column "cust_email" violates not-null constraint

See Also



Yes No