Alter Command: Learn SQL Table Modification

ALTER Command


Definition: 

ALTER is a SQL Server command used to modify the structure of a database object such as a table, view, or stored procedure. The ALTER command can be used to add, modify or remove columns, constraints, indexes, or other attributes of the object. 

Some examples of common ALTER commands in SQL Server include: 

  • ALTER TABLE: used to add, modify, or drop columns or constraints in a table 
  • ALTER VIEW: used to modify the definition of a view 
  • ALTER PROCEDURE: used to modify the definition of a stored procedure 
  • ALTER INDEX: used to modify the attributes of an index on a table 

The ALTER command can be a powerful tool, but it should be used with caution as it can affect the data in the database. It is important to thoroughly test any changes before implementing them in a production environment. 

Now we will use some examples to understand the uses of ALTER command. 

— Create a new table called “Users” 

CREATE TABLE Users ( 
   UserID INT PRIMARY KEY,  
   FirstName VARCHAR(50), 
   LastName VARCHAR(50), 
   Email VARCHAR(100) 
); 

— Insert some data into the Users table 

INSERT INTO Users (UserID, FirstName, LastName, Email) 
VALUES (1, ‘John’, ‘Doe’, ‘johndoe@example.com’), 
       (2, ‘Jane’, ‘Doe’, ‘janedoe@example.com’), 
       (3, ‘Bob’, ‘Smith’, ‘bobsmith@example.com’); 

We can view the Users table using following syntax:  

SELECT * FROM dbo.Users; 

Now let’s look at some examples of using ALTER command: 

Adding a column to a table: 

Example: Add a new column called “Age” to the Users table.  

ALTER TABLE dbo.Users
ADD Age INT;
ALTER TABLE dbo.Users 
ADD Age INT; 


Result:

As you can see in the above image, we have added “Age” column in the Users table. 

Modifying a column in a table: 

Example: Change the data type of the “Email” column from VARCHAR(100) to VARCHAR(150). 

ALTER TABLE dbo.Users
ALTER COLUMN Email VARCHAR(150);

Result:


Dropping a column from a table: 

Example: Remove the “Age” column from the Users table. 

ALTER TABLE dbo.Users 
DROP COLUMN Age; 

Adding a Constraint: 

Adding a UINQUE constraint: 

ALTER TABLE Table_Name 
ADD CONSTRAINT Constraint_Name UNIQUE (Col_Name); 

Example: Add a constraint to the Users table to ensure that the Email column is unique. 

ALTER TABLE dbo.Users
ADD CONSTRAINT UQ_Email UNIQUE (Email);

Adding a PRIMARY KEY constraint: 
Syntax-
ALTER TABLE Table_Name 
ADD CONSTRAINT Constraint_Name PRIMARY KEY (Col_Name); 

 Adding a FOREIGN KEY constraint: 
Syntax-
ALTER TABLE Table_Name 
ADD CONSTRAINT Constraint_Name FOREIGN KEY (Child_Col_Name)
REFERENCES Parent_Table(Parent_column_name); 

Adding a DEFAULT constraint: 

ALTER TABLE Table_Name 
ADD CONSTRAINT Constraint_Name
DEFAULT Default_Value FOR Col_Name; 


ALTER Procedure Syntax: 

ALTER PROCEDURE procedure_name 
AS 
BEGIN 
   — Statements to modify the procedure go here 
END 

In this syntax, you would replace procedure_name with the name of the stored procedure you want to modify, and add any necessary statements between the BEGIN and END keywords. 

ALTER VIEW Syntax: 

ALTER VIEW View_Name 
AS 
SELECT Column1, Column2, … 
FROM Table_Name 
WHERE Condition; 

In this syntax, you would replace view_name with the name of the view you want to modify, and modify the SELECT statement to include any necessary changes to the view’s columns, table name, or filter conditions. 

ALTER INDEX Syntax:  

ALTER INDEX Index_Name ON Table_Name 
REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON); 

 In this syntax, you would replace index_name with the name of the index you want to modify, and table_name with the name of the table the index is associated with. You can then modify the options within the REBUILD WITH clause as needed. 

Advantages & Disadvantages of using ALTER: 

ALTER is a powerful command in SQL Server that allows you to modify the structure of database objects such as tables, views, and stored procedures. Here are some advantages and disadvantages of using ALTER in SQL Server: 

Advantages: 

Flexibility: ALTER allows you to make changes to existing database objects without having to drop and recreate them, which can be time-consuming and potentially risky. 

Granularity: ALTER allows you to make targeted changes to specific database objects, rather than having to make sweeping changes across the entire database. 

Efficiency: ALTER can be a more efficient way to modify database objects than dropping and recreating them, particularly for large objects with many dependencies. 

Disadvantages: 

Risk: Any change you make using ALTER has the potential to impact the functionality and integrity of your database. Careful planning and testing is required to ensure that any changes made using ALTER do not have unintended consequences. 

Complexity: ALTER can be a complex command with many options and syntax requirements. It’s important to have a good understanding of the options available and how they will impact your database objects before making changes. 

Limitations: There are some changes that cannot be made using ALTER, particularly for objects with dependencies or constraints that cannot be easily modified. 

Overall, ALTER is a powerful tool in SQL Server that allows you to make targeted changes to specific database objects. While it can be a more efficient and flexible way to modify your database structure than dropping and recreating objects, it also requires careful planning and testing to ensure that changes are made safely and without unintended consequences. 

Interview related questions & answers: 

  1. What is the ALTER command used for in SQL Server?  
    Answer: The ALTER command is used to modify the structure of existing database objects in SQL Server, such as tables, views, and stored procedures. 
  1. Can you give an example of how you would use ALTER to add a column to an existing table in SQL Server?  
    Answer:  
    ALTER TABLE Table_Name 
    ADD Column_Name Data_Type; 


    In this example, you would replace table_name with the name of the table you want to modify, column_name with the name of the new column you want to add, and data_type with the data type of the new column. 
  1. What are some advantages of using ALTER instead of dropping and recreating database objects?  
    Answer: Some advantages of using ALTER instead of dropping and recreating database objects include increased flexibility, granularity, and efficiency. 
  1. What are some risks associated with using ALTER to modify database objects?  
    Answer: Some risks associated with using ALTER to modify database objects include the potential to impact the functionality and integrity of your database, and the complexity of the command and options available. 
  1. Can you give an example of how you would use ALTER to add a primary key constraint to an existing table in SQL Server?  
    Answer:  
    ALTER TABLE Table_Name 
    ADD CONSTRAINT constraint_name PRIMARY KEY (column_name); 


Conclusion: 

In conclusion, ALTER is a powerful command in SQL Server that allows you to modify the structure of existing database objects such as tables, views, and stored procedures. It provides flexibility, granularity, and efficiency when making changes to specific objects in the database. However, it also carries some risks, such as the potential to impact the functionality and integrity of the database and the complexity of the command and options available. To use ALTER safely and effectively, it is important to carefully plan and test any changes before implementing them, and to consult the documentation for the specific object type and modification being made.