SQL NULL Nullable columns in tables

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.


Got any SQL Question?