Skip to content

DML in SQL Server: INSERT, UPDATE, DELETE

DML commands change the DATA inside tables.

INSERT -> add rows
UPDATE -> change rows
DELETE -> remove rows

This lesson creates its own small tables, so you can run it on any SQL Server database. The Output under each query is the real result.

IF DB_ID('SqlBasicsDb') IS NULL
    CREATE DATABASE SqlBasicsDb;
GO

USE SqlBasicsDb;
GO

DROP TABLE IF EXISTS Products;
GO

CREATE TABLE Products
(
    ProductID   INT PRIMARY KEY,
    ProductName VARCHAR(50),
    Price       INT,
    Stock       INT
);
GO

INSERT one row

INSERT INTO Products (ProductID, ProductName, Price, Stock)
VALUES (1, 'Pen', 10, 100);

SELECT * FROM Products;
Messages
(1 row affected)
Output
ProductIDProductNamePriceStock
1Pen10100

INSERT many rows at once

INSERT INTO Products (ProductID, ProductName, Price, Stock)
VALUES (2, 'Pencil',   5,  200),
       (3, 'Notebook', 50, 80),
       (4, 'Eraser',   3,  150),
       (5, 'Bag',      500, 10);

SELECT * FROM Products;
Messages
(4 rows affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil5200
3Notebook5080
4Eraser3150
5Bag50010

INSERT only some columns (others become NULL)

INSERT INTO Products (ProductID, ProductName)
VALUES (6, 'Ruler');

SELECT * FROM Products;     -- Ruler has NULL price and stock
Messages
(1 row affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil5200
3Notebook5080
4Eraser3150
5Bag50010
6RulerNULLNULL

UPDATE one row

UPDATE Products
SET Price = 8, Stock = 50
WHERE ProductID = 6;

SELECT * FROM Products;
Messages
(1 row affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil5200
3Notebook5080
4Eraser3150
5Bag50010
6Ruler850

UPDATE many rows (increase price of cheap items by 2)

UPDATE Products
SET Price = Price + 2
WHERE Price < 10;

SELECT * FROM Products;
Messages
(3 rows affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil7200
3Notebook5080
4Eraser5150
5Bag50010
6Ruler1050

DELETE one row

DELETE FROM Products
WHERE ProductID = 4;

SELECT * FROM Products;     -- Eraser is gone
Messages
(1 row affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil7200
3Notebook5080
5Bag50010
6Ruler1050

DELETE many rows

DELETE FROM Products
WHERE Stock < 20;

SELECT * FROM Products;     -- Bag is gone

-- !!! WARNING !!!
-- UPDATE or DELETE without WHERE changes EVERY row:
--   DELETE FROM Products;            -- deletes all rows
--   UPDATE Products SET Price = 0;   -- every price becomes 0
-- Always write the WHERE first, and check it with a SELECT.
Messages
(1 row affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil7200
3Notebook5080
6Ruler1050

Copy rows into a new table (SELECT INTO)

DROP TABLE IF EXISTS ProductsBackup;
GO

SELECT * INTO ProductsBackup FROM Products;

SELECT * FROM ProductsBackup;

-- =============================================
-- DELETE vs TRUNCATE vs DROP
--   DELETE   -> removes rows (all or with WHERE), table stays    (DML)
--   TRUNCATE -> removes ALL rows, no WHERE, faster, table stays  (DDL)
--   DROP     -> removes the whole table                          (DDL)
-- =============================================
Messages
(4 rows affected)
Output
ProductIDProductNamePriceStock
1Pen10100
2Pencil7200
3Notebook5080
6Ruler1050