You are currently viewing SQL 1.62 SQL ALTER TABLE Command

SQL 1.62 SQL ALTER TABLE Command

Mastering SQL ALTER TABLE Command


SQL (Structured Query Language) is a powerful tool for managing and manipulating data in relational database management systems (RDBMS). One essential aspect of SQL is altering tables, which allows you to modify the structure of existing database tables. In this tutorial, we’ll delve into the ALTER TABLE command in SQL, exploring its syntax, common use cases, and providing step-by-step instructions with sample code.

  1. Syntax of ALTER TABLE Command
    The ALTER TABLE command is used to modify an existing table structure. Its basic syntax is as follows:
ALTER TABLE table_name
[ALTER COLUMN column_name {datatype}]
[ADD column_definition]
[DROP COLUMN column_name]
[ADD CONSTRAINT constraint_definition]

Explanation:

  • ALTER TABLE table_name: Specifies the name of the table that you want to alter.
  • ALTER COLUMN column_name {datatype}: Allows you to modify the data type of a specific column in the table.
  • ADD column_definition: Enables you to add a new column to the table along with its data type and constraints.
  • DROP COLUMN column_name: Removes a column from the table.
  • ADD CONSTRAINT constraint_definition: Adds a constraint to the table, such as PRIMARY KEY, FOREIGN KEY, UNIQUE, etc.
  1. Scenario 1: Altering Data Type of a Column:
    Suppose you have a table named “employees” with a column named “salary” of type INTEGER, but now you want to change it to DECIMAL(10,2).

Sample Code:

ALTER TABLE employees
ALTER COLUMN salary DECIMAL(10,2);

Explanation:

  • The ALTER TABLE command is used to modify the “employees” table.
  • ALTER COLUMN specifies the column “salary” whose data type is being modified to DECIMAL(10,2).
  • DECIMAL(10,2) defines the new data type for the “salary” column, allowing for precise numeric values with up to 10 digits, 2 of which are after the decimal point.
  1. Scenario 2: Adding a New Column to the Table:
    Let’s say you want to add a new column named “email” of type VARCHAR(100) to the “employees” table.

Sample Code:

ALTER TABLE employees
ADD email VARCHAR(100);

Explanation:

  • The ALTER TABLE command is used to modify the “employees” table.
  • ADD specifies that a new column named “email” is being added to the table with the data type VARCHAR(100), which can store strings of up to 100 characters.
  1. Scenario 3: Dropping a Column from the Table:
    Suppose you want to remove the “phone_number” column from the “employees” table.

Sample Code:

ALTER TABLE employees
DROP COLUMN phone_number;

Explanation:

  • The ALTER TABLE command is used to modify the “employees” table.
  • DROP COLUMN specifies that the “phone_number” column is being removed from the table.
  1. Scenario 4: Adding a Constraint to the Table:
    Let’s add a PRIMARY KEY constraint to the “employee_id” column in the “employees” table.

Sample Code:

ALTER TABLE employees
ADD CONSTRAINT pk_employee_id PRIMARY KEY (employee_id);

Explanation:

  • The ALTER TABLE command is used to modify the “employees” table.
  • ADD CONSTRAINT specifies that a constraint is being added to the table.
  • pk_employee_id is the name of the primary key constraint.
  • PRIMARY KEY (employee_id) indicates that the “employee_id” column is designated as the primary key for the table.

Conclusion


Mastering the ALTER TABLE command in SQL is crucial for efficiently managing database structures. By following this tutorial, you’ve learned the basic syntax of ALTER TABLE and explored various scenarios with sample code. Practice applying these concepts in your SQL projects to solidify your understanding and enhance your database management skills.

Leave a Reply