Chapter 5: Basic Data Manipulation (DML)

This is the chapter where we finally start playing with real data! Up to now we created the structure (databases & tables). Now we will:

  • Add data (INSERT)
  • Read data (SELECT)
  • Change data (UPDATE)
  • Remove data (DELETE)
  • Completely empty a table (TRUNCATE)

These are the four most important commands you will use every single day as a developer!

Make sure you’re inside your database:

SQL

And that you have the students table we created in Chapter 4. (If you don’t have it yet, quickly run the CREATE TABLE command from the previous chapter.)

1. INSERT INTO – Adding New Records (Single & Multiple Rows)

Syntax – Single Row

SQL

Syntax – Multiple Rows (Very efficient!)

SQL

Important: You must provide values for all NOT NULL columns (like full_name in our table). For AUTO_INCREMENT columns like student_id, you don’t need to give a value — MySQL will auto-assign it.

Example – Adding 4 students

SQL

After running this, you should see:

text

Tip: If you forget to mention a column that has DEFAULT, MySQL uses the default value. If you forget a NOT NULL column, you get an error.

Another cool way – Insert without specifying column names (only if you give values in exact order)

SQL

(Note: NULL for student_id because it’s AUTO_INCREMENT)

2. SELECT Statement – Reading / Retrieving Data (The most used command!)

Basic Syntax:

SQL

Most common – See everything:

SQL

You should see something like this:

text

Select specific columns:

SQL

Select with alias (change display name):

SQL

Select only active students:

SQL

(We’ll learn WHERE properly in the next chapter – for now just remember it filters rows)

3. UPDATE – Changing Existing Records

Syntax:

SQL

Very important: Always use WHERE! If you forget WHERE, all rows will be updated — very dangerous!

Example – Update phone number of Priya Sharma

SQL

Update multiple columns:

SQL

Update all students who joined before 2025-02-01:

SQL

4. DELETE – Removing Records

Syntax:

SQL

Again – always use WHERE! No WHERE = deletes everything!

Example – Delete one student:

SQL

Delete all inactive students:

SQL

Delete everything (careful!):

SQL

(This empties the table but keeps the structure)

5. TRUNCATE TABLE – Completely Empty a Table (Faster than DELETE)

Syntax:

SQL

Differences between DELETE and TRUNCATE:

Feature DELETE TRUNCATE
Removes all rows? Yes Yes
Keeps table structure? Yes Yes
Can use WHERE? Yes No (always all rows)
Resets AUTO_INCREMENT? No Yes (back to 1)
Can be rolled back? Yes (inside transaction) No (faster, no logging)
Speed Slower (logs each row) Much faster

Example:

SQL

After this:

  • Table is empty
  • student_id will start from 1 again for new inserts

That’s it for Chapter 5! 🎉

Summary – What we learned today:

  • INSERT INTO → Add new rows (single or multiple)
  • SELECT → Read data (* or specific columns)
  • UPDATE → Change existing data (always with WHERE!)
  • DELETE → Remove rows (always with WHERE!)
  • TRUNCATE → Fast way to empty entire table

Homework for today (do it right now – very important!)

  1. Make sure you’re in my_coaching database
  2. Run these commands one by one and copy-paste the output here:
SQL

You may also like...

Leave a Reply

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