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))
-
- ALTER TABLE stud
- ADD age numeric(2,0)
- SELECT * FROM stud
-
- ALTER TABLE stud
- ADD gen char(1), email varchar(200)
- SELECT * FROM stud
- DROP TABLE stud
-
- 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
-
-
- 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
-
- ALTER TABLE stud
- DROP COLUMN rno
- DROP TABLE stud
-
- CREATE TABLE stud
- (rno int,
- sname varchar(10),
- gen char(1))
- INSERT INTO stud VALUES(1,'Amit','M'),(2,'Tina','F')
-
- 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
-
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- UPDATE stud SET gen='M' WHERE rno=3
- SELECT * FROM stud
-
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
- DROP TABLE stud
-
-
- 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
-
- ALTER TABLE stud
- DROP CONSTRAINT unq1
- sp_help stud
-
- ALTER TABLE stud
- ADD CONSTRAINT pk1 PRIMARY KEY(rno)
-
- 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
-
- 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
-
- ALTER TABLE stud
- DROP COLUMN age
-
- 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
-
- SELECT * FROM stud
- sp_rename 'stud.rno','roll_number'
- SELECT * FROM stud
-
- sp_rename 'stud','student'
- SELECT * FROM stud
- SELECT * FROM student