SQL ALTER TABLE

Introduction

In this chapter, we will learn how to use the ALTER TABLE statement in SQL. The ALTER TABLE statement is used to modify the structure of an existing table. This includes adding, deleting, or modifying columns, and adding or dropping constraints. This chapter will cover the syntax, usage, and examples to help you understand how to use the ALTER TABLE statement effectively.

Syntax for ALTER TABLE

The ALTER TABLE statement has different syntaxes depending on the modification you want to make. Here are some common operations:

1. Add a Column

ALTER TABLE table_name
ADD column_name datatype;

Example

ALTER TABLE employees
ADD date_of_birth DATE;

2. Drop a Column

ALTER TABLE table_name
DROP COLUMN column_name;

Example

ALTER TABLE employees
DROP COLUMN date_of_birth;

3. Modify a Column

ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;

Example

ALTER TABLE employees
MODIFY COLUMN email VARCHAR(150);

4. Rename a Column

MySQL

ALTER TABLE table_name
CHANGE COLUMN old_column_name new_column_name datatype;

Example

ALTER TABLE employees
CHANGE COLUMN email email_address VARCHAR(150);

SQL Server

EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';

Example

EXEC sp_rename 'employees.email', 'email_address', 'COLUMN';

5. Add a Constraint

ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_definition;

Example

ALTER TABLE employees
ADD CONSTRAINT uc_email UNIQUE (email);

6. Drop a Constraint

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example

ALTER TABLE employees
DROP CONSTRAINT uc_email;

Step-by-Step Example

1. Create a Sample Table

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100)
);

2. Add a Column

ALTER TABLE employees
ADD date_of_birth DATE;

3. Verify the Column Addition

DESCRIBE employees;

Output

Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
first_name varchar(50) YES NULL
last_name varchar(50) YES NULL
email varchar(100) YES UNI NULL
date_of_birth date YES NULL

4. Modify a Column

ALTER TABLE employees
MODIFY COLUMN email VARCHAR(150);

5. Verify the Column Modification

DESCRIBE employees;

Output

Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
first_name varchar(50) YES NULL
last_name varchar(50) YES NULL
email varchar(150) YES UNI NULL
date_of_birth date YES NULL

6. Rename a Column

MySQL

ALTER TABLE employees
CHANGE COLUMN email email_address VARCHAR(150);

SQL Server

EXEC sp_rename 'employees.email', 'email_address', 'COLUMN';

7. Verify the Column Rename

DESCRIBE employees;

Output

Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
first_name varchar(50) YES NULL
last_name varchar(50) YES NULL
email_address varchar(150) YES UNI NULL
date_of_birth date YES NULL

8. Add a Constraint

ALTER TABLE employees
ADD CONSTRAINT uc_email UNIQUE (email_address);

9. Drop a Constraint

ALTER TABLE employees
DROP CONSTRAINT uc_email;

Conclusion

The ALTER TABLE statement is used for modifying the structure of existing tables in a database. It allows you to add, delete, and modify columns, as well as add and drop constraints. Understanding how to use the ALTER TABLE statement effectively is essential for managing and evolving your database schema.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare