2.3 - SQL Insert Update Delete
2.3 - SQL Insert Update Delete
The SQL INSERT INTO statement is one of the most essential commands for adding new data into a
database table. Whether you are working with customer records, product details or user information,
understanding and mastering this command is important for effective database management.
1. Inserting Data into All Columns: If you want to insert data into all columns of the table, you
can omit the column names and simply provide values for every column.
2. Inserting Data with Specific Columns: You can explicitly specify the columns you want to
insert data into, followed by the corresponding values.
The choice of method depends on whether you're inserting data for all the columns in a table or only
for a subset of them.
Syntax:
Example:
For better understanding, let's look at the SQL INSERT INTO statement with examples.
Let us first create a table named 'Student'.
Output
ROLL_NO NAME ADDRESS PHONE AGE
1 Ram Delhi xxxxxxxxxxxxxx 18
2 RAMESH GURGAON xxxxxxxxxxxxxx 18
3 SUJIT ROHTAK xxxxxxxxxxxxxx 20
4 SURESH ROHTAK xxxxxxxxxxxxxx 18
3 SUJIT ROHTAK xxxxxxxxxxxxxx 20
2 RAMESH GURGAON xxxxxxxxxxxxxx 18
If we don’t want to specify the column names (and you’re inserting data into all columns), we can
directly insert values in the order they appear in the table structure.
Here's an example:
Query:
INSERT INTO Student
VALUES ('5','HARSH','WEST BENGAL',
'XXXXXXXXXX','19');
Output
ROLL_NO NAME ADDRESS PHONE Age
1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
5 HARSH WEST BENGAL XXXXXXXXXX 19
Syntax
Output:
Note: Columns not included in the INSERT statement are filled with default values (typically NULL).
Example:
If we want to add multiple students to the Students table in one go, the query would
look like this:
INSERT INTO Student (ROLL_NO, NAME, AGE, ADDRESS,
PHONE)
VALUES
(6, 'Amit Kumar', 15, 'Delhi', 'XXXXXXXXXX'),
(7, 'Gauri Rao', 18, 'Bangalore', 'XXXXXXXXXX'),
(8, 'Manav Bhatt', 17, 'New Delhi', 'XXXXXXXXXX'),
(9, 'Riya Kapoor', 10, 'Udaipur', 'XXXXXXXXXX');
Output:
Explanation:
This method is faster than running multiple individual INSERT INTO commands.
If you're inserting more than 1000 rows, consider using bulk insert or multiple insert
statements for efficiency.
In SQL, the UPDATE statement is used to modify existing records in a table. Whether
you are updating a single record or multiple records at once, SQL provides the
necessary functionality to make these changes. Whether you are working with a small
dataset or handling large-scale databases, the UPDATE statement plays a crucial role
in maintaining the integrity of your data.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;
Parameters
UPDATE: The SQL command used to modify the data in a table.
SET: This clause defines which columns will be updated and what their new
values will be.
WHERE: The condition that determines which rows will be updated. Without it,
all rows will be affected.
Note: In the above query the SET statement is used to set new values to the
particular column and the WHERE clause is used to select the rows for which the
columns are needed to be updated. If we have not used the WHERE clause then the
columns in all the rows will be updated. So the WHERE clause is used to choose the
particular rows.
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
Output
.
Example 1: Update Single Column Using UPDATE Statement
We have a Customer table and we want to Update the CustomerName where the
Age is 22.
Query:
UPDATE Customer
SET CustomerName = 'Nitin'
WHERE Age = 22;
Output:
Explanation: Only the rows where Age is 22 will be updated, and
the CustomerName will be set to 'Nitin'.
We need to update both the CustomerName and Country for a specific CustomerID.
Query:
UPDATE Customer
SET CustomerName = 'Satyam',
Country = 'USA'
WHERE CustomerID = 1;
Output:
Explanation: For the row where CustomerID is 1, both CustomerName and Country will
be updated simultaneously.
Note: For updating multiple columns we have used comma(,) to separate the names
and values of two columns.
If we accidentally omit the WHERE clause, all the rows in the table will be updated,
which is a common mistake. Let’s update the CustomerName for every record in the
table:
Query:
UPDATE Customer
SET CustomerName = 'Shubham';
Output
Explanation: This will set the CustomerName for every row in the Customer table to
'Shubham'. Be careful while omitting the WHERE clause, as this action is irreversible
unless you have a backup.
2. Check your data before updating: Run a SELECT query to view the data you want
to update before executing the UPDATE statement. This helps avoid accidental data
modifications.
SELECT * FROM Customer WHERE Age = 22;
3. Use transactions for critical updates: When performing updates in a production
environment, consider using transactions. Transactions allow you to commit or roll
back changes as needed.
BEGIN TRANSACTION;
UPDATE Customer SET CustomerName = 'John'
WHERE CustomerID = 3;
COMMIT; -- Use ROLLBACK to undo if
necessary
The SQL DELETE statement is an essential command in SQL used to remove one or
more rows from a database table. Unlike the DROP statement, which removes the
entire table, the DELETE statement removes data (rows) from the table retaining only
the table structure, constraints, and schema. Whether you need to delete a single row
based on a condition or remove an entire dataset, understanding this command will
help you manage your data effectively.
Syntax:
DELETE FROM table_name
WHERE some_condition;
Parameter Explanation
Some_condition: A condition used to filter the rows you want to delete.
table_name: The name of the table from which you want to delete the rows
Note: We can delete single as well as multiple records depending on the condition we
provide in the WHERE clause. If we omit the WHERE clause then all of the records
will be deleted and the table will be empty.
Query:
DELETE FROM GFG_Employees
WHERE NAME = 'Rithvik';
Output:
Query
DELETE FROM GFG_Employees
WHERE department = 'Development';
Output
Query:
DELETE FROM GFG_Employees;
Or
DELETE * FROM GFG_Employees;
Output:
All of the records in the table will be deleted, there are no records left to display.
The table GFG_Employees will become empty.
Query:
START TRANSACTION;
DELETE FROM GFG_Employees WHERE department =
'Development';
-- If needed, you can rollback the deletion
ROLLBACK;
Explanation: The ROLLBACK command will undo the changes made by the DELETE statement,
effectively restoring the records that were deleted during the transaction.