Lesson

MySQL INSERT INTO

10 min reading
Free Course

MySQL INSERT INTO: Adding Rows to a Table

The MySQL INSERT INTO statement adds one or more new records to an existing table. You specify the target table, list the columns you are populating, and supply matching values in the exact same order.

Single-Row and Multi-Row Syntax

Listing column names explicitly protects your code when someone later adds a new column to the table:

INSERT INTO table_name (column1, column2)
VALUES
    (value1a, value2a),
    (value1b, value2b);

Multi-row INSERT statements execute in a single network round-trip and are dramatically faster than running hundreds of single-row inserts in a loop.

Practical SQL Example

This script adds two new rows into demo_customers while letting MySQL generate the AUTO_INCREMENT primary key automatically.

INSERT INTO demo_customers (first_name, last_name, email, city)
VALUES
    ('Maya', 'Patel', 'maya@example.com', 'Seattle'),
    ('Liam', 'Chen', 'liam@example.com', 'Boston');

-- Verify inserted records
SELECT id, first_name, email, city
FROM demo_customers
ORDER BY id DESC
LIMIT 2;
id first_name email city
5 Liam liam@example.com Boston
4 Maya maya@example.com Seattle

Handling Conflicts and Defaults

  • Use DEFAULT or Omit Optional Columns: Omit AUTO_INCREMENT IDs and timestamp columns with default values so MySQL fills them automatically.
  • Avoid INSERT IGNORE Blindly: INSERT IGNORE silently drops bad data and truncates strings. Prefer INSERT ... ON DUPLICATE KEY UPDATE when you need upsert behavior.
  • Keep Column and Value Counts Equal: Every column named after the table must match a value at the same position in VALUES (...).

Frequently Asked Questions

Can I insert data without listing column names?

Yes, if you supply a value for every single column in exact table order, though omitting column names breaks easily after schema migrations.

How do I get the generated ID after an INSERT?

Run SELECT LAST_INSERT_ID(); on the same database connection immediately after inserting the row.

How many rows can one multi-row INSERT statement hold?

It is bounded by max_allowed_packet (commonly 64 MB); batching 500 to 1,000 rows per statement works well in practice.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum