A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

5.1 INSERT, UPDATE, DELETE statements

 

5.1 INSERT, UPDATE, DELETE Statements

5.1.1 INSERT Statement

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

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example: Consider a table named Employees with columns EmployeeID, FirstName, and LastName.

-- Inserting a new employee
INSERT INTO Employees (EmployeeID, FirstName, LastName)
VALUES (1, 'John', 'Doe');

Query Output:

EmployeeIDFirstNameLastName
1JohnDoe

5.1.2 UPDATE Statement

The UPDATE statement is used to modify existing records in a table.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example: Continuing with the Employees table, let's update John Doe's last name.

-- Updating the last name of employee with ID 1
UPDATE Employees
SET LastName = 'Smith'
WHERE EmployeeID = 1;

Query Output:

EmployeeIDFirstNameLastName
1JohnSmith

5.1.3 DELETE Statement

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

Syntax:

DELETE FROM table_name WHERE condition;

Example: Removing the employee with ID 1 from the Employees table.

-- Deleting the employee with ID 1
DELETE FROM Employees WHERE EmployeeID = 1;

Query Output:

EmployeeIDFirstNameLastName
(No rows)(No rows)(No rows)

This table would indicate that there are no rows left after the DELETE operation.

Conclusion

Understanding how to use INSERT, UPDATE, and DELETE statements is essential for manipulating data within SQL Server tables. Always be cautious when using DELETE to avoid unintentional data loss.