Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
排除 MySQL 技術疑難問題
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== RENAME TABLE 時出現 Table already exists === 狀況:使用 <code>RENAME TABLE</code> 替換資料表,即使先將舊表改名備份,仍出現: <pre lang="text"> Table '.\DATABASE_NAME\TABLE_NAME' already exists </pre> 原因:來源表與目的表使用相同的外鍵約束名稱。舊表改名後,自訂外鍵名稱仍保留,移入新表時產生命名衝突。MySQL 5.7、8.0 與 8.4 都要求外鍵名稱在同一資料庫內唯一。參考:[https://docs.oracle.com/cd/E17952_01/mysql-5.7-en/create-table-foreign-keys.html MySQL 外鍵約束說明]。 解決方案: '''1. 確認備份表名稱尚未存在,並檢查兩張表的外鍵名稱。''' <pre lang="sql"> SHOW CREATE TABLE target_db.example_items; SHOW CREATE TABLE staging_db.example_items; </pre> 查看結果是否包含 <code>FOREIGN KEY</code>。例如: <pre lang="sql"> KEY `ix_example_parent_id` (`parent_id`), CONSTRAINT `fk_example_parent` FOREIGN KEY (`parent_id`) REFERENCES `example_parents` (`id`) ON DELETE SET NULL </pre> <table> <tr><th>語法</th><th>意義</th></tr> <tr><td><code>KEY ...</code></td><td>索引,'''不代表有外鍵'''</td></tr> <tr><td><code>CONSTRAINT fk_example_parent</code></td><td>外鍵約束名稱,刪除外鍵時使用此名稱</td></tr> <tr><td><code>FOREIGN KEY (parent_id)</code></td><td>本表的外鍵欄位</td></tr> <tr><td><code>REFERENCES example_parents (id)</code></td><td>引用的父表與欄位</td></tr> <tr><td><code>ON DELETE SET NULL</code></td><td>父表資料刪除時,將對應外鍵欄位設為 <code>NULL</code></td></tr> </table> 不能只看名稱是否包含 <code>fk_</code>;必須看到 <code>FOREIGN KEY (...) REFERENCES ...</code> 才能確認。 另外,<code>SHOW CREATE TABLE</code> 只列出這張表自身定義的外鍵,不會列出其他表對它的外鍵引用。 '''2. 若不需要外鍵約束,可先移除目的端舊表的衝突外鍵,再執行更改資料表。''' <pre lang="sql"> ALTER TABLE target_db.example_items DROP FOREIGN KEY fk_example_parent; RENAME TABLE target_db.example_items TO target_db.example_items_backup, staging_db.example_items TO target_db.example_items; </pre> 注意事項: * 刪除外鍵會保留欄位與索引,但不再驗證關聯,也不再執行原本的 <code>ON DELETE SET NULL</code>。 * 若需保留外鍵,應以不同名稱重新建立約束。 * 換表後應確認外鍵實際引用的資料庫;其他表對舊表的外鍵引用會跟著舊表改名,不會自動切換到新表。參考:[https://docs.percona.com/percona-toolkit/pt-online-schema-change.html Percona 外鍵處理說明]。
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)