如何设置约束条件,添加“在删除案例中"? [英] How to set the constraints, add "on delete casecade"?...

查看:61
本文介绍了如何设置约束条件,添加“在删除案例中"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在网格上使用删除命令,该表的主键在另一个表的外键中.所以,我在执行删除操作时遇到此异常-DELETE语句与REFERENCE约束冲突.解决此问题.我要从中删除的表是带有主键Pid的tblProducts.并且Pid是表tblOrderItems的外键.

我看到了一个解决方案,但不明白如何使用它.----

在管理工作室中,右键单击->编辑,并在其设置约束的底部,添加"on delete casecade"并仅执行更改.

帮我解决这个问题.

谢谢
Amit

Hey everyone,

I am using a delete command on a grid,the primary key of this table in the foreign key in an another table.So,i''m getting this exception while performing delete operation--The DELETE statement conflicted with the REFERENCE constrain.How to resolve this.The table from which i''m trying to delete is tblProducts with primary key Pid.And Pid is foreign key to table tblOrderItems.

I saw a solution but can''t understand how to use it.It was--

From management studio, right click --> edit, and toward the bottom where it sets the constraints, add "on delete casecade" and execute just the alter.

Help me resolve this.

Thanks
Amit

推荐答案

首先,您应该删除引用键表记录,然后删除主键记录,如

删除tblOrderItems其中Pid =``p100'';
delete tblProducts其中pid =``p100'';
First you should delete reference key table record and then delete primary key record Like

delete tblOrderItems where Pid = ''p100'';
delete tblProducts where pid = ''p100'';


是的,必须先删除引用表行,然后才能删除主键表记录....
Yes you must delete reference table row first then only you can able to delete primary key table record ....


创建一个过程或在您的计算机上运行以下过程,它将起作用
无需内部联接即可删除参考键,您可以直接删除

Create a Procedure or run the below procedure on your machine it will work
No need inner join for deleting reference key u can just delete as it is

Create Procedure DeleteProduct
@PID varchar(100)
AS
begin transaction
DELETE tblOrderItems where Pid=@PID
DELETE tblProducts where Pid=@PID
IF @@error <> 0
BEGIN
ROLLBACK
END
ELSE
BEGIN
COMMIT
END


这篇关于如何设置约束条件,添加“在删除案例中"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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