我在MySQL 5.1.41上執行同一事務中的多個ALTER TABLE
語句時出現問題。有運行相同的代碼,並有上49年5月1日在單個事務中發出多個ALTER TABLE語句
多沒有這些問題在同一個表做工精細ADD FOREIGN KEY
語句的另一個開發商:
ALTER TABLE school_state ADD FOREIGN KEY (school_id) REFERENCES school(id) ON DELETE CASCADE;
ALTER TABLE school_state ADD FOREIGN KEY (state_id) REFERENCES state(id) ON DELETE CASCADE;
但在不同的表,我得到一個神祕的錯誤:
ALTER TABLE school_state ADD FOREIGN KEY (school_id) REFERENCES school(id) ON DELETE CASCADE;
ALTER TABLE school_state ADD FOREIGN KEY (state_id) REFERENCES state(id) ON DELETE CASCADE;
ALTER TABLE publisher_login ADD FOREIGN KEY (publisher_id) REFERENCES publisher(id);
Can't create table 'my_database.#sql-2cd_45' (errno: 150)
更奇怪的是,多個DROP FOREIGN KEY
語句失敗乃至全國同一張表:
ALTER TABLE school_state DROP FOREIGN KEY school_state_ibfk_2;
ALTER TABLE school_state DROP FOREIGN KEY school_state_ibfk_1;
Error on rename of './my_database/school_state' to './my_database/#sql2-2cd-45' (errno: 152)
我真的要去執行每一個單個語句作爲它自己的事務嗎?因爲這種做法違背了原子交易的目的。
更新:下面每艾克的建議下,我的ADD FOREIGN KEY
上面的查詢後運行SHOW ENGINE INNODB STATUS
,並得到如下:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110630 8:01:12 Error in foreign key constraint of table my_database/#sql-2cd_95:
FOREIGN KEY (publisher_id) REFERENCES publisher(id):
Cannot resolve column name close to:
) REFERENCES publisher(id)
老實說,我還是不明白的問題是,作爲鍵引用(publisher.id
)確實存在,並且打算成爲外鍵的字段(publisher_login.publisher_id
)也非常存在。這些語句一次只能運行一次,所以爲什麼一次運行時會導致錯誤?