gpt4 book ai didi

mysql - 我可以追溯添加外键 ID 列并用另一个表的主键的值填充它,而无需手动输入吗?

转载 作者:行者123 更新时间:2023-11-29 10:43:39 25 4
gpt4 key购买 nike

我有两个表:

Table1 (TagID, TagName, Primary Key (TagID)<br>
Table2 (TagName,ProductName, ProductID)

我想向 Table2 添加一个 TagID 列,并用 table1 中的 TagID 填充它,其中 标签名称=标签名称

无需仔细检查并单独输入 TagID 是否可以实现这一点?

最佳答案

是的,您可以通过更改表格然后更新值来做到这一点:

alter table table2 add column TagId int;

update table2 t2 join
table1 t1
on t2.TagName = t1.TagName
set t2.TagId = t1.TagId;

alter table table2 add constraint fk_table2_tagid
foreign key (TagId) references table1(TagId);

为了提高性能,我建议在 table1(TagName) 上构建索引。

关于mysql - 我可以追溯添加外键 ID 列并用另一个表的主键的值填充它,而无需手动输入吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com