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))
-
- 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
- DROP TABLE stud
-
- CREATE TABLE stud
- (rno int,
- sna varchar(10))
- INSERT INTO stud VALUES(1,'John'),(2,'Sam')
- SELECT * FROM stud
-
- ALTER TABLE stud
- ADD gen char(1)
- SELECT * FROM stud
- INSERT INTO stud VALUES(3,'Smith','M'),(4,'Kamal','M')
- SELECT * FROM stud
-
- ALTER TABLE stud
- ADD age numeric(2,0), email_id varchar(200)
- SELECT * FROM stud
- DROP TABLE stud
-
-
- CREATE TABLE stud
- (rno int,
- sna varchar(20),
- age numeric(2,0),
- gen char(1),
- email_id varchar(100))
- SELECT * FROM stud
-
- ALTER TABLE stud
- DROP COLUMN sna
- SELECT * FROM stud
-
- ALTER TABLE tab1
- DROP COLUMN email_id,age
- SELECT * FROM stud
- ALTER TABLE stud
- DROP COLUMN gen
- SELECT * FROM stud
-
-
-
-
- ALTER TABLE stud
- DROP COLUMN rno
- DROP TABLE stud
- SELECT * FROM stud
-
- 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
-
- ALTER TABLE stud
- DROP COLUMN gen,rno
- SELECT * FROM stud
- DROP TABLE stud
- SELECT * FROM stud
-
- CREATE TABLE stud
- (rno int,
- sna varchar(10),
- age numeric(2,0),
- gen char(1))
- sp_help stud
- SELECT * FROM stud
-
- 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
-
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
-
- 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
-
- UPDATE stud
- SET gen='Male'
- WHERE gen='M'
- sp_help stud
-
- 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
-
-
-
-
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
-
- 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
-
- ALTER TABLE stud
- ALTER COLUMN gen char(1)
- sp_help stud
In this blog, which is Part 1 of ALTER table structure, we have discussed adding a new column, removing unwanted column(s), and increasing as well as decreasing the width of the column in a table. In the next blog which will be Part 2, a continuation of this blog. We will cover the topics related to altering table structure (i.e. adding constraints, removing constraints, renaming the column of the table and renaming the table).