[英]MySQL: Move data from one table to another with different schema
我的桌子設計不好:
CREATE TABLE token (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT(10) UNSIGNED NOT NULL,
token VARCHAR(191) NOT NULL,
expiration TIMESTAMP NOT NULL,
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
我現在認為它很糟糕,即使它以前運行良好,也是因為現在,如果我想要與電子郵件或其他內容相關的令牌,就無法重用該token
表。 我可以只添加email_id
列,但是然后我需要丑陋的邏輯來確定令牌是分別用於用戶還是電子郵件。
我想規范化這種設計,以便可以有一個不與任何user_id
或email_id
耦合的單個token
表,並具有用於關系的user_token
和email_token
數據透視表。
但是我也在使用遷移,所以我不能只重寫我的架構。 我需要使用數據庫中存在的數據即時對此進行修改。
我需要執行以下操作:
user_token
和email_token
表(已完成) token.id
復制到user_token.token_id
列,並將token.user_id
值復制到user_token.user_id
列。 復制需要在INSERT
語句中完成,因為user_token
表將是全新的,沒有數據(TODO) token.user_id
列(完整) 步驟2是我需要幫助的部分。 任何幫助將不勝感激寫該查詢。
如果新模式有助於為步驟2創建查詢,則新模式將如下所示:
CREATE TABLE token (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
token VARCHAR(191) NOT NULL,
expiration TIMESTAMP NOT NULL,
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE user_token (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT(10) UNSIGNED NOT NULL,
token_id INT(10) UNSIGNED NOT NULL,
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
FOREIGN KEY (token_id) REFERENCES token (id) ON DELETE CASCADE
);
CREATE TABLE email_token (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
email_id INT(10) UNSIGNED NOT NULL,
token_id INT(10) UNSIGNED NOT NULL,
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (email_id) REFERENCES user (id) ON DELETE CASCADE,
FOREIGN KEY (token_id) REFERENCES token (id) ON DELETE CASCADE
);
如果您尚未從令牌表中刪除token.user_id和token.email_id列,則可以使用簡單的INSERT復制它們:
INSERT INTO user_token (user_id, token_id)
SELECT user_id, id
FROM token
INSERT INTO email_token (email_id, token_id)
SELECT email_id, id
FROM token
如果要與它們一起復制created_at,updated_at和Deleted_at列,則可以將它們添加到查詢中:
INSERT INTO user_token (user_id, token_id, created_at, updated_at, deleted_at)
SELECT user_id, id, created_at, updated_at, deleted_at
FROM token
INSERT INTO email_token (email_id, token_id, created_at, updated_at, deleted_at)
SELECT email_id, id, created_at, updated_at, deleted_at
FROM token
這會將它們復制到新表中。 然后,您只需要通過簡單的ALTER從令牌表中刪除user_id和email_id列:
第一個刪除外鍵:
ALTER TABLE token DROP FOREIGN KEY user_id
然后刪除列:
ALTER TABLE token DROP COLUMN user_id;
ALTER TABLE token DROP COLUMN email_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.