What is the purpose of the NOT NULL constraint in SQL?
A
To ensure all values are unique B
To prevent duplicate rows C
To ensure a column cannot have NULL values D
To automatically generate values
Analysis & Theory
NOT NULL makes sure every row has a value for that column.
Which of the following statements correctly creates a column that cannot contain NULLs?
A
CREATE TABLE Users (ID INT NOT NULL); B
CREATE TABLE Users (ID NOT NULL INT); C
CREATE TABLE Users (NOT NULL ID INT); D
CREATE TABLE Users (ID INT NULL NOT);
Analysis & Theory
The correct syntax is column_name datatype NOT NULL.
If you try to insert a row without specifying a value for a NOT NULL column, what happens?
A
SQL inserts NULL automatically B
SQL uses 0 as default C
SQL raises an error D
SQL skips the row
Analysis & Theory
An error is thrown if a NOT NULL column is left without a value (unless a DEFAULT is defined).
Can a PRIMARY KEY column contain NULL values?
A
Yes, always B
Yes, but only one NULL C
No, PRIMARY KEY implies NOT NULL D
Yes, if UNIQUE is used
Analysis & Theory
PRIMARY KEY automatically enforces NOT NULL and UNIQUE.
Which constraint would you use to ensure a column always has data?
A
DEFAULT B
UNIQUE C
FOREIGN KEY D
NOT NULL
Analysis & Theory
NOT NULL is specifically for requiring a value.
How can you add a NOT NULL constraint to an existing column?
A
ALTER TABLE table_name ADD NOT NULL column_name; B
ALTER TABLE table_name MODIFY column_name NOT NULL; C
ALTER TABLE table_name SET column_name NOT NULL; D
ALTER TABLE table_name CREATE NOT NULL column_name;
Analysis & Theory
ALTER TABLE ... MODIFY ... NOT NULL is the syntax used in many databases.
If a column has a DEFAULT value and is NOT NULL, what happens when you omit the column in an INSERT?
A
NULL is inserted B
An error occurs C
The DEFAULT value is used D
The row is skipped
Analysis & Theory
The DEFAULT value fills in when no value is provided.
Which of these constraints can you combine with NOT NULL on the same column?
A
UNIQUE B
PRIMARY KEY C
CHECK D
All of the above
Analysis & Theory
NOT NULL can be combined with other constraints like UNIQUE, CHECK, and PRIMARY KEY.
What is the default behavior if you don't specify NOT NULL or NULL in a column definition?
A
NULL values are allowed B
NOT NULL is assumed C
A DEFAULT value is required D
The column becomes a PRIMARY KEY
Analysis & Theory
By default, columns allow NULL values unless NOT NULL is explicitly declared.
Which of these statements correctly defines a NOT NULL constraint together with DEFAULT?
A
CREATE TABLE Products (Price DECIMAL(10,2) NOT NULL DEFAULT 0.0); B
CREATE TABLE Products (Price DECIMAL(10,2) DEFAULT 0.0 NOT NULL); C
Both A and B D
Neither A nor B
Analysis & Theory
Both orders of DEFAULT and NOT NULL are valid in column definitions.