Menu
Courses / sql-basic / Inserting and Managing Data

Inserting and Managing Data

07 / 14 Part of sql-basic






SQL Topic 7 - 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;













IDNameCity
1RahulDelhi




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



















IDNameCity
1RahulDelhi
2AmitMumbai


After updating Rahul's city:



















IDNameCity
1RahulJaipur
2AmitMumbai





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



  1. Insert three employee records.

  2. Update one employee's salary.

  3. Delete one employee record.

  4. Insert five student records.

  5. 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.