[英]create a new junction table to reflect two column primary key in one of the joined tables
在MYSQL數據庫中,我需要為多對多關系創建一個新的聯結表,以反映該關系中的表之一具有兩列復合主鍵的事實。 如何編寫SQL來完成此任務?
這是當前的表結構:
wordkeys
keyword (pk)
description
id
effectiveTime
other columns
primary key (id, effectiveTime)
oldjunction
keyword (fk to wordkeys)
descriptionid (fk to description)//this needs to change to add effectiveTime
primary key (keyword, descriptionid)//this needs to change to add effectiveTime
newjunction (need to create this table as a function of the others above)
keyword (fk to wordkeys table)
descriptionid
effectiveTime
primary key (keyword, descriptionid, effectiveTime)
foreign key (descriptionid, effectiveTime) to description table
SQL需要完成以下任務:
FOR EACH row in oldjunction
1.) get all the values of effectiveTime associated with the given descriptionid from that row
2.) FOR EACH value of effectiveTime from step 1, create a new row in newjunction table
編輯:
為了回應這些評論,我添加了以下更多信息:
我需要幫助將上述原始文章的FOR EACH
組件轉換為SQL語法。 這將需要創建新newjunction
表。 我們需要新newjunction
表的原因是,僅descriptionid
不能唯一地標識description
表中的各個行。 取而代之的是,我們需要descriptionid
和effectiveTime
的組合,以便獲得description
表中每一行的唯一標識符。 這是否可以解決問題?
因此,根據我的收集,我認為您正在尋找的是:
INSERT INTO newjunction
SELECT o.keyword, d.id, d.effectiveTime
FROM description d
JOIN oldjunction o
ON o.descriptionid = d.id
我也認為newjunction
缺少FK到wordkeys
表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.