Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The case is like this. I have to delete a record from the child table.
But in the table schema there is a foreign key reference from the child
table to the parent table.

Therefore in order to delete the record, i need to set the foreign key
cell in the parent table null. But i found out that the parent record has a not null constraint on that column. therefore i am not able to set it null.

at last the child table could not be deleted. So any suggestions on how i can delete the record from the child table?
ParentTable
{
id int PK,
child_id int FK NOT NULL,
Other_id int FK NOT NULL
}

ChildTable
{
child_id int PK
}
Posted

Try thinking about a concrete example of what you are trying to do.
Consider for the moment that the parent table represents whole bicycles, which *MUST* include front wheels, and the child table represents front wheels. What you are trying to do is steal the front wheel from a bicycle and expecting it to remain a bicycle.
Life's not like that, even in databases. Even if one front wheel could belong to several bicycles, the same idea holds.

I'm sure there are much better example around, but this one should get the point across.

Cheers,
Peter
 
Share this answer
 
Comments
loveangel888 15-Aug-11 23:33pm    
I know it is a design flaw or something like that. The problem is this action is already documented and I don't really have the authority to change it.
The one who documented it left the project before this even started. So thats why I'm trying the best I can to complete the action described.
Peter_in_2780 16-Aug-11 0:34am    
Sounds like the database design needs review. Some bits of bad design can be overcome or worked around, but this doesn't look like one of them. At this point, I think the best thing to do is to take the problem to someone who has authority to make changes, and hope that they care enough to get it right. Good luck!

If you would be able to delete the child record what would be the expected result?

Or example if you have a record in parenttable which is referencing childtable with id 1. Now you delete the childtable row, what would be the value on the remaining row in child_id?

I suggest that you double check the design if the tables and if it really is a design flaw, as Peter suggested,talk to the person who's responsible for making modifications to that schema.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900