Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running an SQL command on all tables of a database

Status
Not open for further replies.

bwelch42

Programmer
Feb 18, 2007
3
US
Using MySQL 5.0 and only SQL, what's a way to run a SQL command on all tables of a database?

The command would be something like: delete userID from tblUser where userID = 'abcd1234';
 
what if some of the tables don't have a userid? you'll get an error :)

best advice: browse the INFORMATION_SCHEMA.COLUMNS view, and build your SQL from that

r937.com | rudy.ca
 
Okay, I've got the list of tables that need updates. What looping structure works best? As I'm switching from Oracle and MS SQL Server, the looping part seems foreign.
 
Hi

Personally I would use Bash or Perl to read the input ( list generated according to r937's advice ) and generate the output ( an SQL script containing the [tt]delete[/tt] statements to execute ). I mean, I would use command line/desktop tools, not MySQL itself.

Feherke.
 
I agree, Feherke. I have a shell script that does this already, but unfortunately have only SQL access to the server in question.

Oh, well. This will force me to develop my SQL skills.
 
i use a simple text editor

how hard is it to change this --

orders.custid
customers.id
suppliers.id
products.supplier_id


to this --

delete from orders where custid = 'todd'
delete from customers where id = 'todd'
delete from suppliers where id = 'todd'
delete from products where supplier_id = 'todd'


r937.com | rudy.ca
 
I know this doesn't help at all - but your database should have maintained these indicies itself. It should be dead easy to recursively delete a user.

I'm done finger waggin - flame me now ;)


Yet another unchecked rambling brought to you by:
Oddball
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top