ALTER TABLE COMMAND
This is a part of DDL command . The ALTER table command is
used to change definition of existing table usually it can add
columns to a table. It can delete column and change their sizes.
In MY SQL alter table command is used…
1. To Add a New Column
2. To Add an Integrity Constraints
3. To Redefine a column (Data type, size, default value)
1. Adding Column:-
We can add a new column in existing table with the
help of ALTER table command.
To ADD a new column use ADD command with the
ALTER table command.
The new column will be added with NULL values for
all rows currently in the table.
Syntax :-
ALTER TABLE tablename
ADD(newcolumname datatype(size));
Example :-
ALTER TABLE STUDENT
ADD(CITY VARCHAR(20));
Q1. To Insert a Record in new column.
UPDATE STUDENT
SET CITY=’ALWAR’;
Insert into student values(‘alwar’);--- wrong query
Modify command:-
We can use the MODIFY command with alter table
command to change structure of table.
We can change following parts of a table:-
Data Type
Size
Default Value
Order of Column
1) Data Type and Size
Syntax ;-
Alter Table tablename
Modify columnname newdatatype(new size);
Example:-
Alter table student
Modify phoneno varchar(20);