×

How to add column in table in SQL ?

How to add column in table in SQL 

Introduction

  • To add a column in already created table, one needs to use the ALTER command along with the ADD clause.
  • If in the query, it is not specified where the new column is to be added, then by default it will be added as a last column.
  • One can also add a new column at the first or even after a specific column of an already created table.
  • You can add one column or more than one columns at a time using a single SQL query.

(A). Adding a new column at the last of an existing table

Syntax:

ALTER TABLE tablename ADD (ColumnName datatype);

         where,

  1. Tablename is the name of an already existing table to which you have to add a new column.
  2. Column_name is the name of the column which is to be added to an already existing table.

Example:

First, we will create a database with name “studentdb”. Then in that database we will create a table “student” and insert records into the table. We will consider the same database and also the same table for subsequent examples.

Now, we will add a new column ‘City’ to an existing table.

 mysql> USE studentdb;
 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD (City VARCHAR(20));
 Query OK, 4 rows affected (0.29 sec)
 Records: 4  Duplicates: 0  Warnings: 0
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City |
 +---------+-----------+-----------+-------------+------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |
 |       2 | Shweta    |       102 | CN          | NULL |
 |       3 | Nikita    |       103 | OS          | NULL |
 |       4 | Ankita    |       104 | C           | NULL |
 +---------+-----------+-----------+-------------+------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘City’ is added to an existing student table. Since, in the query we haven’t specified where it is to be added so by default it is added as the last column.

(B)Adding more than one column to an existing table        

Syntax:

ALTER TABLE tablename ADD (ColumnName1 datatype, ColumnName2 datatype);

Example:

Now, we will add two new columns ‘City’ and ‘Marks’ to an existing table using a single query.

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD (City VARCHAR(20),Marks INT);
 Query OK, 4 rows affected (0.40 sec)
 mysql> SELECT *FROM student; 

Output:

 +---------+-----------+-----------+-------------+------+-------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City | Marks |
 +---------+-----------+-----------+-------------+------+-------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |  NULL |
 |       2 | Shweta    |       102 | CN          | NULL |  NULL |
 |       3 | Nikita    |       103 | OS          | NULL |  NULL |
 |       4 | Ankita    |       104 | C           | NULL |  NULL |
 +---------+-----------+-----------+-------------+------+-------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New columns ‘City’ and ‘Marks’ are added to an existing student table. Since, in the query we haven’t specified where it is to be added so by default both the columns are added at the last using a single query.

(C) Adding a column at the first position of an existing table

Syntax:

ALTER TABLE tablename ADD ColumnName datatype FIRST;

Example: Now, we will add a new column ‘Sr_No’ to an existing table as a first column.

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 +---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD Sr_No INT FIRST; 

Output:

 Query OK, 4 rows affected (0.24 sec)
 Records: 4  Duplicates: 0  Warnings: 0
 mysql> SELECT *FROM student; 

Output:

 +-------+---------+-----------+-----------+-------------+
 | Sr_No | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +-------+---------+-----------+-----------+-------------+
 |  NULL |       1 | Prajakta  |       101 | DBMS        |
 |  NULL |       2 | Shweta    |       102 | CN          |
 |  NULL |       3 | Nikita    |       103 | OS          |
 |  NULL |       4 | Ankita    |       104 | C           |
 +-------+---------+-----------+-----------+-------------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘Sr_No’ is added to an existing student table. Since, in the query we have specified ‘FIRST’ keyword so ‘Sr_No’ is added as a first column.

(D) Adding a new column after a specific column of an existing table

Syntax:

ALTER TABLE tablename ADD ColumnName datatype AFTER column_name;

Example:

Now, we will add a new column ‘Marks’ to an existing table after the  ‘Course_Name’ .

mysql> USE studentdb;

Output:

 Database changed
 mysql> SELECT *FROM student; 

Output:

 +---------+-----------+-----------+-------------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City |
 +---------+-----------+-----------+-------------+------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |
 |       2 | Shweta    |       102 | CN          | NULL |
 |       3 | Nikita    |       103 | OS          | NULL |
 |       4 | Ankita    |       104 | C           | NULL |
 +---------+-----------+-----------+-------------+------+
 4 rows in set (0.00 sec)
 mysql> ALTER TABLE student ADD Marks INT AFTER Course_Name; 

Output:

 Query OK, 4 rows affected (0.28 sec)
 Records: 4  Duplicates: 0  Warnings: 0 
mysql> SELECT *FROM student;

Output:

 +---------+-----------+-----------+-------------+-------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | Marks | City |
 +---------+-----------+-----------+-------------+-------+------+
 |       1 | Prajakta  |       101 | DBMS        |  NULL | NULL |
 |       2 | Shweta    |       102 | CN          |  NULL | NULL |
 |       3 | Nikita    |       103 | OS          |  NULL | NULL |
 |       4 | Ankita    |       104 | C           |  NULL | NULL |
 +---------+-----------+-----------+-------------+-------+------+
 4 rows in set (0.00 sec) 
add column in table in SQL

New column ‘Marks’ is added to an existing student table. Since, in the query we have specified ‘AFTER’ keyword with the column name after which the new column is to be added. Hence, ‘Marks’ is added after ‘Course_Name’.


Related Topics

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.