+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

Thread: Simulate a MySQL update?

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    Simulate a MySQL update?

    Is it possible to simulate running a statement in MySQL? For example, say I want to run something like this:
    Code:
    UPDATE table SET column0='value' WHERE column1='value'
    
    but have MySQL simply spit out a report that shows how many rows would be affected.

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Why not just use a SELECT? That would give you the number of rows that match your WHERE clause...

    You may be able to do something with transactions, provided you're running a version of MySQL that supports them, but it'd would be alot more complicated than a simple SELECT....

    I'm assuming you don't want to actually execute the UPDATE here? I think there may be a MySQL function to retrieve the number of affected records, but that would assume you actually wanted to execute the update.

  3. #3
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    I did run the where clause with a select and it gave me the number of rows, but I was just wondering if it was possible to simulate an update... Just paranoia on my part, I guess. I'm always hesitant to run an update that affects some 30k rows... I'm thinking I might want start running two backups a day. Y'know, so I CAN screw everything up and still recover.

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    I think I see what you mean....So you basically want to know if only the rows you wanted to update were updated?

    I think there may be a function in MySQL that returns the number of affected rows...

    Try the Handbook documentation MySQL :: MySQL Documentation

  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

  6. #6
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    93
    Rep Power
    4

    I read through some of the MySQL documentation, but couldn't find anything that suggested any kind of simulation execution.

    Here's the situation I was encountering:
    I have a list of leads and a list of do-not-call phone numbers. I needed to update the leads against the DNC list, setting any matching numbers' status as 'DNC'. First I checked the lists to see how many leads would be affected by the potential update:
    Code:
    SELECT COUNT(*) FROM leads WHERE phone_number IN(SELECT phone_number FROM dnc_list);
    
    My concern came when I ran the actual update. The update statement I needed to use was:
    Code:
    UPDATE leads SET status='DNC' WHERE phone_number IN(SELECT phone_number FROM dnc_list);
    
    However, if I had made a typo, I could have nuked my lead list:
    Code:
    UPDATE leads SET status='DNC' WHERE phone_number IN(SELECT phone_number FROM leads);
    
    Ergo, you can see why I was wondering if there was any way to simulate the update. I would see something like, "Query OK, 523455 rows affected" and I would know I made a mistake in my statement without actually committing the update.

  7. #7
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    Well, of course there's no way to protect yourself against a typo, but I think the safest way would be to write a little script that assigns your WHERE criteria as a string variable, uses that string in a SELECT COUNT(*)... query, asks you to confirm that you want to continue with the update, then uses the same string variable in an UPDATE... query.

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Yes, I am afraid there's very little you can do to protect yourself against typo errors....We've all been there...

    I guess there are a couple of options....Use completely different fieldNames in one of your tables, which would throw an error when you tried to run the update query on the wrong table. That way you'd have to have a typo in the tablename and the fieldname to update the wrong table....The other option is to take a copy of each table before you run the update so you've got a backup if it goes wrong....

    I can't think of anything that can stop typos....If one update query can cause your whole data audit to fail then I guess I'd be taking a backup before I ran it each time...That's the only real safe answer I guess...

    It'd be no different to someone from accounts sending an email out saying
    Quote Originally Posted by Accounts
    I'm going to kill all our customers today
    when infact they meant to say
    Quote Originally Posted by Accounts
    I'm going to bill all our customers today

  9. #9
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    Quote Originally Posted by richyrich View Post
    It'd be no different to someone from accounts sending an email out saying
    I'm going to kill all our customers today
    when infact they meant to say
    I'm going to bill all our customers today
    That wouldn't be good for business!

  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by don94403 View Post
    That wouldn't be good for business!
    Depends how good the customers were at paying the biils don..

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. CType when retrieving CAST value from MySQL
    By richyrich in forum .NET Development
    Replies: 4
    Last Post: February 19th, 2009, 09:58 AM
  2. Problem inserting records into mysql...
    By bryceowen in forum PHP Development
    Replies: 1
    Last Post: January 19th, 2009, 09:23 PM
  3. Pagination, using PHP and MySQL
    By don94403 in forum PHP Development
    Replies: 0
    Last Post: December 13th, 2008, 01:01 AM
  4. Problem with MySql Stored Procedure
    By micky in forum .NET Development
    Replies: 24
    Last Post: October 20th, 2008, 09:08 AM
  5. Stored Procedures on MySQL
    By richyrich in forum MySQL
    Replies: 7
    Last Post: June 9th, 2008, 12:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO