Inserting and Managing Data
SQL Basics Course
Topic 7: Inserting and Managing Data
Learn how to add, update, and remove records from database tables.
Learning Objectives
- Insert records into tables
- Insert multiple rows
- Update existing data
- Delete records safely
- Understand common data management mistakes
INSERT INTO Statement
The INSERT INTO statement is used to add new records into a table.
INSERT INTO students (id, name, city)
VALUES (1, 'Rahul', 'Delhi');
Viewing the Inserted Data
SELECT * FROM students;
| ID | Name | City |
|---|---|---|
| 1 | Rahul | Delhi |
Inserting Multiple Records
INSERT INTO students (id, name, city)
VALUES
(1, 'Rahul', 'Delhi'),
(2, 'Amit', 'Mumbai'),
(3, 'Priya', 'Pune');
This inserts three records with a single query.
UPDATE Statement
The UPDATE statement modifies existing records.
UPDATE students
SET city = 'Jaipur'
WHERE id = 1;
The WHERE clause is important to specify which record should be updated.
UPDATE Without WHERE
UPDATE students
SET city = 'Delhi';
Warning: This updates every row in the table.
DELETE Statement
DELETE FROM students
WHERE id = 2;
The specified record is permanently removed.
DELETE Without WHERE
DELETE FROM students;
Warning: This removes all rows from the table.
Before and After Example
| ID | Name | City |
|---|---|---|
| 1 | Rahul | Delhi |
| 2 | Amit | Mumbai |
After updating Rahul's city:
| ID | Name | City |
|---|---|---|
| 1 | Rahul | Jaipur |
| 2 | Amit | Mumbai |
Data Integrity Tips
- Always verify values before inserting.
- Use WHERE when updating data.
- Use WHERE when deleting data.
- Backup important information.
- Validate user input.
Common Mistakes
- Missing WHERE clause.
- Wrong column order.
- Duplicate primary keys.
- Incorrect data types.
- Deleting data accidentally.
Practice Exercises
- Insert three employee records.
- Update one employee's salary.
- Delete one employee record.
- Insert five student records.
- Update a city value.
Quiz
1. Which command inserts new records?
2. Which command modifies records?
3. Which command removes records?
4. Why is WHERE important?
5. Can INSERT add multiple rows at once?
Summary
- INSERT INTO adds new records.
- UPDATE modifies existing records.
- DELETE removes records.
- WHERE controls which rows are affected.
- Always verify data before making changes.