ALTER is not part of classical CRUD operation but it’s one of the important DDL commands. One of the most frequent uses of ALTER command in SQL is adding and removing indexes to improve the performance of SQL SELECT queries.
Let’s see some of the most frequently used examples of ALTER command in SQL. In this section, we will see How to use ALTER command to add, modify, and drop existing columns in the table.
Example 1 : How to add a column in an existing table using ALTER clause in SQL:
Suppose we have one table called student with field roll_number, stu_name, class, subject and we want to add one more column called marks-obtained, how will do using ALTER Command.
ALTER TABLE Student
ADD marks_obtained Number (3);
When this field is added to the table it will contain NULL values by default.
Example 2 : How to modify existing column using ALTER command in SQL:
Suppose now we want to modify above added field mark_obtained from number to decimal data type we can use modify column along with the alter command.
ALTER TABLE Student
ALTER COLUMN marks_obtained DECIMAL (5, 2);
Example 3 : How to drop an existing column from the table using ALTER command in SQL:
Now if we want to drop some existing column from the table we use the drop along with ALTER command
ALTER TABLE Student
DROP column marks_obtained;
Example 4. How to drop Primary Key Constraint using ALTER command in database :
Suppose there is a PRIMARY KEY CONSTRAINT named 'pk_roll_num' for the column roll_number of the Student table and we want to drop this constraint
ALTER TABLE Student
DROP CONSTRAINT 'pk_roll_num’;
Example 5. How to add primary key constraints using ALTER command in SQL:
Now if we want to add the primary key constraint in the table Student named 'pk_roll_num' for the column roll_number'. We do like this
ALTER TABLE Student
ADD Constraint 'pk_roll_num PRIMARY KEY(roll_number)
Example 6. How to drop foreign key Constraint using Alter command:
Suppose there is a foreign key constraint named 'fk_roll_num' for the column roll_number' of the 'Marks' table and we want to drop this constraint
ALTER TABLE Marks
DROP CONSTRAINT 'fk_roll_num’;
Example 7 : How to add foreign key Constraints using alter command
Now if we want to add foreign key constraint in table Marks named 'fk_roll_num' for the column roll_number' .we do like this
ALTER TABLE Marks
ADD Constraint 'fk_roll_num FOREIGN KEY (roll_number) REFERENCES Students (roll_number);
Example 8 : How to add unique key Constraints using ALTER command
Now if we want to add a Unique key constraint in the table Student named ‘unique_roll_no ' for the column roll_number' .we do like this
ALTER TABLE Student
ADD CONSTRAINT unique_roll_no
UNIQUE (roll_Number);
Example 9: How to drop unique Key Constraint using ALTER command
Now if we want to drop unique a key constraint in table Student named ‘unique_roll_no ' for the column roll_number' .we do like this
ALTER TABLE Student
DROP CONSTRAINT unique_roll_no);
Example 10 : How to add check Constraint using ALTER command in SQL
The basic syntax for adding check constraint in a table is like this
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint
CHECK (CONDITION);
This is all about ALTER command examples in SQL. We have seen 10 different examples of using ALTER command in SQL. ALTER is one of the most important while working in any database like MySQL, Oracle, or SQL Server, especially from the command line. Since we often need to add, modify and drop columns, indexes, and constraints in the table.
Source: Java67
The Tech Platform
Comments