Sql 需要向表中添加FK约束,同时保留现有数据';s与另一个表的关系
我目前拥有此模式,以及两个表中的一些数据:Sql 需要向表中添加FK约束,同时保留现有数据';s与另一个表的关系,sql,database,tsql,database-design,relational-database,Sql,Database,Tsql,Database Design,Relational Database,我目前拥有此模式,以及两个表中的一些数据: animal table pet_accessories table +---------------+---------+ +-----------------------+-----------+ | animal_key | type | | pet_accessories_key | animal | +---------------+---------+ +-
animal table pet_accessories table
+---------------+---------+ +-----------------------+-----------+
| animal_key | type | | pet_accessories_key | animal |
+---------------+---------+ +-----------------------+-----------+
| 1 | Dog | | 1 | Dog |
| 2 | Cat | | 2 | Bird |
| 3 | Bird | | 3 | Cat |
+---------------+---------+ | 4 | Cat |
+-----------------------+-----------+
但是,需要在具有FK约束的表之间添加一个关系(从pet_附件到动物表)。最终,这就是我需要的:
animal table pet_accessories table
+---------------+---------+ +-----------------------+---------------+
| animal_key | type | | pet_accessories_key | animal_key |
+---------------+---------+ +-----------------------+---------------+
| 1 | Dog | | 1 | 1 |
| 2 | Cat | | 2 | 3 |
| 3 | Bird | | 3 | 2 |
+---------------+---------+ | 4 | 2 |
+-----------------------+---------------+
我已尝试将新的键列添加到现有的pet_附件表中,但无法正确设置此动物_键:
+-----------------------+-----------+--------------+
| pet_accessories_key | animal | animal_key |
+-----------------------+-----------+--------------+
| 1 | Dog | |
| 2 | Bird | |
| 3 | Cat | |
| 4 | Cat | |
+-----------------------+-----------+--------------+
我知道SQL主要是一种面向集合的语言——在其中使用循环通常是个坏主意。我还读到,我可能会使用游标,尽管我对它们不太熟悉
问题是,循环遍历pet_accessories.animal中的数据并与animals.type进行比较的最佳方法是什么,这样我就可以最终为所有现有的pet_accessories记录设置pet_accessories.animal_key=animal.animal_key?换句话说,我如何:
for each record in pet_accessories
for each record in animal
if pet_accessories.animal == animal.type
then pet_accessories.animal_key = animal_animal_key
首先,添加列:
alter table pet_accessories add animal_key integer;
update pa
set animal_key = a.animal_key
from pet_accessories pa join
animals a
on pa.animal = a.type;
然后,更新列:
alter table pet_accessories add animal_key integer;
update pa
set animal_key = a.animal_key
from pet_accessories pa join
animals a
on pa.animal = a.type;
然后,检查确保一切都是你想要的
然后,删除旧列:
alter table pet_accessories drop column animal;
然后添加外键约束:
alter table add constraint fk_animal_key
foreign key (animal_key) references animal(animal_key);
首先,添加列:
alter table pet_accessories add animal_key integer;
update pa
set animal_key = a.animal_key
from pet_accessories pa join
animals a
on pa.animal = a.type;
然后,更新列:
alter table pet_accessories add animal_key integer;
update pa
set animal_key = a.animal_key
from pet_accessories pa join
animals a
on pa.animal = a.type;
然后,检查确保一切都是你想要的
然后,删除旧列:
alter table pet_accessories drop column animal;
然后添加外键约束:
alter table add constraint fk_animal_key
foreign key (animal_key) references animal(animal_key);
如果我有一百万条记录要更新怎么办?我怎样才能使它充满活力?这就是为什么我问如何遍历每条记录。@LeonardoLopez。我不明白你的评论。这将一次性为查询中的所有记录分配
animal\u键
。对此问题表示抱歉。你完全正确,戈登。只是这个sql语句看起来如此简单和优雅,我以为它只是在更新第一条记录。介意用文字解释一下它是如何从一个记录到下一个记录的吗?@LeonardoLopez。SQL就是这样工作的——同时在整个数据表上工作。如果我有一百万条记录要更新呢?我怎样才能使它充满活力?这就是为什么我问如何遍历每条记录。@LeonardoLopez。我不明白你的评论。这将一次性为查询中的所有记录分配animal\u键
。对此问题表示抱歉。你完全正确,戈登。只是这个sql语句看起来如此简单和优雅,我以为它只是在更新第一条记录。介意用文字解释一下它是如何从一个记录到下一个记录的吗?@LeonardoLopez。SQL就是这样工作的——同时在整个数据表上工作。