Để xóa cột hiện có khỏi bảng MySQL, dùng câu lệnh DROP kết hợp với ALTER TABLE. Cú pháp:
Cú pháp
ALTER TABLE table_name DROP column_name;
Trong đó:
- table_name: tên bảng chứa cột cần xóa.
- column_name: tên cột cần xóa.
Ví dụ
Xóa cột address khỏi bảng student_detail:
mysql> select * from student_detail;
+-----------+-------------+----------+
| Studentid | StudentName | address |
+-----------+-------------+----------+
| 100 | Gaurav | Delhi |
| 101 | Raman | Shimla |
| 103 | Rahul | Jaipur |
| 104 | Ram | Ludhiana |
| 105 | Mohan | Patiala |
+-----------+-------------+----------+
5 rows in set (0.19 sec)
mysql> ALTER TABLE student_detail DROP address;
Query OK, 0 rows affected (1.43 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student_detail;
+-----------+-------------+
| Studentid | StudentName |
+-----------+-------------+
| 100 | Gaurav |
| 101 | Raman |
| 103 | Rahul |
| 104 | Ram |
| 105 | Mohan |
+-----------+-------------+
5 rows in set (0.00 sec)
Kết quả xác nhận cột address đã bị xóa.