Monday, September 2, 2024

10 Examples of ALTER Table Command in SQL

 In this SQL tutorial, you will learn how to use ALTER command in the table on the database. ALTER command is mainly used to add, modify and drop columns, indexes, and constraints on the table in relational databases e.g. MySQLOracleSybase, and SQL Server.  Though 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.


In this SQL and database tutorial, we will see different ways of using ALTER command. By using ALTER command we can add, edit, or modify tables, views, and databases. We can add a new column on the table, we can change the value of the column or we can rename the column also.





10 ALTER command Example in SQL

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_numberstu_nameclasssubject 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 the 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 columns 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 a 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 constraints 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.

No comments:

Post a Comment