简体   繁体   English

使用外键更新表数据

[英]Update table data using foreign key

I have two tables structure like below 我有两个表结构如下

Table1 表格1

Serial  |   Src            | Albumid(primarykey)  
________|__________________|________
   1    |   /root/wewe.jpg |   20
   2    |   /root/wewe.jpg |   21
   3    |   /root/wewe.jpg |   21
   4    |   /root/wewe.jpg |   23
   5    |   /root/wewe.jpg |   18

Table2 表2

Albumid |  Albumname       | AlbumCover  //albumid is secondary key ref. to first table
________|__________________|________
   20   |   AAA            |   null
   21   |   bbb            |   null
   31   |   vcc            |   null
   42   |   ddd            |   null
   18   |   eee            |   null

I followed this POST two update my Albumcover in Table2 using Serial no. 我按照此POST两次使用Serial no.更新了Table2 Albumcover Serial no. of first table.. 第一张桌子的..

create proc AddCover @Serial int
as
Begin
update Table1 set albumcover='somthing' where table1.serial = @Serial
end

Can i do like this using foregin key constraint?? 我可以使用foregin键约束来做到这一点吗?

You'll need to do the update on Table2. 您需要在Table2上进行更新。 To tell it to have a condition based on values from table1, check this post for examples: 要使其具有基于table1中的值的条件,请查看此帖子以获取示例:

MySQL - UPDATE query based on SELECT Query MySQL-基于SELECT查询的UPDATE查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM