Could somebody please help me with how I can run a command in phpmyadmin which will drop all columns in a database that have the prefix "test_".
I'm not very good with MYSQL so I don't even know where to start, any help would be appreciated.
Many thanks. :)
To drop a column from a table, use the syntax:
To find all the columns in a table in a database that start with test_, do the following:
If you were doing this manually, I would recommend running the following query and then pasting the results in to a mysql query interface:
You can do something similar in code, by running the query, returning the results and then running each row as a query.
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):
With this procedure defined, one need only
CALL dropMatchingColumns('test\_%');
in order to drop all the columns prefixed withtest_
from the current database.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:
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 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