3 - ALTER - Part 3
3 - ALTER - Part 3
ALTER TABLE COMMAND: This command is used to modify the base table definition. The
modifications that can be done using this command are:
(a) Adding a column:
Syntax: ALTER TABLE Tablename ADD Col_name datatype;
Suppose we want to add a column ‘Age’ in the Teacher table. Following command is used
ALTER TABLE Teacher ADD Age INTEGER;
(b) Dropping a column: A column can be removed using DROP clause.
Syntax: ALTER TABLE Table_name DROP Col_name;
(c) To remove the Dept_No column in the Teacher Table
ALTER TABLE Teacher DROP Dept_no;
Altering a Column: A column definition can also be altered.
(a) Syntax: Change the datatype of a column
ALTER TABLE table_name
MODIFY col_name datatype;
To change the Datatype of Dept_no of teacher table from char to int(3).
ALTER TABLE teacher
MODIFY Dept_no INT(3);
(b) Syntax: Rename the column
ALTER TABLE table_name
CHANGE old_columnname new_columnname datatype;
To rename the column Dept_no as D_id
ALTER TABLE table_name
CHANGE Dept_no D_id char(3);
FUNCTIONS:
Functions in SQL Server are the database object that contains a set of SQL statements to
perform a specific task.
Single Row functions - Single row functions are the one who work on single row and return
one output per row.
Work sheet:
Consider the following table SPORTS and write SQL statements for the following: