Altering tables in SQL is a crucial operation that allows you to modify the structure of an existing database table without losing any of the data it contains. This chapter will guide you through the basics of using the ALTER TABLE command, advancing to more complex scenarios with examples and explanations.
The ALTER TABLE
statement in SQL is used to make changes to the structure of an existing table. These changes could involve adding, deleting, or modifying columns, changing data types, or renaming the table itself.
ALTER TABLE table_name
MODIFY column_name column_type;
Adding new columns to an existing table is one of the most common uses of the ALTER TABLE
command. The new column is automatically populated with NULL
values unless specified otherwise.
ALTER TABLE table_name
ADD column_name column_type;
-- Adding a new column "email" of type VARCHAR to the "employees" table
ALTER TABLE employees
ADD email VARCHAR(100);
Here, a new column email
is added to the employees
table, allowing you to store email addresses for each employee. The new column will be populated with NULL
values for all existing rows.
The ALTER TABLE
statement also allows you to remove an unwanted column from a table. Dropping a column is a permanent action, meaning that the column and its data will be permanently deleted.
ALTER TABLE table_name
DROP COLUMN column_name;
-- Dropping the "address" column from the "employees" table
ALTER TABLE employees
DROP COLUMN address;
In this case, the address
column is removed from the employees
table. Be cautious when dropping columns, as any data stored in the column will be lost.
You can use the ALTER TABLE
command to modify the attributes of a column, such as changing its data type, renaming the column, or altering its size.
ALTER TABLE table_name
MODIFY column_name new_column_type;
-- Modifying the "phone" column to hold up to 15 characters
ALTER TABLE employees
MODIFY phone VARCHAR(15);
Here, the phone
column in the employees
table is modified to hold a maximum of 15 characters, allowing for longer phone numbers.
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
-- Renaming "emp_id" column to "employee_id"
ALTER TABLE employees
RENAME COLUMN emp_id TO employee_id;
The emp_id
column is renamed to employee_id
for better readability.
If you need to rename an entire table, you can use the ALTER TABLE
statement to achieve this.
ALTER TABLE old_table_name
RENAME TO new_table_name;
-- Renaming the "employees" table to "staff"
ALTER TABLE employees
RENAME TO staff;
The entire employees
table is renamed to staff
. All data remains intact, but the table is now referenced by the new name.
You can add or modify constraints such as NOT NULL
, DEFAULT
, UNIQUE
, or CHECK
using the ALTER TABLE
command. This allows you to enforce data integrity and validation rules.
NOT NULL
Constraint:
ALTER TABLE employees
MODIFY email VARCHAR(100) NOT NULL;
This statement modifies the email
column to ensure that it cannot hold NULL
values.
DEFAULT
Value:
ALTER TABLE employees
MODIFY status VARCHAR(20) DEFAULT 'Active';
A default value of ‘Active’ is added to the status
column, so any new records will automatically have this value unless explicitly set.
Constraints can be added to columns to maintain the integrity of the data. Examples include adding PRIMARY KEY
, FOREIGN KEY
, and UNIQUE
constraints.
ALTER TABLE employees
ADD PRIMARY KEY (employee_id);
Here, the employee_id
column is set as the primary key, meaning it will uniquely identify each row in the table.
ALTER TABLE employees
DROP CONSTRAINT employee_id_pk;
This statement drops the primary key constraint from the employee_id
column.
The ALTER TABLE statement in SQL provides flexibility to adjust the structure of your database tables as your needs evolve. Whether you're adding new columns, modifying existing ones, or enforcing constraints, ALTER TABLE empowers you to make structural changes without having to recreate your entire table. However, care must be taken when performing alterations, especially when dropping data or changing constraints. Happy coding !❤️