Lesson 29 of 60 – ALTER TABLE
48%

ALTER TABLE in SQL

The ALTER TABLE statement is used to modify the structure of an existing table. You can use it to add, modify, rename, or remove columns from a table.

Note: ALTER TABLE changes the structure of a table. Always use it carefully, especially when modifying or deleting columns.

1. What is ALTER TABLE?

ALTER TABLE is used to change the structure of an existing table.

ALTER TABLE students
ADD email VARCHAR(100);

This adds a new email column to the students table.

2. Basic ALTER TABLE Syntax

The basic syntax is:

ALTER TABLE table_name
operation;

The operation can be used to add, modify, rename, or remove a column.

3. Add a New Column

The ADD COLUMN command adds a new column to an existing table.

ALTER TABLE students
ADD COLUMN email VARCHAR(100);

A new email column will be added.

4. Add a Numeric Column

You can add columns with numeric data types.

ALTER TABLE students
ADD COLUMN age INT;

This adds an age column that can store integer values.

5. Add Multiple Columns

You can add multiple columns in one ALTER TABLE statement.

ALTER TABLE students
ADD COLUMN phone VARCHAR(15),
ADD COLUMN address VARCHAR(200);

Both phone and address columns are added.

6. Add a Date Column

You can add a column for storing dates.

ALTER TABLE students
ADD COLUMN admission_date DATE;

This creates an admission_date column.

7. Add a Column with DEFAULT

You can define a default value when adding a column.

ALTER TABLE students
ADD COLUMN status VARCHAR(20) DEFAULT 'Active';

New records can automatically use Active as the default status.

8. Modify a Column Data Type

In MySQL, the MODIFY COLUMN command can change the definition of an existing column.

ALTER TABLE students
MODIFY COLUMN name VARCHAR(150);

This changes the size of the name column.

9. Modify a Numeric Column

You can change the data type of a numeric column.

ALTER TABLE students
MODIFY COLUMN age BIGINT;

The age column is changed from its previous definition to BIGINT.

10. Change NOT NULL Property

You can modify whether a column allows NULL values.

ALTER TABLE students
MODIFY COLUMN name VARCHAR(100) NOT NULL;

This makes the name column required.

11. Change a Column Default Value

You can modify the default value of a column.

ALTER TABLE students
ALTER COLUMN status SET DEFAULT 'Active';

This syntax is supported by some SQL database systems. MySQL commonly uses:

ALTER TABLE students
ALTER status SET DEFAULT 'Active';

12. Rename a Column

In MySQL, RENAME COLUMN can be used to change a column name.

ALTER TABLE students
RENAME COLUMN phone TO mobile;

The phone column is renamed to mobile.

13. Rename a Column Using CHANGE

MySQL also supports the CHANGE COLUMN syntax.

ALTER TABLE students
CHANGE COLUMN phone mobile VARCHAR(15);

CHANGE can rename the column and define its data type at the same time.

14. Drop a Column

The DROP COLUMN command removes a column from a table.

ALTER TABLE students
DROP COLUMN email;

The email column and its data are removed.

15. Drop Multiple Columns

You can remove more than one column from a table.

ALTER TABLE students
DROP COLUMN email,
DROP COLUMN address;

Both columns will be removed.

16. Rename a Table

You can rename an existing table using RENAME TO.

ALTER TABLE students
RENAME TO learners;

The table name changes from students to learners.

17. Add a PRIMARY KEY

You can add a primary key to an existing table.

ALTER TABLE students
ADD PRIMARY KEY (student_id);

The student_id column becomes the primary key.

18. Add a UNIQUE Constraint

You can add a UNIQUE constraint to an existing column.

ALTER TABLE students
ADD UNIQUE (email);

This prevents duplicate email values.

19. Add a FOREIGN KEY

You can add a foreign key to connect two tables.

ALTER TABLE students
ADD FOREIGN KEY (course_id)
REFERENCES courses(course_id);

This creates a relationship between students and courses.

20. Add a CHECK Constraint

You can add a CHECK constraint to restrict values.

ALTER TABLE students
ADD CHECK (age >= 18);

This requires the age value to be at least 18.

21. Add a Named Constraint

You can give a constraint a name.

ALTER TABLE students
ADD CONSTRAINT chk_age
CHECK (age >= 18);

The constraint is named chk_age.

22. Drop a Constraint

Constraints can be removed using ALTER TABLE. The exact syntax depends on the database system and constraint type.

For example, a MySQL foreign key can be removed using:

ALTER TABLE students
DROP FOREIGN KEY fk_course;

The named foreign key constraint is removed.

23. Alter Table with AUTO_INCREMENT

In MySQL, you can modify an integer column to use AUTO_INCREMENT.

ALTER TABLE students
MODIFY student_id INT AUTO_INCREMENT;

New student IDs can then be generated automatically.

24. Alter Table After Creating It

ALTER TABLE is useful when the original table structure needs to change after the table has already been created.

CREATE TABLE students (
    student_id INT,
    name VARCHAR(100)
);

ALTER TABLE students
ADD email VARCHAR(100);

The table is first created and then modified.

25. ALTER TABLE vs UPDATE

ALTER TABLE changes the structure of a table.

UPDATE changes existing data inside a table.

ALTER TABLE students
ADD email VARCHAR(100);

UPDATE students
SET email = 'student@example.com'
WHERE student_id = 101;

26. ALTER TABLE vs DELETE

ALTER TABLE changes table structure.

DELETE removes records from a table.

ALTER TABLE students
ADD phone VARCHAR(15);

DELETE FROM students
WHERE student_id = 101;

27. Check Table Structure

In MySQL, you can use DESCRIBE to view the table structure.

DESCRIBE students;

You can check the columns and their definitions before modifying the table.

28. Common ALTER TABLE Mistake

A common mistake is trying to add a column that already exists.

ALTER TABLE students
ADD email VARCHAR(100);

If the email column already exists, the database may return an error.

Tip: Check the table structure before making structural changes.

29. Practical ALTER TABLE Example

Suppose your students table needs a mobile number column.

ALTER TABLE students
ADD mobile VARCHAR(15);

Now the table can store mobile numbers for students.

30. Complete ALTER TABLE Example

Here is a practical example that adds, modifies, renames, and removes columns.

ALTER TABLE students
ADD email VARCHAR(100);

ALTER TABLE students
MODIFY email VARCHAR(150);

ALTER TABLE students
RENAME COLUMN email TO email_address;

ALTER TABLE students
DROP COLUMN email_address;

These commands demonstrate common ALTER TABLE operations.

📌 Key Points

  • ALTER TABLE is used to modify an existing table.
  • You can add new columns using ADD COLUMN.
  • You can modify column definitions using MODIFY COLUMN in MySQL.
  • You can rename columns using RENAME COLUMN.
  • You can remove columns using DROP COLUMN.
  • You can rename a table using RENAME TO.
  • ALTER TABLE can also be used to add constraints.
  • Always check the table structure before making important changes.

🧠 Quick Quiz

Question: Which SQL statement is used to modify the structure of an existing table?