SQL Nullable columns in tables


Example

When creating tables it is possible to declare a column as nullable or non-nullable.

CREATE TABLE MyTable
(
    MyCol1 INT NOT NULL, -- non-nullable
    MyCol2 INT NULL      -- nullable
) ;

By default every column (except those in primary key constraint) is nullable unless we explicitly set NOT NULL constraint.

Attempting to assign NULL to a non-nullable column will result in an error.

INSERT INTO MyTable (MyCol1, MyCol2) VALUES (1, NULL) ;  -- works fine

INSERT INTO MyTable (MyCol1, MyCol2) VALUES (NULL, 2) ;  
        -- cannot insert
        -- the value NULL into column 'MyCol1', table 'MyTable'; 
        -- column does not allow nulls. INSERT fails.