How to Alter Multiple Columns at Once in SQL Server?
Last Updated :
16 Nov, 2021
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table of FIRM inside the database GeeksForGeeks. This table has 4 columns namely FIRST_NAME, LAST_NAME, SALARY, and BONUS containing the first names, last names, salaries, and bonuses of the members in a firm.
Query:
CREATE TABLE FIRM(
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
SALARY INT,
BONUS INT
);
Output:

Step 4: Describe the structure of the table FIRM.
Query:
EXEC SP_COLUMNS FIRM;
Output:

Step 5: Insert 5 rows into the FIRM table.
Query:
INSERT INTO FIRM VALUES('ALEX','STONE',10000,1000);
INSERT INTO FIRM VALUES('MATT','JONES',20000,2000);
INSERT INTO FIRM VALUES('JOHN','STARK',30000,3000);
INSERT INTO FIRM VALUES('GARY','SCOTT',40000,4000);
INSERT INTO FIRM VALUES('RICHARD','WALT',50000,5000);
Output:

Step 6: Display all the rows of the FIRM table.
Query:
SELECT * FROM FIRM;
Output:

Step 7: Alter multiple(2) columns of the table FIRM by adding 2 columns to the table simultaneously. The 2 columns are JOINING_DATE and LEAVING_DATE containing the date of joining of the member and the date of leaving of the member. Use the keyword ALTER and ADD to achieve this.
Syntax:
ALTER TABLE TABLE_NAME ADD COLUMN1
DATA_TYPE, COLUMN2 DATA_TYPE........;
Query:
ALTER TABLE FIRM ADD JOINING_DATE DATE,
LEAVING_DATE DATE;
Output:

Step 8: Describe the structure of the altered table FIRM.
Query:
EXEC SP_COLUMNS FIRM;
Note: The table description now has 2 extra columns.
Output:

Step 9: Update the table by inserting data into the 2 newly added columns of the FIRM table. Use keyword UPDATE.
Syntax:
UPDATE TABLE_NAME SET COLUMN1=VALUE,
COLUMN2=VALUE WHERE CONDITION;
Query:
UPDATE FIRM SET JOINING_DATE='01-JAN-2001',
LEAVING_DATE='01-JAN-2002' WHERE FIRST_NAME='ALEX';
UPDATE FIRM SET JOINING_DATE='02-FEB-2001',
LEAVING_DATE='02-FEB-2002' WHERE FIRST_NAME='MATT';
UPDATE FIRM SET JOINING_DATE='03-MAR-2001',
LEAVING_DATE='03-MAR-2002' WHERE FIRST_NAME='JOHN';
UPDATE FIRM SET JOINING_DATE='04-APR-2001',
LEAVING_DATE='04-APR-2002' WHERE FIRST_NAME='GARY';
UPDATE FIRM SET JOINING_DATE='05-MAY-2001',
LEAVING_DATE='05-MAY-2002' WHERE FIRST_NAME='RICHARD';
Output:

Step 10: Display all the rows of the altered FIRM table.
Query:
SELECT * FROM FIRM;
Note: The displayed table now has 2 extra columns.
Output:

Step 11: Alter multiple(2) columns of the table FIRM by dropping 2 columns from the table simultaneously. The 2 columns are JOINING_DATE and LEAVING_DATE containing the date of joining of the member and the date of leaving of the member. Use the keyword ALTER and DROP to achieve this.
Syntax:
ALTER TABLE TABLE_NAME DROP
COLUMN COLUMN1, COLUMN2........;
Query:
ALTER TABLE FIRM DROP COLUMN
JOINING_DATE,LEAVING_DATE;
Output:

Step 12: Describe the structure of the altered table FIRM.
Query:
EXEC SP_COLUMNS FIRM;
Note: The table description now has 2 fewer columns.
Output:

Step 13: Display all the rows of the altered FIRM table.
Query:
SELECT * FROM FIRM;
Note: The displayed table now has 2 fewer columns.
Output:

Similar Reads
How to Concatenate Text From Multiple Rows in SQL Server
When we fetch data from a table, there may be requirements to concatenate the text value of a table column in multiple rows into a single row. There are many ways we can concatenate multiple rows into single row SQL Server. We can use different ways based on need and convenience. In this article, we
6 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from
4 min read
How to Find the Maximum of Multiple Columns in SQL Server?
When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read
How to Rename Multiple Columns in R
Renaming columns in R Programming Language is a basic task when working with data frames, and it's done to make things clearer. Whether you want names to be more understandable, follow certain rules, or match your analysis, there are different ways to change column names. There are types of methods
3 min read
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
4 min read
Selecting Multiple Columns Based On Condition in SQL
SQL (Structured Query Language) is used to manage and query databases. One common requirement when querying data is selecting multiple columns based on specific conditions. Understanding how to use SQL for this purpose can enhance your ability to retrieve relevant data efficiently. In this article,
4 min read
How To Update Multiple Columns in MySQL?
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time. In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples,
3 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
SQL Query to Alter Column Size in MySQL
In MySQL, managing the structure of a database is crucial for optimal data organization and integrity. One common task is altering the size of a column to accommodate changing the data requirements. The ALTER TABLE statement and the MODIFY clause provide a straightforward method to achieve this. Her
3 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting
4 min read