Introduction
This blog post is related to the ALTER command. Whenever we want to change the structure of the table we can use the following commands as mentioned in this blog. This post covers complete syntax and examples related to the ALTER command.
Altering the SQL Table
Whenever we change or execute commands that change/affect the table structure, the table is considered to be altered. A table in the database is considered to be altered when any of the following are applied to the table structure.
- When we want to add a new column or columns as part of the table.
- When we want to remove an existing column or columns from the table.
- When we want to change the size of the column (that is when we want to increase the width or decrease the width of the column).
- When we want to change the datatype of the column.
- When we want to add a constraint on the column of the table.
- When we want to remove an existing constraint on the column of the table.
- When we want to change the name of the column of a table.
- When we want to change the name of the table.
Let's look at the general Syntax for changing the structure of the table in the above-mentioned scenarios. We must use the DDL command ALTER to change the structure of the table.
1. Syntax to add a new column
ALTER TABLE <TableName>
ADD <ColName> <DataType>
Syntax to add more than one column
ALTER TABLE <TableName>
ADD <ColName-1> <DataType>[,<ColName-2> <DataType>,.....,<ColName-n> <DataType>]
2. Syntax to remove a column from a table
ALTER TABLE <TableName>
DROP COLUMN <ColName>
Syntax to remove more than one column from a table.
ALTER TABLE <TableName>
DROP COLUMN <ColName-1>[,<ColName-2>,...,<ColName-n>]
3. Syntax to change the size of the column (Increasing the width or decreasing the width of the column).
ALTER TABLE <TableName>
ALTER COLUMN <DataType> <NewSize>
4. Syntax to change the datatype of a column.
ALTER TABLE <TableName>
ALTER COLUMN <NewDataType>
5. Syntax to add a constraint on the column.
ALTER TABLE <TableName>
ADD CONSTRAINT <Constraint-Name> <ConstraintType>(<ColName-1>[,<colName-2>,...,<ColName-n>])
6. Syntax to remove a constraint from a table's column.
ALTER TABLE <TableName>
DROP CONSTRAINT <Constraint-Name-1>[,<Constraint-Name-2>,...,<Constraint-Name-n>]
7. Syntax to change the name of a column in a table.
sp_rename '<TableName>.<ExistingColName>','<NewColName>'
8. Syntax to change the name of the table.
sp_rename '<ExistingTableName>','<NewTableName>'
Some sample code...
- use samp1
- SELECT * FROM stud
- DROP TABLE stud
- CREATE TABLE stud
- (rno int,
- sname varchar(20))
- --Adding a new column to the table when there is no data in the table
- ALTER TABLE stud
- ADD age numeric(2,0)
- SELECT * FROM stud
- --Adding more than one column to the table when there is no data....
- ALTER TABLE stud
- ADD gen char(1), email varchar(200)
- SELECT * FROM stud
- DROP TABLE stud
- --Looking at a scenario when we want to add column(s) when there is data in the table.
- CREATE TABLE stud
- (rno int,
- sname varchar(20))
- SELECT * FROM stud
- INSERT INTO stud VALUES(1,'Amit'),(2,'Anil')
- SELECT * FROM stud
- ALTER TABLE stud
- ADD gen char(1)
- SELECT * FROM stud
- ALTER TABLE stud
- ADD age numeric(2,0), email varchar(200)
- SELECT * FROM stud
- --Removing column(s) from the table....
- --When we have data. When we have no data also we can remove the columns in the same way.
- ALTER TABLE stud
- DROP COLUMN age
- SELECT * FROM stud
- ALTER TABLE stud
- DROP COLUMN email,sname
- SELECT * FROM stud
- ALTER TABLE stud
- DROP COLUMN gen
- SELECT * FROM stud
- --Error - When there is only one column we cannot remove it. It is better to drop the table.
- ALTER TABLE stud
- DROP COLUMN rno
- DROP TABLE stud
- --Changing the size of the column....
- CREATE TABLE stud
- (rno int,
- sname varchar(10),
- gen char(1))
- INSERT INTO stud VALUES(1,'Amit','M'),(2,'Tina','F')
- --Error - As the column "gen" can store only 1 character.
- INSERT INTO stud VALUES(3,'Harry','Male')
- sp_help stud
- ALTER TABLE stud
- ALTER COLUMN gen varchar(6)
- INSERT INTO stud VALUES(3,'Harry','Male')
- SELECT * FROM stud
- --Error
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- UPDATE stud SET gen='M' WHERE rno=3
- SELECT * FROM stud
- --Reducing the width of the column from varchar(6) back to char(1)
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
- DROP TABLE stud
- --When we want to add a constraint on a column of a table when there is no data. It is easy to remove a constraint on the column of the table whether there is data or not.
- --It is difficult to add a constraint on the column of a table when there is data but, easy to add when there is no data.
- CREATE TABLE stud
- (rno int,
- sname varchar(20))
- sp_help stud
- SELECT * FROM stud
- ALTER TABLE stud
- ADD CONSTRAINT unq1 UNIQUE(rno)
- sp_help stud
- --Removing the constraint
- ALTER TABLE stud
- DROP CONSTRAINT unq1
- sp_help stud
- --Error – Cannot add a PRIMARY KEY constraint on a column which is not defined with a NOT NULL constraint. Therefore first add NOT NULL constraint and then add the Primary Key constraint.
- ALTER TABLE stud
- ADD CONSTRAINT pk1 PRIMARY KEY(rno)
- --Adding NOT NULL to the rno column.
- ALTER TABLE stud
- ALTER COLUMN rno int NOT NULL
- sp_help stud
- ALTER TABLE stud
- ADD CONSTRAINT pk1 PRIMARY KEY(rno)
- SELECT * FROM stud
- ALTER TABLE stud
- ADD age numeric(2,0) NOT NULL
- ALTER TABLE stud
- DROP COLUMN age
- SELECT * FROM stud
- INSERT INTO stud VALUES(1,'Anil')
- SELECT * FROM stud
- --ERROR - When we have data in the table we cannot add NOT NULL we must use DEFAULT constraint also.
- ALTER TABLE stud
- ADD age numeric(2,0) NOT NULL
- ALTER TABLE stud
- ADD age numeric(2,0) NOT NULL DEFAULT 20
- SELECT * FROM stud
- --Error - We cannot drop a column when we have a constraint associated with it.
- ALTER TABLE stud
- DROP COLUMN age
- --Therefore first remove/drop the constraint and then we can drop/remove the column
- ALTER TABLE stud
- DROP CONSTRAINT <DefaultConstraintName>
- ALTER TABLE stud
- DROP COLUMN age
- SELECT * FROM stud
- ALTER TABLE stud
- ADD age numeric(2,0) DEFAULT 20 NOT NULL check(age between 15 and 20)
- sp_help stud
- SELECT * FROM stud
- --Renaming the column of a table....
- SELECT * FROM stud
- sp_rename 'stud.rno','roll_number'
- SELECT * FROM stud
- --Renaming the name of a table...
- sp_rename 'stud','student'
- SELECT * FROM stud
- SELECT * FROM student
Sourav Kumar DasPosted Nov 20, 2019, 10:52 PM
Hi Govardhan P, See the links for writing articles in C-sharpcorner https://www.c-sharpcorner.com/forums/how-to-publish-article-on-csharpcorner, https://www.c-sharpcorner.com/Publish/ArticleSubmissionGuideLine.aspx, https://www.c-sharpcorner.com/blogs/writing-articles-on-c-sharp-corner1, https://www.c-sharpcorner.com/Tips/3/
Sourav Kumar DasPosted Nov 20, 2019, 10:32 PM
Nice good article for basics in Altering tables, but please don't use colors for text instead use Syntax highlighter code next to Insert image icon, it will look beautiful and readable article for everyone.