mysql> alter table [table name] drop column [column name];
mysql> alter table [table name] add column [new column name] varchar (20);
To delete a column in MySQL, you would use the ALTER TABLE
statement followed by the DROP COLUMN
keyword. Here’s the syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Replace table_name
with the name of your table and column_name
with the name of the column you want to delete.
To add a new column, you would also use the ALTER TABLE
statement, this time followed by the ADD COLUMN
keyword. Here’s the syntax:
ALTER TABLE table_name
ADD COLUMN new_column_name column_definition;
Replace table_name
with the name of your table, new_column_name
with the name of the new column, and column_definition
with the data type and any additional attributes for the column, such as INT
, VARCHAR
, DATE
, etc.
For example, to delete a column named old_column
and add a new column named new_column
of type VARCHAR(50)
to a table named my_table
, you would do:
ALTER TABLE my_table
DROP COLUMN old_column;
ALTER TABLE my_table
ADD COLUMN new_column VARCHAR(50);
Remember to be cautious when altering tables, especially if they contain important data, as these operations can’t be undone. It’s a good idea to make backups before making significant changes.