INSERT — Adding Data
Add new rows to a table with the SQL INSERT statement — the “C” in CRUD. Learn the syntax and how to insert one row or many at once.
What you will learn
- Insert one and many rows
Adding rows with INSERT
A new table is empty. To put data in, you use INSERT INTO — the C (Create) in CRUD. You tell MySQL which table, which columns you are filling, and the values to put in them. The order of the values must match the order of the columns you listed.
An INSERT works in three simple steps:
- Name the table and the columns you want to fill:
INSERT INTO users (name, email, age). - Provide the matching values in the same order with
VALUES ('Asha', 'asha@x.com', 22). - MySQL adds a new row and fills in any columns you left out (like
idandcreated_at) automatically.
INSERT INTO users (name, email, age)
VALUES ('Asha', 'asha@x.com', 22);
INSERT INTO users (name, email, age) VALUES
('Ravi', 'ravi@x.com', 25),
('Meera', 'meera@x.com', 23);The first statement adds a single row for Asha. The second adds two rows in one go — notice you only write VALUES once, then list each row in its own brackets separated by commas. Inserting many rows at once like this is faster than running a separate INSERT for each. The text values (name, email) sit in single quotes; the number (age) does not.
Note: Output:
Query OK, 1 row affected.
Query OK, 2 rows affected.
MySQL confirms how many rows it added. The id column fills in automatically (AUTO_INCREMENT gives Asha 1, Ravi 2, Meera 3) and created_at fills in with the current time (its DEFAULT) — that is why you do not list them.
Q. Which SQL command adds new rows to a table?
✍️ Practice
- Insert three users into your table.
- Insert several products in one statement.
🏠 Homework
- Populate your library tables with sample data.