Alter table command

461 views 3 slides Apr 23, 2021
Slide 1
Slide 1 of 3
Slide 1
1
Slide 2
2
Slide 3
3

About This Presentation

MY SQL NOTES


Slide Content

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);

Alter table student
Modify city default ‘jaipur’;
Tags