INSERT
INSERT is the command that adds a new row into a table. The full form names each column explicitly: INSERT INTO students (id, name, age) VALUES (1, "Ali", 16);. The order of the values must match the order of the column names you listed. You can insert several rows in one statement by separating each set of values with a comma: INSERT INTO students (id, name, age) VALUES (2, "Sara", 17), (3, "Omar", 15);. This is far more efficient than running three separate INSERT statements. If you list values for every column in the table's exact defined order, you can technically omit the column name list but writing the column names explicitly is safer: it keeps working correctly even if the table's column order ever changes, and it makes the statement self-explanatory to read later.
Why this matters
- ·INSERT is how a table actually gets real data, turning an empty structure into something useful.
- ·Inserting many rows in one statement is faster than running many separate single-row inserts.
- ·Naming columns explicitly makes an INSERT statement clear and safe against future table changes.
Common mistakes
- ·Mismatching the order of values against the order of listed columns silently puts data in the wrong place.
- ·Inserting a duplicate primary key value causes an error, since primary keys must be unique.
- ·Skipping a required (NOT NULL) column without a default value will cause the insert to fail.
Query OK, 2 rows affected +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | Ali | 16 | | 2 | Sara | 17 | +----+------+-----+ 2 rows in set
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Frequently asked questions
Yes. Separate each set of values with a comma inside a single INSERT statement.
Practice questions
Question 1
Insert a row into students with id 3, name "Omar", age 15.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Question 2
Insert two rows into students in a single statement, with ids 4 and 5.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
