Python MySQL:清理多个外键表

发布于 2024-08-12 23:39:11 字数 1306 浏览 3 评论 0原文

我正在使用 Python MySQL,需要清理数据库中包含 13328 行的表。

我无法制作一个简单的删除表,因为该表是子表,也是链接在其上的其他子外键的父亲。如果我尝试删除表,系统会禁止我。表用ON UPDATE CASCADE、ON DELETE CASCADE和InnoDB定义;该表的primary_key索引定义为

productID INT(6) NOT NULL AUTO_INCREMENT ...
PRIMARY KEY (productID,productNO)

因此,我只需要清理;这将自动将表主键索引恢复为 1 以供下一次输入。正确的?

此过程适用于另一个作为父表的表,但不适用于父表和子表。但是,对于这张表(它是其他表的子表和父表),我陷入了困境。

这是代码 - ProductID 是该表的主索引键:

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],)) 

如果我转到 MySQL 控制台检查处理结果,它会得到:

mysql> SELECT MIN(productID)
    FROM product;
4615748

mysql> SELECT MAX(productID)
    FROM product;
4629075

如果我在控制台上运行相同的命令来清理表,它会工作:

mysql> DELETE FROM product WHERE productID <='4629075';
Query OK, 13328 rows affected (0.64 sec)

并显示我通常所期望的。

但是,如果我在控制台上清理表后转到 Python 函数,然后再次运行程序,并清理表以重新启动处理,它会重新启动表索引,而不是 MIN:1,而是 4629076。

有什么建议吗?

所有意见和建议都受到高度赞赏和欢迎。

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

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:

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:

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-08-19 23:39:11

要从表中删除所有记录:

truncate table product

将下一个 ID 重置为 1:

ALTER TABLE product AUTO_INCREMENT = 1

SET insert_id;
INSERT INTO product ...;

(我还没有测试过这一点。但我应该工作)

To remove all records from the table:

truncate table product

To reset next ID to 1:

ALTER TABLE product AUTO_INCREMENT = 1

or

SET insert_id;
INSERT INTO product ...;

(I haven't tested this. But i should work)

空城旧梦 2024-08-19 23:39:11

如果使用 truncate: 清理表:

TRUNCATE TABLE product

作为副作用,这将重置自动增量计数器。但您也可以手动重置它:

ALTER TABLE product AUTO_INCREMENT = 1

If you clean a table with truncate:

TRUNCATE TABLE product

As a side effect, that will reset the auto-increment counter. But you can also reset it manually:

ALTER TABLE product AUTO_INCREMENT = 1
瀞厅☆埖开 2024-08-19 23:39:11

非常感谢您的所有意见和快速反馈。它成功了!

现在的样子是这样的:

def clean_tableProduct(self):
    cleanTabeMySQLQuery = """TRUNCATE TABLE product;"""
    self.cursorMySQL.execute(cleanTabeMySQLQuery)

    setIndexMySQLQuery = """ALTER TABLE product AUTO_INCREMENT = 1;"""
    self.cursorMySQL.execute(setIndexMySQLQuery)

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

Here is how it looks now:

def clean_tableProduct(self):
    cleanTabeMySQLQuery = """TRUNCATE TABLE product;"""
    self.cursorMySQL.execute(cleanTabeMySQLQuery)

    setIndexMySQLQuery = """ALTER TABLE product AUTO_INCREMENT = 1;"""
    self.cursorMySQL.execute(setIndexMySQLQuery)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文