如何更新,从具有主键和foriegn键约束的多个表中删除记录? [英] how to update,delete record from multiple table having primary key and foriegn key constrain?

查看:93
本文介绍了如何更新,从具有主键和foriegn键约束的多个表中删除记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有桌子:1)用户

字段:姓名,

地址,

暴徒,

log_id



2)登录

字段:log_id

用户名,

密码



当我更新或删除用户表中的记录,更新或删除登录表中的记录时也已完成。

我必须使用存储过程。我不知道如何更新,使用多个表中的存储过程删除记录。

推荐答案

您可以打开级联删除 [ ^ ],允许您删除外键行。
You can turn on Cascade Delete [^] which will allow you to allow you to delete foreign key rows.






如果我们知道你在更新或删除时可用的字段会有所帮助,但在这里我会假设你有这些字段在用户表中。



更新



Hi,

It would help if we knew what fields you have available on update or delete, but here I will assume you have al the fields in the user table.

For Update

Create Procedure ProcUpdateUser()
@logid int,
@name varchar(50)

as 
begin
update user set name=@name where log_id=@logid
update login set username=@name where log_id=@logid

end





删除





For Delete

Create Procedure ProcDeleteUser

@name varchar(50)

as
begin
delete user where name=@name
delete login where username=@name

end







您需要根据需要更改参数,但希望这会有所帮助。




You will need to change the parameters according to your needs but hope this helps.


这篇关于如何更新,从具有主键和foriegn键约束的多个表中删除记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆