Python MySQL:清理多个外键表
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要从表中删除所有记录:
将下一个 ID 重置为 1:
或
(我还没有测试过这一点。但我应该工作)
To remove all records from the table:
To reset next ID to 1:
or
(I haven't tested this. But i should work)
如果使用 truncate: 清理表:
作为副作用,这将重置自动增量计数器。但您也可以手动重置它:
If you clean a table with truncate:
As a side effect, that will reset the auto-increment counter. But you can also reset it manually:
非常感谢您的所有意见和快速反馈。它成功了!
现在的样子是这样的:
Thanks a lot for all input and quick feedback. It did the trick!
Here is how it looks now: