INSERT, UPDATE & DELETE
INSERT, UPDATE and DELETE are important SQL commands used to add, modify and remove records from database tables.
These commands are part of Data Manipulation Language (DML). They allow applications and developers to work with the actual data stored inside SQL Server tables.
What is DML?
DML stands for Data Manipulation Language. It is used to manipulate the records stored inside database tables.
The three commonly used DML commands covered in this section are:
- INSERT – Used to add new records.
- UPDATE – Used to modify existing records.
- DELETE – Used to remove records.
Sample Students Table
We will use the following Students table to understand INSERT, UPDATE and DELETE operations.
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 22 | Pune |
| 102 | Priya | 23 | Mumbai |
| 103 | Amit | 21 | Pune |
1. INSERT Statement
The INSERT statement is used to add new records into a database table.
When a new student, employee, customer or product needs to be stored in a table, we can use the INSERT statement.
INSERT Syntax
INSERT INTO table_name
(
column1,
column2,
column3
)
VALUES
(
value1,
value2,
value3
);
INSERT Example
Suppose we want to add a new student to the Students table.
INSERT INTO Students
(
StudentId,
StudentName,
Age,
City
)
VALUES
(
104,
'Neha',
22,
'Nashik'
);
Output
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 22 | Pune |
| 102 | Priya | 23 | Mumbai |
| 103 | Amit | 21 | Pune |
| 104 | Neha | 22 | Nashik |
Insert Multiple Records
SQL Server also allows multiple records to be inserted using a single INSERT statement.
INSERT INTO Students
(
StudentId,
StudentName,
Age,
City
)
VALUES
(105, 'Kiran', 24, 'Pune'),
(106, 'Sneha', 22, 'Mumbai'),
(107, 'Aakash', 23, 'Nashik');
2. UPDATE Statement
The UPDATE statement is used to modify existing records in a table.
For example, if a student's city or age changes, we can update the existing record instead of inserting a new one.
UPDATE Syntax
UPDATE table_name
SET column_name = value
WHERE condition;
UPDATE Example
Suppose Rahul's age changes from 22 to 23.
UPDATE Students
SET Age = 23
WHERE StudentId = 101;
Output
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 23 | Pune |
Update Multiple Columns
Multiple columns can be modified using the same UPDATE statement.
UPDATE Students
SET Age = 24,
City = 'Mumbai'
WHERE StudentId = 101;
This query changes both the Age and City of the student whose StudentId is 101.
UPDATE Without WHERE
If the WHERE clause is not specified, the UPDATE statement can modify the selected column for every row in the table.
UPDATE Students
SET City = 'Pune';
3. DELETE Statement
The DELETE statement is used to remove records from a database table.
We can delete a specific record by using a WHERE condition.
DELETE Syntax
DELETE FROM table_name
WHERE condition;
DELETE Example
Suppose we want to remove the student whose StudentId is 104.
DELETE FROM Students
WHERE StudentId = 104;
Output
After executing the query, the record with StudentId 104 will no longer exist in the table.
DELETE Without WHERE
If DELETE is executed without a WHERE condition, all rows from the table are targeted for deletion.
DELETE FROM Students;
INSERT vs UPDATE vs DELETE
| Command | Purpose | Operation | Example |
|---|---|---|---|
| INSERT | Adds new records. | Creates rows. | Add new student |
| UPDATE | Modifies existing records. | Changes rows. | Change student age |
| DELETE | Removes records. | Deletes rows. | Remove student |
INSERT, UPDATE & DELETE Flow
These three commands perform different operations on table data.
Table
|
------------------------
| | |
INSERT UPDATE DELETE
| | |
Add Modify Remove
| | |
------------------------
|
Table Data
Example 🌍🤓
Consider a Student Management System.
| Situation | SQL Command |
|---|---|
| New student joins the institute | INSERT |
| Student changes city | UPDATE |
| Student record needs to be removed | DELETE |
Important Points
- INSERT is used to add new records.
- UPDATE is used to modify existing records.
- DELETE is used to remove records.
- Use WHERE carefully with UPDATE.
- Use WHERE carefully with DELETE.
Summary
In this section, we learned how INSERT is used to add records, UPDATE is used to modify records and DELETE is used to remove records. We also learned their syntax, practical examples, output and important precautions when using WHERE conditions.