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:
|
0 1 2 3 4 5 6 |
USE my_coaching; |
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
|
0 1 2 3 4 5 6 7 |
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); |
Syntax – Multiple Rows (Very efficient!)
|
0 1 2 3 4 5 6 7 8 9 10 |
INSERT INTO table_name (column1, column2, ...) VALUES (value1a, value2a, ...), (value1b, value2b, ...), (value1c, value2c, ...); |
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
|
0 1 2 3 4 5 6 7 8 9 10 11 |
INSERT INTO students (full_name, phone, email, date_of_birth, gender, join_date) VALUES ('Priya Sharma', '9876543210', 'priya.sharma@gmail.com', '2008-05-12', 'F', '2025-01-05'), ('Rahul Patel', '9123456789', 'rahul.p@gmail.com', '2007-11-20', 'M', '2025-01-10'), ('Sneha Joshi', '9988776655', 'sneha.joshi@yahoo.com', '2009-03-03', 'F', DEFAULT), ('Aarav Khan', '9765432109', 'aarav.khan@outlook.com', '2006-08-15', 'M', '2025-02-01'); |
After running this, you should see:
|
0 1 2 3 4 5 6 |
Query OK, 4 rows affected |
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)
|
0 1 2 3 4 5 6 7 |
INSERT INTO students VALUES (NULL, 'Kavya Iyer', '9898989898', 'kavya.iyer@gmail.com', '2010-04-22', 'F', DEFAULT, DEFAULT, NULL); |
(Note: NULL for student_id because it’s AUTO_INCREMENT)
2. SELECT Statement – Reading / Retrieving Data (The most used command!)
Basic Syntax:
|
0 1 2 3 4 5 6 7 |
SELECT column1, column2, ... FROM table_name; |
Most common – See everything:
|
0 1 2 3 4 5 6 |
SELECT * FROM students; |
You should see something like this:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
+------------+---------------+------------+---------------------------+---------------+--------+-----------+------------+-------+ | student_id | full_name | phone | email | date_of_birth | gender | is_active | join_date | notes | +------------+---------------+------------+---------------------------+---------------+--------+-----------+------------+-------+ | 1 | Priya Sharma | 9876543210 | priya.sharma@gmail.com | 2008-05-12 | F | 1 | 2025-01-05 | NULL | | 2 | Rahul Patel | 9123456789 | rahul.p@gmail.com | 2007-11-20 | M | 1 | 2025-01-10 | NULL | | 3 | Sneha Joshi | 9988776655 | sneha.joshi@yahoo.com | 2009-03-03 | F | 1 | 2025-01-10 | NULL | | 4 | Aarav Khan | 9765432109 | aarav.khan@outlook.com | 2006-08-15 | M | 1 | 2025-02-01 | NULL | +------------+---------------+------------+---------------------------+---------------+--------+-----------+------------+-------+ |
Select specific columns:
|
0 1 2 3 4 5 6 7 |
SELECT full_name, phone, join_date FROM students; |
Select with alias (change display name):
|
0 1 2 3 4 5 6 7 |
SELECT full_name AS 'Student Name', phone AS 'Contact Number' FROM students; |
Select only active students:
|
0 1 2 3 4 5 6 |
SELECT * FROM students WHERE is_active = TRUE; |
(We’ll learn WHERE properly in the next chapter – for now just remember it filters rows)
3. UPDATE – Changing Existing Records
Syntax:
|
0 1 2 3 4 5 6 7 8 |
UPDATE table_name SET column1 = new_value, column2 = new_value, ... WHERE condition; |
Very important: Always use WHERE! If you forget WHERE, all rows will be updated — very dangerous!
Example – Update phone number of Priya Sharma
|
0 1 2 3 4 5 6 7 8 |
UPDATE students SET phone = '98765-43210' WHERE full_name = 'Priya Sharma'; |
Update multiple columns:
|
0 1 2 3 4 5 6 7 8 9 10 |
UPDATE students SET is_active = FALSE, notes = 'Left coaching due to relocation' WHERE student_id = 3; |
Update all students who joined before 2025-02-01:
|
0 1 2 3 4 5 6 7 8 |
UPDATE students SET is_active = TRUE WHERE join_date < '2025-02-01'; |
4. DELETE – Removing Records
Syntax:
|
0 1 2 3 4 5 6 7 |
DELETE FROM table_name WHERE condition; |
Again – always use WHERE! No WHERE = deletes everything!
Example – Delete one student:
|
0 1 2 3 4 5 6 7 |
DELETE FROM students WHERE student_id = 4; |
Delete all inactive students:
|
0 1 2 3 4 5 6 7 |
DELETE FROM students WHERE is_active = FALSE; |
Delete everything (careful!):
|
0 1 2 3 4 5 6 |
DELETE FROM students; |
(This empties the table but keeps the structure)
5. TRUNCATE TABLE – Completely Empty a Table (Faster than DELETE)
Syntax:
|
0 1 2 3 4 5 6 |
TRUNCATE TABLE table_name; |
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:
|
0 1 2 3 4 5 6 |
TRUNCATE TABLE students; |
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!)
- Make sure you’re in my_coaching database
- Run these commands one by one and copy-paste the output here:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
-- First clear the table if needed TRUNCATE TABLE students; -- Insert 5 students INSERT INTO students (full_name, phone, email, date_of_birth, gender, join_date) VALUES ('Priya Sharma', '9876543210', 'priya.sharma@gmail.com', '2008-05-12', 'F', '2025-01-05'), ('Rahul Patel', '9123456789', 'rahul.p@gmail.com', '2007-11-20', 'M', '2025-01-10'), ('Sneha Joshi', '9988776655', 'sneha.joshi@yahoo.com', '2009-03-03', 'F', '2025-02-15'), ('Aarav Khan', '9765432109', 'aarav.khan@outlook.com', '2006-08-15', 'M', '2025-03-01'), ('Kavya Iyer', '9898989898', 'kavya.iyer@gmail.com', '2010-04-22', 'F', '2025-03-10'); -- See all students SELECT * FROM students; -- Update Kavya's phone UPDATE students SET phone = '98989-89898' WHERE full_name = 'Kavya Iyer'; -- Delete Aarav DELETE FROM students WHERE full_name = 'Aarav Khan'; -- See final data SELECT * FROM students; |
