SQL Server ALTER TABLE ADD Column
Last Updated :
01 Dec, 2023
The ALTER TABLE ADD is a Data Definition Language (DDL) command that is used to alter the structure of the table by adding an extra column based on the new requirement. Using ALTER TABLE ADD we can also add new constraints and also indexes for the table. With the help of this command, We can simply apply modifications to the columns of our tables.
Syntax:
ALTER TABLE table_name
ADD column1 data_type , ADD column2 data_type
Explanation: In the above query, We have added two columns called column1 and column2 in table table_name. We follow this syntax for adding columns to our table. We will understand with the help of examples.
Usage of ALTER TABLE ADD Column
Step 1: Create database Geeksforgeeks by using the following SQL query
CREATE DATABASE Geeksforgeeks
Step 2: Use the GFG Database.
USE Geeksforgeeks
Step 3: Create a table with the name studentsMark which contains the studentName along with their details
CREATE TABLE studentsMarks
(
studentId INT PRIMARY KEY,
studentName VARCHAR(100),
courseId VARCHAR(100),
marksObtained INT,
);
Step 4 : Inserting Sample Data into the table
INSERT INTO studentsMarks
VALUES (19104060, 'Student1', 'ECPC-1001', 25),
(19104061, 'Student2', 'ECPC-1001',96),
(19104062, 'Student3', 'ECPC-1001',81),
(19104063, 'Student4', 'ECPC-1001',85),
(19104064, 'Student5', 'ECPC-1001', 86),
(19104065, 'Student6', 'ECPC-1001', 55),
(19104066, 'Student7', 'ECPC-1001',70),
(19104067, 'Student8', 'ECPC-1001',71),
(19104068, 'Student9', 'ECPC-1001',65),
(19104069, 'Student10', 'ECPC-1001',68);
Step 5 : Check the table
SELECT * FROM studentsMarks
The Result Looks Like:
StudentMark TableAdding a Single Column
Let's add the new column name gradeObtained into our existing table called studentsMarks, the values of gradeObtained column are initially NULL.
Query
ALTER TABLE studentsMarks
ADD gradeObtained VARCHAR(50)
SELECT * FROM studentsMarks
The Result Looks Like:
Table After Adding Single ColumnExplanation: This query adds the gradeObtained column in the table
Adding a Multiple Column
Let's see how we add multiple columns with constraints into our table.
Query
ALTER TABLE studentsMarks
ADD gradeLockStatus VARCHAR(50),
backLogs INT CONSTRAINT backLogConstraint DEFAULT 0 NOT NULL,
remarks VARCHAR(50) CONSTRAINT remarkConstraint DEFAULT 'NA' NOT NULL
SELECT * FROM studentsMarks
The Result Looks Like:
Table After Adding Multiple Column
Explanation: After executing this query, the studentsMarks
table will have the additional columns gradeLockStatus
, backLogs
, and remarks
with the specified data types, default values, and constraints.
Conclusion
The ALTER TABLE ADD command plays a key role when we want to make any column additions for the existing Table as the requirements come on updating the applications. Sometimes it might happen that at the time of table creation, we forget to add some columns to our table. We resolve this problem by using ALTER TABLE ADD command, which allows us to add columns to our table.
Similar Reads
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read
SQL Server ALTER TABLE
In SQL Server, there are various commands to Add, Update, and Delete a Database Schema called DDL or Data Definition Language commands. A Table in a database is part of the database schema and the 'ALTER TABLE Moify Column' command is used to make changes to the database table column data type, colu
5 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
PostgreSQL - ALTER TABLE
In PostgreSQL, the ALTER TABLE statement is a powerful and essential tool that allows us to modify the structure of an existing table to meet evolving database needs. With PostgreSQL ALTER TABLE, we can perform various modifications on the table without disrupting the ongoing operations of our datab
6 min read
SQL Server Describe Table
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting informati
4 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
SQL Server Rename Table
In SQL Server, renaming tables is a frequent operation that we often require during database maintenance or schema changes. This article ensures your seamless transition through the table-renaming process without compromising data integrity. it provides comprehensive guidance and guarantees protecti
2 min read
Update Date Field in SQL Server
The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statements as per our requirement. With this article, we will learn how to Update the Date Field in SQL Server. In this article, we w
2 min read
SQL ALTER COLUMN
In today's data-driven world, successful organizations require the ability to process and manipulate data effectively as part of their business foundation. Structured Query Language (SQL) is a powerful tool that enables users to manipulate relational databases. Within SQL, there is an entity where d
4 min read
How to Check if a Column Exists in a SQL Server Table?
In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data. Creating table: Syntax: CREATE TABLE table_name ( column1 datatype,
2 min read