Introduction
We can change the structure of the table anytime after the table is created. When we change the schema or specifications of a table, the table is considered as 'altered'. The following are the situations in which a table in a database is considered to be altered.
- When we add a new column to the table after the table is created.
- When we remove a column from the table.
- When we want to increase/decrease the width of the column that is, changing the size of the column.
- When we want to change the datatype of the column.
- When we want to add a constraint on a column of a table after the table is created.
- When we want to drop/remove a constraint on a column of a table.
- When we want to rename a column in a table.
- When we want to rename the table itself.
CREATE DATABASE mysampdb1
use mysampdb1
General Syntax to add new column(s) to the table, we can add one or more than one column to a table at a time.
ALTER TABLE <TabName>
ADD <col1> <datatype>[,<col2> <datatype>,......,<coln> <datatype>]
General Syntax to remove column(s) from the table, we can remove one or more than one column from a table at a time.
ALTER TABLE <TabName>
DROP COLUMN <col1>[,<col2>,....,<coln>]
General Syntax to increase or decrease or change the datatype of the column.
The following syntax can be used to change the datatype of a column as well as to increase or decrease the width of a column.
ALTER TABLE <TabName>
ALTER COLUMN <ColName> <NewDataType>/<NewSize>
- CREATE TABLE stud
- (rno int,
- sname varchar(10))
- --Adding a new column to the table when there is no data in the table.
- SELECT * FROM stud
- --Adding a new column gender to the above table tab1
- ALTER TABLE stud
- ADD gen char(1)
- SELECT * FROM stud
- --Adding more than one column to the table tab1
- ALTER TABLE stud
- ADD age numeric(2,0),email varchar(200)
- SELECT * FROM stud
- DROP TABLE stud
- --Adding column(s) to the table when there is data.....
- CREATE TABLE stud
- (rno int,
- sna varchar(10))
- INSERT INTO stud VALUES(1,'John'),(2,'Sam')
- SELECT * FROM stud
- --Adding a new column to the table stud when there is data in the table
- ALTER TABLE stud
- ADD gen char(1)
- SELECT * FROM stud
- INSERT INTO stud VALUES(3,'Smith','M'),(4,'Kamal','M')
- SELECT * FROM stud
- --Adding more than one column when there is data in the table....
- ALTER TABLE stud
- ADD age numeric(2,0), email_id varchar(200)
- SELECT * FROM stud
- DROP TABLE stud
- --Removing column(s) from the table
- --The following is the scenario where there is no data in the table we are performing these operations..
- CREATE TABLE stud
- (rno int,
- sna varchar(20),
- age numeric(2,0),
- gen char(1),
- email_id varchar(100))
- SELECT * FROM stud
- --Removing one column "sna" from the table stud
- ALTER TABLE stud
- DROP COLUMN sna
- SELECT * FROM stud
- --Removing multiple columns "email_id" and "age" from the table.
- ALTER TABLE tab1
- DROP COLUMN email_id,age
- SELECT * FROM stud
- ALTER TABLE stud
- DROP COLUMN gen
- SELECT * FROM stud
- --When there is only one column in the table and we want to remove that column?
- --ERROR - Why? Because a table to exist in the database it must have at least one column.
- --If we need to remove the last column from the table better drop the table....
- --ERROR - The following command will result in error for the above stated reason
- ALTER TABLE stud
- DROP COLUMN rno
- DROP TABLE stud
- SELECT * FROM stud
- --Following is the scenario for removing column from a table WHEN THERE IS data in the table...
- CREATE TABLE stud
- (rno int,
- sna varchar(20),
- age numeric(2,0),
- gen char(1))
- INSERT INTO stud VALUES(1,'Amit',21,'M'),(2,'James',23,'M'),(3,'Smith',24,'M')
- SELECT * FROM stud
- ALTER TABLE stud
- DROP COLUMN age
- SELECT * FROM stud
- --Removing more than one column "gen" and "rno" from the table when we have data....
- ALTER TABLE stud
- DROP COLUMN gen,rno
- SELECT * FROM stud
- DROP TABLE stud
- SELECT * FROM stud
- --Increasing the width of the column (when there is no data in the column)
- CREATE TABLE stud
- (rno int,
- sna varchar(10),
- age numeric(2,0),
- gen char(1))
- sp_help stud
- SELECT * FROM stud
- --Increasing the width for "gen" column from char(1) to char(6)
- ALTER TABLE stud
- ALTER COLUMN gen char(6)
- sp_help stud
- ALTER TABLE stud
- ALTER COLUMN gen varchar(6)
- sp_help stud
- ALTER TABLE stud
- ALTER COLUMN rno VARCHAR(6)
- Sp_help tab1
- --Reducing the width of the column from 6 character width back to 1 character width
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
- --Increasing the width of the column (when there IS DATA in the column)
- DROP TABLE stud
- CREATE TABLE stud
- (rno int,
- sna varchar(10),
- age numeric(2,0),
- gen char(1))
- INSERT INTO stud
- VALUES(1,'Amit',21,'M'),(2,'Sunil',25,'M')
- SELECT * FROM stud
- sp_help stud
- --ERROR - Because the column width is of 1 character and cannot store more than 1 character.
- UPDATE stud
- SET gen='Male'
- WHERE gen='M'
- sp_help stud
- --Increasing the width of the column when there is data in the table.
- ALTER TABLE stud
- ALTER COLUMN gen varchar(6)
- SELECT * FROM stud
- sp_help stud
- UPDATE stud
- SET gen='Male'
- WHERE gen='M'
- SELECT * FROM stud
- sp_help stud
- INSERT INTO stud VALUES(5,'Reema',22,'Female')
- SELECT * FROM stud
- --Decreasing the width of the column gen back to char(1) when there is data in the table.
- --Please note that when we have data in the column of a table we cannot reduce the size less than
- --Maximum data occupying data.
- --ERROR
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- --In order to reduce the column width from char(6) to char(1) we better modify the contents of data
- UPDATE stud
- SET gen='M'
- WHERE gen='male'
- SELECT * FROM stud
- UPDATE stud
- SET gen='F'
- WHERE gen='female'
- SELECT * FROM stud
- sp_help stud
- --Now the following command works as we have reduced the content of data in the column that is from Male to M and Female to F
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
Join the conversation! Your thoughts help the community grow.