我必须在大型innodb表上执行一些alter操作,我发现最好创建一个像旧表一样的表,然后改变它。我现在发现所有表仍然指旧表。要完全理解我的问题,请考虑以下问题
CREATE table books2 LIKE books;
ALTER TABLE `books2` DROP `not_needed_fields`;
INSERT INTO books2 (fields) SELECT fields FROM books;
RENAME TABLE books TO old_books;
RENAME TABLE books2 TO books;
book_id的所有外键都显示与old_books的关系我想将其更改为books表。我怎么能这样做?
答案 0 :(得分:1)
您应该重新创建FK,例如 -
-- Drop FK that refers to old_books
ALTER TABLE child_table
DROP FOREIGN KEY FK_books;
-- Add new FK
ALTER TABLE child_table
ADD CONSTRAINT FK_books FOREIGN KEY (book_id)
REFERENCES books(book_id);