What does the NOT NULL constraint do in a database table?
It ensures that a column cannot have a NULL value, meaning every row must have a valid value for that column.
Click to reveal answer
beginner
Can you insert a row with a NULL value in a column that has a NOT NULL constraint?
No, the database will reject the insert and return an error because the NOT NULL constraint requires a value.
Click to reveal answer
intermediate
How does the NOT NULL constraint help maintain data quality?
By preventing missing or unknown values in important columns, it ensures data completeness and reliability.
Click to reveal answer
intermediate
What happens if you try to update a NOT NULL column to NULL?
The update will fail with an error because the NOT NULL constraint forbids NULL values in that column.
Click to reveal answer
advanced
Is it possible to add a NOT NULL constraint to an existing column that already contains NULL values?
No, you must first update or remove NULL values before adding the NOT NULL constraint, or the database will reject it.
Click to reveal answer
What will happen if you insert a NULL value into a NOT NULL column?
AThe insert will succeed with NULL stored
BThe NULL will be ignored and default value used
CThe NULL will be converted to zero
DThe insert will fail with an error
✗ Incorrect
The NOT NULL constraint forbids NULL values, so the insert fails with an error.
Which of the following best describes the NOT NULL constraint?
APrevents NULL values in a column
BAllows NULL values in a column
CAutomatically fills NULL values
DDeletes rows with NULL values
✗ Incorrect
NOT NULL constraint prevents NULL values in the specified column.
Can you add a NOT NULL constraint to a column that currently has NULL values without changing the data?
ANo, you must first remove or update NULL values
BYes, always
CYes, but only if the table is empty
DNo, constraints cannot be added after table creation
✗ Incorrect
You must remove or update NULL values before adding a NOT NULL constraint.
What is the main benefit of using NOT NULL constraints?
AImproves query speed
BEnsures data completeness
CAllows flexible data entry
DAutomatically indexes the column
✗ Incorrect
NOT NULL constraints ensure that important columns always have data, improving completeness.
If a column has a NOT NULL constraint, what happens when you try to update its value to NULL?
AThe update succeeds and sets NULL
BThe update sets the default value instead
CThe update fails with an error
DThe update is ignored silently
✗ Incorrect
NOT NULL constraint forbids NULL values, so updating to NULL causes an error.
Explain what the NOT NULL constraint does and why it is important in databases.
Think about how missing data can affect your information.
You got /3 concepts.
Describe the steps needed to add a NOT NULL constraint to an existing column that currently has NULL values.
You cannot add NOT NULL if NULLs exist already.
You got /3 concepts.
Practice
(1/5)
1. What does the NOT NULL constraint do in a database table?
easy
A. It allows a column to have duplicate values.
B. It ensures that a column cannot have empty or missing values.
C. It automatically fills the column with default values.
D. It deletes rows with null values in any column.
Solution
Step 1: Understand the purpose of NOT NULL
The NOT NULL constraint prevents a column from having NULL (empty) values, ensuring data completeness.
Step 2: Compare options with the definition
Only the description 'It ensures that a column cannot have empty or missing values.' correctly describes this behavior; others describe different constraints or actions.
Final Answer:
It ensures that a column cannot have empty or missing values. -> Option B
Quick Check:
NOT NULL = no empty values [OK]
Hint: NOT NULL means no empty values allowed in that column [OK]
Common Mistakes:
Thinking NOT NULL allows duplicates
Confusing NOT NULL with default values
Assuming NOT NULL deletes rows
2. Which of the following is the correct syntax to add a NOT NULL constraint to a column named email in a table users?
easy
A. ALTER TABLE users MODIFY email NOT NULL;
B. ALTER TABLE users ADD email NOT NULL;
C. ALTER TABLE users SET email NOT NULL;
D. ALTER TABLE users CHANGE email TO NOT NULL;
Solution
Step 1: Recall ALTER TABLE syntax for modifying column constraints
To add NOT NULL, we use ALTER TABLE with MODIFY (MySQL syntax).
Step 2: Evaluate each option's syntax
ALTER TABLE users MODIFY email NOT NULL; uses correct syntax; others use invalid or incorrect commands.
Final Answer:
ALTER TABLE users MODIFY email NOT NULL; -> Option A
Quick Check:
ALTER TABLE + MODIFY + NOT NULL = correct syntax [OK]
Hint: Use ALTER TABLE with MODIFY to change NOT NULL [OK]
Common Mistakes:
Using ADD instead of MODIFY for existing columns
Using SET or CHANGE incorrectly
Missing semicolon or wrong keywords
3. Given the table products with a price column defined as DECIMAL NOT NULL, what happens when you run this query?
INSERT INTO products (name, price) VALUES ('Pen', NULL);
medium
A. The query fails with an error due to NOT NULL violation.
B. The row is inserted with price set to 0.
C. The row is inserted with price set to NULL anyway.
D. The query succeeds but price is ignored.
Solution
Step 1: Understand NOT NULL effect on insert
NOT NULL means the column cannot accept NULL values during insert or update.
Step 2: Analyze the insert statement
Inserting NULL into a NOT NULL column causes the database to reject the query with an error.
Final Answer:
The query fails with an error due to NOT NULL violation. -> Option A
Quick Check:
Inserting NULL into NOT NULL column = error [OK]
Hint: Inserting NULL into NOT NULL column causes error [OK]
Common Mistakes:
Assuming NULL becomes 0 automatically
Thinking NULL is allowed silently
Believing the query will partially succeed
4. You have this table creation statement:
CREATE TABLE employees (id INT, name VARCHAR(50) NOT NULL);
You try to insert:
INSERT INTO employees (id, name) VALUES (1, NULL);
But it gives an error. What is the fix?
medium
A. Add DEFAULT NULL to the name column.
B. Change the id column to NOT NULL.
C. Remove NOT NULL from the name column.
D. Insert a non-NULL value for name.
Solution
Step 1: Identify cause of error
The name column is NOT NULL, so inserting NULL causes an error.
Step 2: Find correct fix
To fix, insert a valid non-NULL value for name; changing other columns or constraints won't help here.
Final Answer:
Insert a non-NULL value for name. -> Option D
Quick Check:
NOT NULL column must get non-NULL value [OK]
Hint: Insert real value for NOT NULL columns to avoid errors [OK]
Common Mistakes:
Removing NOT NULL without reason
Changing unrelated columns
Adding DEFAULT NULL which conflicts with NOT NULL
5. You want to ensure that the username column in your accounts table always has a value and cannot be empty. Which of these combined approaches is best to enforce this?
CREATE TABLE accounts ( id INT PRIMARY KEY, username VARCHAR(30) NOT NULL CHECK (username <> '') );
hard
A. Use NOT NULL only, because it blocks empty strings too.
B. Use CHECK only, because NOT NULL allows empty strings.
C. Use both NOT NULL and CHECK to block NULL and empty strings.
D. No constraints needed; application code will handle it.
Solution
Step 1: Understand NOT NULL and empty strings
NOT NULL prevents NULL values but allows empty strings ('').
Step 2: Understand CHECK constraint role
CHECK (username <> '') prevents empty strings but allows NULL unless combined with NOT NULL.
Step 3: Combine constraints for full validation
Using both NOT NULL and CHECK ensures username is neither NULL nor empty.
Final Answer:
Use both NOT NULL and CHECK to block NULL and empty strings. -> Option C
Quick Check:
NOT NULL + CHECK = no NULL or empty [OK]
Hint: Combine NOT NULL and CHECK to block NULL and empty strings [OK]
Common Mistakes:
Assuming NOT NULL blocks empty strings
Using CHECK without NOT NULL allows NULLs
Relying only on application code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.