Concept Flow - INSERT with DEFAULT values
Start INSERT command
Check if VALUES provided
Insert given
Add row to table
End
When inserting a row, if no value is given for a column, SQL uses the DEFAULT value for that column.
CREATE TABLE fruits ( id INT DEFAULT 1, name VARCHAR(10) DEFAULT 'apple' ); INSERT INTO fruits DEFAULT VALUES;
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Start INSERT command | INSERT INTO fruits DEFAULT VALUES; | Ready to insert a new row |
| 2 | Check if VALUES provided | No explicit values given | Use DEFAULT values for all columns |
| 3 | Assign DEFAULT to id | id DEFAULT is 1 | id = 1 |
| 4 | Assign DEFAULT to name | name DEFAULT is 'apple' | name = 'apple' |
| 5 | Add row to table | Row to insert: (1, 'apple') | Row inserted successfully |
| 6 | End | Insertion complete | Table now has 1 row with id=1 and name='apple' |
| Variable | Start | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|
| id | NULL | 1 | 1 | 1 |
| name | NULL | NULL | 'apple' | 'apple' |
INSERT with DEFAULT values: - Use: INSERT INTO table DEFAULT VALUES; - Inserts one row using all columns' default values - If no default and no value given, error occurs - Useful for quick inserts with defaults - Defaults defined in table schema