INSERT INTO
In this page:
Introduction to INSERT INTO
INSERT INTO adds a new row by naming the target table, listing which columns you're supplying values for, and then providing those values in matching order. If you list every column in the table's own order, you can actually omit the column list entirely, though naming columns explicitly is safer and clearer.
Example: Introduction to INSERT INTO
INSERT INTO users (name, email) VALUES ('Amit', '[email protected]');
Inserting into All Columns
If you supply a value for every single column in the exact order the table defines them, you can technically omit the column-name list — though this is fragile if the table's structure ever changes.
Example: Inserting into All Columns
INSERT INTO users VALUES (1, 'Amit', '[email protected]');
Handling Default Values
Columns with a DEFAULT value don't need to appear in your INSERT at all; MySQL automatically fills them in with their configured default when you leave them out. This is especially convenient for columns like created_at, where you'd otherwise have to compute and supply the current timestamp yourself every time.
Example: Handling Default Values
CREATE TABLE users (id INT, status VARCHAR(20) DEFAULT 'active');
INSERT INTO users (id) VALUES (1);
Inserting NULL Values
For a column that permits NULL, explicitly writing the NULL keyword tells MySQL 'this value is intentionally unknown or absent,' distinct from an empty string or zero. Omitting a nullable column from your INSERT entirely has the same practical effect as writing NULL explicitly for it.
Example: Inserting NULL Values
INSERT INTO users (id, name, middle_name) VALUES (1, 'Amit', NULL);
Best Practices for INSERT
Always listing your target columns explicitly, even when inserting into all of them, protects your INSERT statements from silently breaking if someone later adds or reorders columns in the table.
Example: Best Practices for INSERT
INSERT INTO users (id, name) VALUES (1, 'Amit');
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: