ALTER Command

 

ALTER Command

ALTER is a DDL command which changes or modifies the existing structure of the database, and it also changes the schema of database objects.

We can also add and drop constraints of the table using the ALTER command.

Examples of ALTER Command in SQL

Example 1: This example shows how to add a new field to the existing table.

Syntax to add a newfield in the table:

  1. ALTER TABLE name_of_table ADD column_name column_definition;  
  2. Suppose, you want to add the 'Father's_Name' column in the existing Student table. To do this, you have to write the following DDL command:
  3. ALTER TABLE Student ADD Father's_Name Varchar(60);  

Example 2: This example describes how to remove the existing column from the table.

Syntax to remove a column from the table:

ALTER TABLE name_of_table DROP Column_Name_1 , column_Name_2 , ….., column_Name_N; 

Suppose, you want to remove the Age and Marks column from the existing Student table. To do this, you have to write the following DDL command:


ALTER TABLE StudentDROP Age, Marks; 

Example 3: This example describes how to modify the existing column of the existing table.


Syntax to modify the column of the table:

  1. ALTER TABLE table_name MODIFY ( column_name column_datatype(size));  
  2. Suppose, you want to change the character size of the Last_Namefield of the Student table. To do this, you have to write the following DDL command:

  1. ALTER TABLE table_name MODIFY ( Last_Name varchar(25));  




No comments:

Post a Comment