NOT NULL and DEFAULT constraints in MySQL - Time & Space Complexity
When we add NOT NULL and DEFAULT constraints in a database, it affects how data is checked and inserted.
We want to understand how these checks grow as more data is added.
Analyze the time complexity of the following table creation and insert statements.
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
status VARCHAR(20) DEFAULT 'active'
);
INSERT INTO users (id, name) VALUES (1, 'Alice');
INSERT INTO users (id, name, status) VALUES (2, 'Bob', 'inactive');
This code creates a table with NOT NULL and DEFAULT constraints, then inserts rows with and without the status value.
Look at what happens each time a row is inserted.
- Primary operation: Checking NOT NULL fields and applying DEFAULT values.
- How many times: Once per inserted row.
Each new row requires checking constraints and setting defaults if needed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and default assignments |
| 100 | 100 checks and default assignments |
| 1000 | 1000 checks and default assignments |
Pattern observation: The work grows directly with the number of rows inserted.
Time Complexity: O(n)
This means the time to check constraints and apply defaults grows in a straight line as more rows are added.
[X] Wrong: "NOT NULL and DEFAULT constraints slow down inserts exponentially as data grows."
[OK] Correct: Each row is checked independently, so the time grows steadily, not exponentially.
Understanding how constraints affect insert speed helps you design tables that balance data safety and performance.
"What if we added a UNIQUE constraint on the name column? How would the time complexity change?"
Practice
NOT NULL constraint do in a MySQL table column?Solution
Step 1: Understand NOT NULL constraint
The NOT NULL constraint means a column must always have a value; it cannot be left empty or null.Step 2: Compare with other options
Only It ensures the column cannot have empty or missing values. correctly describes this behavior. It automatically fills the column with a default value. describes DEFAULT, C is unrelated, and D is incorrect.Final Answer:
It ensures the column cannot have empty or missing values. -> Option DQuick Check:
NOT NULL means no empty values allowed [OK]
- Confusing NOT NULL with DEFAULT
- Thinking NOT NULL deletes rows
- Assuming NOT NULL allows empty strings
age of type INT in MySQL?Solution
Step 1: Review correct ALTER TABLE syntax
To modify a column with NOT NULL and DEFAULT, the syntax is: MODIFY column_name data_type NOT NULL DEFAULT value.Step 2: Check each option
ALTER TABLE users MODIFY age INT NOT NULL DEFAULT 18; matches this syntax exactly. Options B and C have wrong order or keywords. ALTER TABLE users MODIFY age INT DEFAULT 18 NULL; allows NULL, which is incorrect.Final Answer:
ALTER TABLE users MODIFY age INT NOT NULL DEFAULT 18; -> Option BQuick Check:
Correct syntax uses MODIFY with NOT NULL then DEFAULT [OK]
- Placing DEFAULT before NOT NULL
- Using ADD instead of MODIFY for existing columns
- Allowing NULL when NOT NULL is required
employees with column status VARCHAR(10) NOT NULL DEFAULT 'active', what will be the value of status after this insert?INSERT INTO employees (name) VALUES ('John');Solution
Step 1: Understand DEFAULT and NOT NULL behavior
The column status cannot be NULL and has a default value 'active'. If no value is given, MySQL uses the default.Step 2: Analyze the INSERT statement
The insert only provides name, so status uses its default 'active'. No error occurs.Final Answer:
'active' -> Option CQuick Check:
Missing value uses DEFAULT when NOT NULL [OK]
- Expecting NULL instead of default
- Thinking insert will fail without explicit value
- Assuming empty string is default
CREATE TABLE products (id INT NOT NULL, price DECIMAL(5,2) DEFAULT 0.00);
What is the problem with this table definition?
Solution
Step 1: Check NOT NULL columns without DEFAULT
The id column is NOT NULL but has no default value. If an insert omits id, it will cause an error.Step 2: Analyze other options
Thepricecolumn cannot have a default value. is wrong because price can have a default. Theidcolumn allows NULL values. is wrong because id is NOT NULL. There is no PRIMARY KEY defined, so the table is invalid. is not mandatory for table creation.Final Answer:
The id column is NOT NULL but has no DEFAULT, causing insert errors. -> Option AQuick Check:
NOT NULL without DEFAULT requires explicit value on insert [OK]
- Assuming NOT NULL columns get default NULL
- Thinking PRIMARY KEY is required for table creation
- Believing DEFAULT is mandatory for NOT NULL
orders where the status column must never be empty and should default to 'pending'. Which of these statements correctly creates this table?Solution
Step 1: Identify constraints needed
Status must never be empty (NOT NULL) and default to 'pending' if no value is given.Step 2: Check each CREATE TABLE option
CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending'); correctly uses NOT NULL and DEFAULT together. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT 'pending'); misses NOT NULL, allowing NULLs. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL); misses DEFAULT, so no automatic value. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT NOT NULL 'pending'); has wrong syntax.Final Answer:
CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending'); -> Option AQuick Check:
NOT NULL with DEFAULT sets required and default value [OK]
- Omitting NOT NULL allows NULL values
- Placing DEFAULT before NOT NULL
- Using invalid syntax with DEFAULT NOT NULL
Ready to prove it?
Pass the test and this pattern moves unknown
