Chapter 7: Inserting Data (DML)

Up until now, we’ve been architects: designing databases, creating tables, choosing perfect data types, and adding strong constraints. Now it’s time to become data entry specialists (but the smart, efficient kind 😄).

In SQL, the main command for adding data is INSERT INTO — part of DML (Data Manipulation Language). Today we’re going to master three very important ways to insert data:

  1. Basic INSERT INTO – Adding one row at a time
  2. Inserting multiple rows in one command (super efficient!)
  3. INSERT … SELECT – Copying data from one table to another (very powerful!)

We’ll continue using our bookshop database. Let’s first quickly set up the tables we need (you can copy-paste this if you want to follow along).

Quick Setup – Our Tables

SQL

Now — let’s start inserting!

1. Basic INSERT INTO – Adding One Row

Syntax (most common style):

SQL

Important rules:

  • You must provide values for all NOT NULL columns (unless they have DEFAULT)
  • Order of columns in () must match order of values
  • Strings go in single quotes‘like this’
  • Numbers do not need quotes
  • Dates usually in ‘YYYY-MM-DD’ format

Real example – Adding one author:

SQL

Adding one book (notice we use the author_id we just created):

SQL

Alternative style (insert without listing columns – values must match ALL columns in order):

SQL

Pro Tip: Always list the columns explicitly — it’s safer and clearer. If someone changes the table later (adds a column), your query won’t break.

2. Inserting Multiple Rows at Once (Very Efficient!)

You can insert many rows in one single command — this is much faster than 100 separate INSERTs.

Syntax:

SQL

Real example – Add 4 authors at once:

SQL

Add 5 books at once:

SQL

Why is this better?

  • Fewer round-trips to the database → much faster
  • In one transaction (safer — all succeed or none do)

3. INSERT … SELECT – Copying Data from One Table to Another

This is one of the most powerful INSERT techniques! You insert rows by selecting them from another table (or even the same table).

Syntax:

SQL

Real example – Create a backup table and copy all books into it:

SQL

More useful example – Copy only expensive books (> ₹300) to a premium table:

SQL

Even cooler – Copy with transformation:

SQL

Important Tips & Best Practices (2026 Style)

Tip Why it matters
Always list columns Safer when table structure changes
Use single quotes for strings ‘Mumbai’, not “Mumbai” (double quotes = identifiers in some DBs)
Escape single quotes inside strings ‘O”Reilly’ (two single quotes = one quote)
Use transactions in production START TRANSACTION; INSERT…; COMMIT; or ROLLBACK;
Batch large inserts (1000–5000 rows) Avoids memory issues and is faster
Check constraints first Make sure your data satisfies NOT NULL, UNIQUE, FOREIGN KEY, etc.
Use IGNORE if you want to skip duplicates INSERT IGNORE INTO … (MySQL/MariaDB)

Common Mistakes & How to Fix Them

Mistake Error Message / Problem Fix
Forgetting quotes around strings Syntax error Always ‘string’
Wrong number of columns/values Column count doesn’t match value count Match them exactly
Violating FOREIGN KEY Cannot add or update a child row Make sure referenced row exists first
Inserting duplicate UNIQUE value Duplicate entry for key Check with SELECT before insert or use IGNORE
Forgetting NOT NULL fields Field cannot be null Provide value or set DEFAULT

Full Fun Example – Let’s Populate the Bookshop!

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *