0
0
SQLquery~5 mins

NOT NULL constraint in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the NOT NULL constraint do in a database?
It ensures that a column cannot have NULL values, meaning every row must have a value for that column.
Click to reveal answer
beginner
How do you add a NOT NULL constraint to a column when creating a table?
Use column_name datatype NOT NULL in the CREATE TABLE statement.
Click to reveal answer
intermediate
Can a column with a NOT NULL constraint accept empty strings ('')?
Yes, NOT NULL only prevents NULL values, but empty strings are considered valid values.
Click to reveal answer
beginner
What happens if you try to insert a row without a value for a NOT NULL column?
The database will reject the insert and return an error because the column requires a value.
Click to reveal answer
intermediate
How can you add a NOT NULL constraint to an existing column?
Use an ALTER TABLE statement like ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL; (syntax may vary by SQL dialect).
Click to reveal answer
What does the NOT NULL constraint prevent?
ADeleting rows
BInserting duplicate values
CInserting NULL values into a column
DUpdating values
Which SQL keyword is used to define a column that cannot be NULL?
AUNIQUE
BCHECK
CPRIMARY KEY
DNOT NULL
If a column is defined as NOT NULL, what happens if you try to insert a row without a value for that column?
AThe insert fails with an error
BThe database inserts a default value automatically
CThe row is inserted with an empty string
DThe row is inserted with NULL
Can a NOT NULL column contain an empty string ('')?
AOnly if the column is numeric
BYes, empty strings are allowed
CNo, empty strings are treated as NULL
DOnly if the column is a primary key
How do you add a NOT NULL constraint to an existing column in most SQL databases?
AALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;
BALTER TABLE table_name MODIFY column_name NOT NULL;
CALTER TABLE table_name ADD NOT NULL column_name;
DALTER TABLE table_name CHANGE column_name NOT NULL;
Explain what the NOT NULL constraint does and why it is useful in a database.
Think about what happens if a column must always have a value.
You got /3 concepts.
    Describe how you would add a NOT NULL constraint to a column in an existing table.
    Consider how to change a column's rules after the table is created.
    You got /3 concepts.