SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool MySQL Python auto-increment foreign-key clean-up

    Hi Everybody,

    I am working with Python MySQL, and need to clean a table in my database that has 13328 rows.

    I can not make a simple drop table, because this table is child and also father of other child foreign-keys linked on it. If I try drop table, the system forbidden me. The table is defined with ON UPDATE CASCADE, ON DELETE CASCADE and InnoDB; The primary_key index to that table is defined as

    Code:
        productID INT(6) NOT NULL AUTO_INCREMENT ...
        PRIMARY KEY (productID,productNO)
    Therefore, I just have to clean; this will automatically restore the table primary key index to 1 for the next input. Right?

    This procedure worked fine for another table that was a father table, but not also a father and child table. But, for this table, which is child and father of other tables, I got stuck on it. Here is the code - productID is my primary index key to this table:

    Code:
        
        def clean_tableProduct(self):
            getMaxID_MySQLQuery = """SELECT MAX(productID)
            FROM product;"""
    
            cleanTabeMySQLQuery="""DELETE FROM product WHERE productID <=%s;"""        
                
            self.cursorMySQL.execute(getMaxID_MySQLQuery)        
    
            for row in self.cursorMySQL:       
                self.cursorMySQL.execute(cleanTabeMySQLQuery,(row[0],))
    If I go to the MySQL console to check the processing results, it gets me:

    Code:
      
        mysql> SELECT MIN(productID)
            FROM product;
        4615748
    
        mysql> SELECT MAX(productID)
            FROM product;
        4629075
    If I run the same command on console to clean the table, it works:
    Code:
          
        mysql> DELETE FROM product WHERE productID <='4629075';
        Query OK, 13328 rows affected (0.64 sec)
    and shows me what I would normally expect.

    However, if I go to Python function after having cleaned the table on console, and run the program again, and clean the table to restart the processing, it restarts the table index not with MIN:1, but instead 4629076.

    Any suggestion?

    All comments and suggestions are highly appreciated and welcome.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The auto_increment value never automatically resets. It is not a row counter, it is a unique identifier.

    If you want to delete all rows from a table, you do not need a where clause at all, just "DELETE FROM product", or "TRUNCATE TABLE product".

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    The auto_increment value never automatically resets.
    it does when you do a TRUNCATE TABLE
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks a lot for all input and quick feedback. It did the trick!

    Here is how it looks now:
    Code:
        def clean_tableProduct(self):
            cleanTabeMySQLQuery = """TRUNCATE TABLE product;"""
            self.cursorMySQL.execute(cleanTabeMySQLQuery)
            
            setIndexMySQLQuery = """ALTER TABLE product AUTO_INCREMENT = 1;"""
            self.cursorMySQL.execute(setIndexMySQLQuery)


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
  •