Tutorials

Alter Command in MySQL

 ALTER command is use to modify the structure or columns of the tables.

There are 3 ways to write the command

  • To ADD New column
  • To MODIFY Existing Column
  • To REMOVE or DELETE Existing Column

Lets start

To ADD NEW Column


(
       AdmissionNumber int(5) NOT NULL Primary Key,
       SName varchar(50) NOT NULL,
       DOB date,
       fee decimal(10,2),
       mobile char(10)
);

Lets imagine that we want to add new column name WhatAppNumber, Data type is char(10)

Command to add this column is 

ALTER TABLE Student

ADD COLUMN WhatAppNumber char(10);


To MODIFY Existing Column


(
       AdmissionNumber int(5) NOT NULL Primary Key,
       SName varchar(50) NOT NULL,
       DOB date,
       fee decimal(10,2),
       mobile char(10),

WhatAppNumber char(10)

);

Lets imagine that we want to modify column WhatAppNumber, New Data type is decimal(10)

Command to modify this column is 

ALTER TABLE Student

MODIFY COLUMN WhatAppNumber decimal(10);


To REMOVE OR DELETE Existing Column


(
       AdmissionNumber int(5) NOT NULL Primary Key,
       SName varchar(50) NOT NULL,
       DOB date,
       fee decimal(10,2),
       mobile char(10),

WhatAppNumber decimal(10)

);

Lets imagine that we want to Delete column WhatAppNumber.

Command to REMOVE this column is 

ALTER TABLE Student

DROP  WhatAppNumber;

No comments:

Post a Comment