Delete Table Across Multiple Databases - mysql

Is there a way to delete a table across multiple databases using MYSQL? I am using Sequel Pro but don't see any option within the GUI to do this so I am wondering if there is some sort of command that can do this?
The table name is the same across all the databases, and removing them one by one is time consuming. This would really help with cleaning up databases as we have hundreds and they all share the same tables. However, some tables are no longer needed and need to be deleted.
Hoping someone can help with a query for doing this.

You would need to run an individual DROP statement for each table.
We can query information_schema.tables to get a list of tables...
SELECT t.table_schema, t.table_name
FROM information_schema.tables t
WHERE t.table_name = 'tablename_i_want_to_drop'
AND t.table_schema NOT IN ('mysql','information_schema','performance_schema')
ORDER BY t.table_schema, t.table_name
And we can use an expression instead of the columns ...
SELECT CONCAT('DROP TABLE `',t.table_schema,'`.`',t.table_name,'` ;') AS `-- stmt`
FROM ...
Then we can take that resultset and save it, and execute the statements from a script. The MySQL command line client allows us to source a script...
https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
The client you are using may have a similar feature, to execute a script. Or somehow copy the resultset and paste that into a query window.
In a MySQL stored program, we could run that same query (to get a list of tables), and then loop those (using a CURSOR), and PREPARE, EXECUTE and DEALLOCATE PREPARE to execute "drop table" statements. That's an option, but for a one time shot, creating the script would be easier.

This is the spencer7593's "Drop query" slightly adaptated under Mysql 5.6.20 and Phpmyadmin :
select concat ('DROP TABLE ',table_schema,'.',table_name,';') FROM information_schema.tables where table_name like '%searched_pattern%' limit 0,500
Limit to 500 because Phpmyadmin displays only the 25 firsts results and I wanted to copy-paste all the results in a new SQL window.

Related

in mysql shell: How to execute the DROP TABLE statement after you HAD QUERIED THE TABLES?

SELECT CONCAT('DROP TABLE `', TABLE_NAME,'`;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'TABLE_PREFIX_GOES_HERE%';
I've connected to the database. I had run the above query. This query only displays the tables. How do I execute the actual command of deleting of what is being displayed inside the mysql shell?
I tried running a DROP TABLE command against single table which works fine/as expected in the mysql cli shell enviroment.
you are just "creating" drop statements, you are not executing them.
you can either use a cursor and then iterate over it executing the drop command
or just push the output of this select via mysql < sqlscript.sql
where sqlscript.sql is the output of your query.
(you need to check the mysql syntax to state the db, and maybe also the user and prompt for password as well)

Update multiple database/tables on one server

I'm using Workbench, I'm not an expert at SQL but always open to learning.
My goal is to Update multiple tables at once and have the Replace happen on all found results.
Something like:
SELECT * FROM *.*;
UPDATE *.* SET post_content = REPLACE(post_content,'data-exclude="direct"','');
But the syntax is wrong. I have 100+ databases that need to be updated so I'd rather not have to list each one out.
Also, if it helps, I'm editing the same table in each database, so if there's another way to go about this, awesome. The table has a different prefix in each database but always ends in: _posts
Thanks ahead of time!
As I noted in the comments, you will have to dynamically write out each UPDATE statement and then execute each dynamically generated statement. You can write a script or procedure to do that, or you can just write some SQL to identify databases/tables that contain the column name you are after and write out the UPDATE statements that way:
SELECT DISTINCT CONCAT('UPDATE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` SET post_content = REPLACE(post_content,''data-exclude="direct"'','''');') as statement
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'post_content';
Or something like that... (haven't tested)
Then just copy/paste the results back into your sql client and execute.

Can I check if a table exists before join?

I have the following problem. I have Table A and would like to join to table B if table B exists. Can this be done? I am only writing SQL in WorkBench to try achieve it.
I am aware I cannot use the EXISTS option as I have tried typing it out but it prompts for an error.
Any suggestions would be greatly appreciated.
Thanks.
I managed to do this using EXECUTE, so using a query that is only prepared at runtime:
SET #sqlCommand = IF(
EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema = '{{yourschemaname}}' AND table_name = '{{yourtablename}}' AND column_name = '{{yourcolumnname}}'),
'SELECT \'Yes! Good to go!\' as ColumnExists',
'SELECT \'Nope!\' as ColumnExists');
PREPARE executable FROM #sqlCommand;
EXECUTE executable;
Note that the two selects at the center (Yes!/No!) are the custom statements that are to be executed conditionally. So if the column exists, the first command is executed (select 'yes!'), otherwise the second one (select 'nope').
I got the hint from this discussion here.. have a look if you're looking for the MSSQL equivalent: https://ask.sqlservercentral.com/questions/97579/check-if-table-exists-in-join.html
Data manipulation language statements are typically written for a specific schema; you are assumed to know what the schema looks like when you issue the statement. So you don't generally have the capacity to ask whether a particular schema object exists or has a particular structure. You could however write a stored procedure that did different things depending upon the schema. You have the ability in a stored procedure to use conditional statements and to look in INFORMATION_SCHEMA.

Create table on every database

The way this database is setup (not my design) is that every user has their own database with many tables, so if a user ever decides to leave us we can just give them their DB and they have all of their data. The issue is, when I create a new form or feature I need to create a table in each one of these databases (50+) and it takes a huge amount of my time. Is there any way to create a table in each database?
Run the following query, then execute the output.
If the table already exists in the schema it will still run. However, if the table already exists and you want to redefine it, you can tune this statement to drop those table and recreate them.
SELECT DISTINCT CONCAT('CREATE TABLE IF NOT EXISTS ', TABLE_SCHEMA, '.define table here')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA NOT IN
('mysql', 'information_schema', 'performance_schema');
You could do it programmatically using a programming language like PHP or Python.
Here's the pseudocode that would need to be implemented.
get array of user databases by selecting from information_schema
for each database:
SQL : "create table if not exists <database>.tableName ..."
end for each

phpMyAdmin - can I export an "alter table" query?

I add a few fields in a table and want to quickly modify the same table in different database - so I want phpMyAdmin to generate the alter table query for selected fields. Is this possible?
You can play with information_schema.
select concat('alter table ',table_schema,'.',table_name, '.....;')
from information_schema.tables
where table_name = 'your_table_name'
Then you can run all the queries.
I don't think this is possible with phpmyadmin, But you can use a tool like heidisql(windows desktop application) where it displays all the sql it runs when you do changes using the gui. You can copy this sql and run it in other dbs.