ALTER COMMAND IN SQL
1. Adding Not null constraint in MySQL
Alter table student modify fees int(4) not null;
2. Adding unique constraint in MySQL
Alter table student add unique (STNAME);
3. Adding Primary Key constraint in MySQL
Alter table student add primary key (rollno,class);
4. Adding a new column to the table
Alter table student add stMarks int(4);
5. CHANGING COLUMN NAME OF A TABLE
Alter table student change rNo RollNo int(3);
6. Dropping constraints of a table
Alter table student drop primary key;
7. Dropping column of a table
Alter table student drop column stmarks;
ALTER COMMAND
Practice Question on Alter command
Write SQL command to add a new column BookPubl in above table Book
containing names of publishers.
Write a SQL statement change the data type of the column BID from char to
varchar in the table locations.
Write a SQL statement to drop the column Quantity from the table.
Write a SQL statement to change the name of the column BookPubl to Publisher,
keeping the data type and size same.
Write a SQL statement to add a primary key for the columns BID in the Books table.
Write a SQL statement to rename the table Book to BookDetails.
Write a SQL statement to add the Not Null constraint on AUName column.
Operations Using Alter Command
1. Adding/Updating constraints to a table.
2. Removing constraints to a table.
3. Deleting Column.
4. Changing Column Name.
5. Adding New Column
6. Changing datatype, Size of existing column.
Keywords Used with Alter Command
CHANGE,
DROP
ADD,
MODIFY