2011-08-17 42 views
2

我不得不進行一些改變大InnoDB表我找到了最好的創造像舊錶的表,然後修改它的操作。我現在發現所有表格仍然指舊桌子。要充分認識我的問題,請考慮以下查詢Innodb的重命名錶原因的關係問題

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表的關係。我怎樣才能做到這一點?

回答

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);