如果存在链接表,如何永久删除记录?
假设我有这 2 个表 QuesType 和 Ques:-
QuesType
QuestypeID|QuesType |Active
------------------------------------
101 |QuesType1 |True
102 |QuesType2 |True
103 |XXInActiveXX |False
Ques
QuesID|Ques|Answer|QUesTypeID|Active
------------------------------------
1 |Ques1|Ans1 |101 |True
2 |Ques2|Ans2 |102 |True
3 |Ques3|Ans3 |101 |True
在 QuesType 表中:- 主键
QuesTypeID 是Ques 表中的 :- QuesID 是主键,QuesType ID 是从 QuesType 表引用 QuesTypeID 的外键
现在我无法从 QuesType 表中删除记录,我只能通过设置 Active=False 使 QuesType 处于非活动状态。 我无法永久删除 QuesTypes,因为它与 Ques Table 具有外键关系。所以,我只是设置了 列 Active=false 并且这些 Questypes 在绑定时不会显示在我的网格上。
我想要做的是能够永久删除任何 QuesType。现在只有在没有任何地方使用的情况下才能删除 在 Ques 表中,对吗?
因此,要永久删除任何 QuesType,我认为这就是我可以做的:-
在显示 QuesTypes 的网格中,我有一个用于活动的复选框和一个用于删除的按钮。我的想法是,当用户 使某些 QuesType 处于非活动状态,然后 OnCheckChanged() 事件将运行,并且将包含删除所有问题的代码 在使用该 QuesTypeID 的 Ques 表中。然后在 QuesType 网格上,该 QuesType 将显示为 Deactivated 并且仅 然后用户可以永久删除它。
我的想法正确吗?
目前在我的 DeleteQuesType 存储过程中我正在做的是:-
设置 Active=false 和 设置 QuesTye= 一些字符串,如 XXInactiveXX
还有其他方法吗?
编辑 如果我只想以这种方式实现,比如首先让用户停用 QuesType,并且只有在停用后用户才能永久删除它,该怎么办?这是正确的逻辑吗?
Let's say I have these 2 tables QuesType and Ques:-
QuesType
QuestypeID|QuesType |Active
------------------------------------
101 |QuesType1 |True
102 |QuesType2 |True
103 |XXInActiveXX |False
Ques
QuesID|Ques|Answer|QUesTypeID|Active
------------------------------------
1 |Ques1|Ans1 |101 |True
2 |Ques2|Ans2 |102 |True
3 |Ques3|Ans3 |101 |True
In the QuesType Table:-
QuesTypeID is a Primary key
In the Ques Table:-
QuesID is a Primary key and QuesType ID is the Foreign Key that refernces QuesTypeID from QuesType Table
Now I am unable to delete records from QuesType Table, I can only make QuesType inactive by setting Active=False.
I am unable to delete QuesTypes permanently because of the Foreign key relation it has with Ques Table. So , I just set the
column Active=false and those Questypes then don't show on my grid when its bound.
What I want to do is be able to delete any QuesType permamnently. Now it can only be deleted if its not being used anywhere
in the Ques table, right?
So to delete any QuesType permanently I thought this is what I could do:-
In the grid that displays QuesTypes, I have this check box for Active and a button for delete.What I thought was, when a user
makes some QuesType inactive then OnCheckChanged() event will run and that will have the code to delete all the Questions
in Ques table that are using that QuesTypeID. Then on the QuesType grid, that QuesType would show as Deactivated and only
then can a user delete it permanently.
Am I thinking correctly?
Currently in my DeleteQuesType Stored Procedure what I am doing is:-
Setting the Active=false and
Setting QuesTye= some string like XXInactiveXX
Is there any other way?
edit
What if I want to implement that way only, like first get the user to deactivate the QuesType and only if it's deactivated that the user can delete it permanently. Is that correct logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要做的就是将该
子句放在你的外键约束上。当删除相应的 QuesType 行时,这将自动从 Ques 表中删除行。
I think what you need to do is put the
clause on your foreign key constraint. This will automatically delete rows from your Ques table when its corresponding QuesType row is deleted.
您只能删除已停用的
QuesTypes
。在删除
QuesType
之前,您必须查询数据库并查看是否QuesType.Active = false
。如果是,您可以从子表中删除引用该QuesType.Id
的所有行(这称为级联删除)。您还可以在子行中的 QuesTypeID 列上设置空值。阅读 这本书在线了解ON DELETE CASCADE | SET NULL 子句。该条款将使您能够做您想做的事。
You can delete only the
QuesTypes
deactivated.Before deleting a
QuesType
you'll have to query the database and see ifQuesType.Active = false
. If yes, you can delete all rows from the child table that reference thatQuesType.Id
(this is known as a cascade delete). You can also set a null value on QuesTypeID column in the child rows.Read this book online to learn about the ON DELETE CASCADE | SET NULL clause. This clause will enable you to do what you want.