Bhubaneswar, Odisha, India
+91 8637 274 400

Mastering Data Manipulation: INSERT, UPDATE, DELETE in SQL Server

Mastering Data Manipulation: INSERT, UPDATE, DELETE in SQL Server

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.

Tags: SQL Server, data manipulation, INSERT, UPDATE, DELETE

Introduction: In the realm of SQL Server, data manipulation is a critical skill. Whether you’re adding new data, updating existing records, or deleting obsolete information, mastering data manipulation commands is essential. This blog will cover the INSERT, UPDATE, and DELETE statements with practical examples.

Creating Tables:

CREATE TABLE Syntax:

The CREATE TABLE statement is used to create a new table in the database.

 CREATE TABLE table_name (

    column1 datatype constraint,

    column2 datatype constraint,

    ...

    columnN datatype constraint

);

Example:

CREATE TABLE Employees (

    EmployeeID INT PRIMARY KEY,

    FirstName NVARCHAR(50),

    LastName NVARCHAR(50),

    Department NVARCHAR(50)

);

INSERT Command

The INSERT statement is used to add new records to a table.

INSERT INTO Syntax

INSERT INTO table_name (column1, column2, ..., columnN)

VALUES (value1, value2, ..., valueN);

Here’s a basic example:

INSERT INTO Employees (FirstName, LastName, Department)
VALUES ('John', 'Doe', 'Marketing');

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.

This query inserts a new record into the Employees table with the specified values for FirstName, LastName, and Department. If you need to insert multiple records, you can do so in a single query:

INSERT INTO Employees (FirstName, LastName, Department)
VALUES 
('Jane', 'Smith', 'Sales'),
('Michael', 'Brown', 'IT'),
('Emily', 'Davis', 'HR');

UPDATE Command

The UPDATE statement is used to modify existing records.

UPDATE Syntax:

UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

Suppose you want to change the department of an employee:

UPDATE Employees
SET Department = 'Sales'
WHERE EmployeeID = 1;

This query updates the Department of the employee with EmployeeID 1 to ‘Sales’. You can update multiple columns at once:

UPDATE Employees
SET Department = 'Sales', LastName = 'Doe'
WHERE EmployeeID = 1;

This query changes both the Department and LastName for the employee with EmployeeID 1.

DELETE Command

The DELETE statement is used to remove records from a table.

DELETE Syntax:

DELETE FROM table_name

WHERE condition;

For instance, to delete an employee record:

DELETE FROM Employees
WHERE EmployeeID = 1;

However, it’s often safer to use the TRUNCATE TABLE command to remove all rows from a table without logging individual row deletions:

Combining Data Manipulation Commands

You can combine these commands to perform complex operations. For example, you might insert new records, update existing ones, and delete outdated data as part of a data refresh process.

By mastering these data manipulation commands, you’ll be well-equipped to manage your data in SQL Server efficiently. Practice these commands and explore their variations to deepen your understanding.

Are you looking for Live Training in C#?

The upcoming batch for C#.NET is sceduled.

Book Your Seat Now.