문제

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?

도움이 되었습니까?

해결책

I think what you need to do is put the

ON DELETE CASCADE

clause on your foreign key constraint. This will automatically delete rows from your Ques table when its corresponding QuesType row is deleted.

다른 팁

You can delete only the QuesTypes deactivated.

Before deleting a QuesType you'll have to query the database and see if QuesType.Active = false. If yes, you can delete all rows from the child table that reference that QuesType.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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top