7. DML Deep Dive — INSERT, UPDATE, DELETE & TRUNCATE
DML Deep Dive — INSERT, UPDATE, DELETE & TRUNCATE
You have built the structure. Now you fill it with life. DML — Data Manipulation Language is the set of SQL commands that put data into your tables, change it, and remove it. These are the commands you will run hundreds of times every day in any database-driven system. A deep understanding of DML — including the subtle differences between commands that look similar — separates beginners from professionals.
Open SSMS, connect to your server, and follow every step in this module hands-on. Do not just read — type every command and observe exactly what SQL Server does.
Getting Ready in SSMS
Open SSMS and connect to your server using Windows Authentication. In Object Explorer, expand Databases and confirm VoidX_Academy is there with the Department and Student tables from Modules 5 and 6.
Click New Query to open a fresh editor. Run this first:
<div class='code-block' data-lang='text'> USE VoidX_Academy;
Press F5. The database dropdown in the top-left of SSMS now shows VoidX_Academy. All commands in this module will run inside this database.
INSERT — Putting Data Into a Table
The INSERT command adds new rows to a table. The basic structure is always the same: the table name, a list of column names, and a matching list of values.
Insert one department:
<div class='code-block' data-lang='text'> INSERT INTO Department (DeptID, DeptName, Location) VALUES (1, 'Computer Science', 'Science Block A');
Press F5. The Messages tab should say: (1 row affected). That confirms one row was added successfully.
Let us examine every word of this command so you understand it completely.
INSERT INTO Department — the keyword INSERT INTO tells SQL Server you are adding data, and Department is the table receiving the new row.
(DeptID, DeptName, Location) — this is the column list. You are explicitly telling SQL Server which columns the values belong to. The order of this list matters — the values must appear in the same order.
VALUES (1, <strong><code>Computer Science, Science Block A) — these are the actual values being inserted. The number 1 goes into DeptID, the text Computer Science goes into DeptName, and Science Block A goes into Location. Text values must always be wrapped in single quote marks.
INSERT — Adding Multiple Rows at Once
You can insert several rows in a single INSERT statement by separating each set of values with a comma. This is much more efficient than running a separate INSERT for each row because it makes only one trip to the database instead of several.
<div class='code-block' data-lang='text'> INSERT INTO Department (DeptID, DeptName, Location) VALUES (2, 'Information Technology', 'Science Block B'), (3, 'Mathematics', 'Math Building'), (4, 'Physics', 'Science Block C');
Press F5. The Messages tab says: (3 rows affected). Three rows were inserted with a single command.
To verify all your departments are there, run a quick SELECT — which you will learn in full detail in Module 8:
<div class='code-block' data-lang='text'> SELECT * FROM Department;
A results grid appears at the bottom of SSMS showing all four departments.
INSERT — When Column Names Are Optional
If you provide values for every column in the table in the exact order they were defined, you can omit the column list. SQL Server assumes you are providing all values in column order.
<div class='code-block' data-lang='text'> INSERT INTO Department VALUES (5, 'Chemistry', 'Science Block D');
This works — but it is fragile. If the table structure ever changes and columns are added or reordered, this command will fail or insert data into the wrong columns. Best practice is to always write the column list explicitly. It takes a few extra seconds but makes your code reliable and clear to anyone reading it later.
INSERT — Working With IDENTITY Columns and Defaults
In Module 5, you created the Student table with an IDENTITY column for StudentID and DEFAULT constraints for IsActive and EnrolledAt. When inserting into a table with these features, you simply leave those columns out of your INSERT statement entirely.
<div class='code-block' data-lang='text'> INSERT INTO Student (FirstName, LastName, Email, DateOfBirth, DeptID) VALUES ('Kofi', 'Mensah', 'kofi.mensah@ucc.edu.gh', '2002-05-14', 1);
Press F5. Notice StudentID is not in the column list — SQL Server generates it automatically. IsActive is not listed either — it defaults to 1 (active). EnrolledAt is not listed — it defaults to the current date and time.
Now add two more students in one statement:
<div class='code-block' data-lang='text'> INSERT INTO Student (FirstName, LastName, Email, DateOfBirth, DeptID) VALUES ('Ama', 'Darko', 'ama.darko@ucc.edu.gh', '2003-01-22', 2), ('Kwame', 'Asante', 'kwame.asante@ucc.edu.gh', '2001-11-08', 1);
Press F5. Check the results:
<div class='code-block' data-lang='text'> SELECT * FROM Student;
You will see three students with automatically generated StudentIDs 1, 2, and 3.
INSERT — Deliberately Inserting NULL
When a column has no NOT NULL constraint, you can explicitly insert NULL to represent the absence of a value. Add a student who has not yet been assigned to a department:
<div class='code-block' data-lang='text'> INSERT INTO Student (FirstName, LastName, Email) VALUES ('Efua', 'Boateng', 'efua.boateng@ucc.edu.gh');
DeptID is left out of the column list — since it has no DEFAULT and no NOT NULL constraint, it becomes NULL automatically. You can also write it explicitly:
<div class='code-block' data-lang='text'> INSERT INTO Student (FirstName, LastName, Email, DeptID) VALUES ('Yaw', 'Amponsah', 'yaw.amponsah@ucc.edu.gh', NULL);