简体   繁体   English

InnoDB外键和提交

[英]InnoDB Foreign Keys and Commit

I'm converting an existing database from MyISAM to InnoDB and implementing various foreign keys, I'm having an issue with running the convert script on my database though:- 我正在将现有数据库从MyISAM转换为InnoDB并实现各种外键,但是在数据库上运行转换脚本时遇到了问题:

I'm running all queries as below 我正在如下运行所有​​查询

DELETE FROM example WHERE user NOT IN (select id FROM users);
ALTER TABLE `example` CHANGE `user` `user` INT( 11 ) UNSIGNED NOT NULL ;
ALTER TABLE example ADD FOREIGN KEY (user) REFERENCES users(ID);
ALTER TABLE example ADD FOREIGN KEY (car) REFERENCES cars(ID);

When I run all queries it fails due to a foreign key constraint, due to the fact the DELETE statement hasn't run - if I run them individually, it's fine - is it an issue with commit on the innodb database or is it due to speed of the delete not completing before the next query? 当我运行所有查询时,由于外键约束,由于未运行DELETE语句而失败-如果我单独运行它们,这很好-是在innodb数据库上提交的问题还是由于下一个查询之前删除的速度没有完成?

Is it also ok to have two foreignkeys of ID? 拥有两个ID的外键也可以吗? (two different tables users.id and cars.id). (两个不同的表users.id和cars.id)。

Thanks! 谢谢!

No idea of what the error message might say or what you're trying to accomplish but ALTER TABLE is a DDL statement and those cannot be rollbacked in MySQL. 不知道错误消息可能会说什么或您要完成什么,但ALTER TABLE是DDL语句,并且这些语句无法在MySQL中回滚。 The Statements That Cause an Implicit Commi manual chapter explains: 导致隐式Commi语句手册章节说明:

The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement . 本节中列出的语句(及其任何同义词) 隐式结束当前会话中任何活动的事务,就像您在执行该语句之前进行了COMMIT一样 As of MySQL 5.5.3, most of these statements also cause an implicit commit after executing; 从MySQL 5.5.3开始,大多数这些语句在执行后也会引起隐式提交;

[...] [...]

Data definition language (DDL) statements that define or modify database objects 定义或修改数​​据库对象的数据定义语言(DDL)语句

[...] [...]

ALTER TABLE , CREATE TABLE , and DROP TABLE do not commit a transaction if the TEMPORARY keyword is used. 如果使用TEMPORARY关键字,则ALTER TABLECREATE TABLEDROP TABLE不会提交事务。 (This does not apply to other operations on temporary tables such as CREATE INDEX , which do cause a commit.) However, although no implicit commit occurs, neither can the statement be rolled back. (这不适用于确实引起提交的临时表上的其他操作,如CREATE INDEX 。)但是,尽管没有发生隐式提交,但也无法回滚该语句。 Therefore, use of such statements will violate transaction atomicity: For example, if you use CREATE TEMPORARY TABLE and then roll back the transaction, the table remains in existence. 因此,使用此类语句将违反事务的原子性:例如,如果使用CREATE TEMPORARY TABLE然后回滚该事务,则该表仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM