0
0
SQLquery~10 mins

NOT NULL constraint behavior in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a table where the 'name' column cannot be empty.

SQL
CREATE TABLE users (id INT, name VARCHAR(100) [1]);
Drag options to blanks, or click blank then click option'
ANULL
BNOT NULL
CDEFAULT NULL
DPRIMARY KEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of NOT NULL allows empty values.
Using PRIMARY KEY when you only want to prevent nulls.
2fill in blank
medium

Complete the INSERT statement to add a user with a non-empty 'email' field that has a NOT NULL constraint.

SQL
INSERT INTO users (id, email) VALUES (1, [1]);
Drag options to blanks, or click blank then click option'
A'user@example.com'
BDEFAULT
C''
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Inserting NULL into a NOT NULL column causes an error.
Using empty string '' may be allowed but is often not meaningful.
3fill in blank
hard

Fix the error in the table creation by adding the correct constraint to the 'age' column to prevent NULL values.

SQL
CREATE TABLE people (id INT, age INT [1]);
Drag options to blanks, or click blank then click option'
AUNIQUE
BDEFAULT 0
CCHECK (age > 0)
DNOT NULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using DEFAULT 0 does not prevent NULL values.
CHECK constraints do not prevent NULL unless specified.
4fill in blank
hard

Fill both blanks to create a table where 'username' cannot be NULL and has a default value 'guest'.

SQL
CREATE TABLE accounts (id INT, username VARCHAR(50) [1] [2]);
Drag options to blanks, or click blank then click option'
ANOT NULL
BDEFAULT 'guest'
CUNIQUE
DPRIMARY KEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using UNIQUE instead of NOT NULL does not prevent NULL values.
Forgetting to set DEFAULT causes errors when no value is given.
5fill in blank
hard

Fill all three blanks to insert a new row with id 10, a non-null name, and a default age in the 'persons' table.

SQL
INSERT INTO persons (id, name, age) VALUES ([1], [2], [3]);
Drag options to blanks, or click blank then click option'
A10
B'Alice'
CDEFAULT
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL for age when it has a NOT NULL constraint causes error.
Not quoting the name string causes syntax error.