Data Manipulation Language (DML)
Explore the core Data Manipulation Language commands in SQL Server. Learn how to insert new data, update existing records carefully, and delete data using conditions to avoid errors. This lesson helps you gain practical skills for manipulating table data safely and effectively.
We'll cover the following...
Data Manipulation Language (DML) is precisely what it sounds like; it manipulates the data. These statements are part of DML and allow us to modify existing data, create new data, or even delete it.
Note: DML is not specific to objects like modifying a table’s structure or a database’s settings but works with the data in the tables.
The three data manipulation statements are:
INSERTUPDATEDELETE
The following sections discuss each of the above DML statements one by one.
The INSERT statement
The INSERT statement allows us to insert data into the tables of a particular database. We use the following syntax to insert three rows into the table in lines 5–7 below:
The UPDATE statement
The UPDATE statement allows us to update existing data. For instance, we may change an existing piece of data in a table to another value.
But we should be careful when updating values in a table as we could set all rows within one column to the same value if we don’t specify a condition in the WHERE clause.
Note: We can always check which values we’re updating by swapping out
UPDATEwithSELECTand commenting out the line that begins withSET, so long as we’re using theWHEREclause. When updating one value, it’s best to use the primary key as a condition in theWHEREclause.
We can update a value or set of values in one column of a table using the following syntax:
The DELETE statement
The DELETE statement deletes data from a specified object, like a table. It’s best to use to be careful when running the DELETE statement.
If we don’t use the WHERE clause in the query, we’ll delete all of the data within the table. So, it’s always best to use a WHERE clause in a DELETE statement.
Note: Much like the
UPDATEstatement, we can also swap outDELETEwithSELECTin the statement to see what data it would affect, so long as we’re using theWHEREclause in the query. Like theUPDATEstatement, when deleting one value, it’s best to use the primary key as a condition in theWHEREclause.
We use the following syntax to delete a certain value from a table using the WHERE clause:
To delete all the values in the table, use:
Let's see how to execute the above-mentioned DML commands by clicking the “Run” button below:
-- We have used additional SELECT statements to show the effect of the queries
CREATE DATABASE Educative -- Creating database Educative
GO
CREATE SCHEMA Ed -- Creating schema Ed
GO
CREATE TABLE Ed.Department -- Creating Table Department
(
DepId INT PRIMARY KEY,
DepName VARCHAR(30)
)
GO
SELECT * FROM Ed.Department -- Select statement to see that the table has been created
GO
ALTER DATABASE Educative MODIFY NAME = EducativeIO -- changing database name
GO
ALTER TABLE Ed.Department -- Altering the table by adding a new column
ADD DepFloor INT
GO
SELECT * FROM Ed.Department -- Select statement to see that the new column has been added
GO
SELECT "--INSERT--"
-- Insert values in the table
INSERT INTO Ed.Department
(DepId, DepName, DepFloor) -- Mention the columns to insert values in.
VALUES
(1, 'Content', 1),
(2, 'Development', 2),
(3, 'Human Resource', 3)
GO
SELECT * FROM Ed.Department -- Select statement to see that the data has been inserted.
GO
SELECT "--UPDATE--"
-- Update the Department
UPDATE Ed.Department
SET
DepName = 'Dev', -- Specify the new value here.
DepFloor = '4'
WHERE
DepId = '1'
GO
SELECT * FROM Ed.Department -- Select statement to see that the table has been updated.
GO
SELECT "--DELETE --"
-- Delete a value from Department
DELETE FROM Ed.Department
WHERE DepFloor = 3
GO
SELECT * FROM Ed.Department -- Select statement to see that the records have been deleted.
GO