Open In App

How to Update a Column in a Table in SQL Server

In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out changes accurately is essential.

In this article, we will learn about Updating a Column in a Table with multiple examples and their best practices in detail.



What is an UPDATE Statement in SQL Server

In SQL Server, the UPDATE statement is the tool that allows the changing of the existing records in a table:

UPDATE table_name
SET column1 = value1, column2 = value2, ...an

Explanation:



Example of Updating a Column in a Table

CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_department VARCHAR(50),
emp_salary DECIMAL(10, 2)
);

INSERT INTO employees (emp_id, emp_name, emp_department, emp_salary)
VALUES
(1, 'John Smith', 'Marketing', 50000),
(2, 'Alice Wang', 'Sales', 55000),
(3, 'Emma Brown', 'HR', 48000);

Output:

emp_id | emp_name    | emp_department | emp_salary
-------|-------------|----------------|-----------
1 | John Smith | Marketing | 50000
2 | Alice Wang | Sales | 55000
3 | Emma Brown | HR | 48000

Example 1: Updating an Employee’s Salary

Suppose John Smith’s salary needs to be increased to $52000:

UPDATE employees
SET emp_salary = 52000
WHERE emp_name = 'John Smith';

Output:

emp_id | emp_name    | emp_department | emp_salary
-------|-------------|----------------|-----------
1 | John Smith | Marketing | 52000
2 | Alice Wang | Sales | 55000
3 | Emma Brown | HR | 48000

Explanation:

Example 2: Changing an Employee’s Department

Suppose Emma Brown is transferred from HR to Finance department:

UPDATE employees
SET emp_department = 'Finance'
WHERE emp_name = 'Emma Brown';

Output:

emp_id | emp_name    | emp_department | emp_salary
-------|-------------|----------------|-----------
1 | John Smith | Marketing | 52000
2 | Alice Wang | Sales | 55000
3 | Emma Brown | Finance | 48000

Explanation:

Example 3: Incrementing Salary for All Employees

Suppose there’s a company-wide salary increment of 5%:

UPDATE employees
SET emp_salary = emp_salary * 1.05;

Output:

emp_id | emp_name    | emp_department | emp_salary
-------|-------------|----------------|-----------
1 | John Smith | Marketing | 54600
2 | Alice Wang | Sales | 57750
3 | Emma Brown | Finance | 50400

Explanation:

Best Practices for Updating Columns

Adhering to best practices not only ensures the accuracy of updates but also enhances the efficiency of database operations:

Conclusion

Changes in database columns is a matter of database management and implies the possibility of changing data and making data flexible. Understanding the SQL UPDATE statement and its syntax, as well as practical examples, can make you to perform changes to existing table records in a simple manner. Among the best practices are establishment of criteria for explicit criteria, transaction management, and backup, which enriches the integrity and efficiency of the update operation and promotes a robust database management.

Article Tags :