How I can run a command in phpMyAdmin which will drop all columns in a database that have the prefix test_
.
To drop a column from a table, use the syntax:
alter table <tablename> drop column <columnname>
To find all the columns in a table in a database that start with test_, do the following:
select column_name
from INFORMATION_SCHEMA.columns
where table_name = <table_name> and
table_schema = <schema_name> and
left(column_name, 5) = 'test_' -- not using "like" because '_' is a wildcard char
If you were doing this manually, I would recommend running the following query and then pasting the results in to a mysql query interface:
select concat('alter table ', table_name, ' drop column ', column_name)
from INFORMATION_SCHEMA.columns
where table_name = <table_name> and
schema_name = <schema_name> and
left(column_name, 5) = 'test_'
You can do something similar in code, by running the query, returning the results and then running each row as a query.
I would like to explain or simplify this answer for those like me who were having trouble with this.
I was having trouble dropping a column with the name 'seq' in all tables in my database 'demo'.
You can create a selection with the commands formatted for each table using something like this:
Select concat('alter table ', table_name, ' drop column ', 'seq;')
from (select table_name
from INFORMATION_SCHEMA.tables
where table_name = table_name and
table_schema = 'demo') as t
This creates the alter table command for each table in the 'demo' database.
You have to select the result, copy it, and paste it back into the query editor.
It's a two step process, but if you have to do this several times, save the commands in a text file to run again later.
If you actually want to drop the columns from your schema, you will need to generate the necessary SQL commands dynamically from MySQL's information schema tables. Whilst it is possible to do that within a MySQL stored procedure using SQL prepared statements, which I demonstrate below, you may well find it easier to implement/understand in your preferred development language (which you do not mention in your question):
DELIMITER ;;
CREATE PROCEDURE dropMatchingColumns(IN pattern VARCHAR(64))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
SELECT CONCAT(
'ALTER TABLE `', REPLACE( TABLE_NAME, '`', '``'), '` ',
GROUP_CONCAT(
'DROP COLUMN `', REPLACE(COLUMN_NAME, '`', '``'), '`'
)
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE pattern AND TABLE_SCHEMA = DATABASE()
GROUP BY TABLE_NAME
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO @sql;
IF done THEN
LEAVE read_loop;
END IF;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
END;;
DELIMITER ;
With this procedure defined, one need only CALL dropMatchingColumns('test\_%');
in order to drop all the columns prefixed with test_
from the current database.
If you have MySQl Workbench then you can delete multiple columns by simply do a mass selection and telling workbench to do a mass deletion of the selected columns