|
|
|
|
Re: Error encountered while deleting a record with foreign key relationship. [message #627774 is a reply to message #627773] |
Sun, 16 November 2014 04:36 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
I agree with you as a quickfix to workaround for a block situation due a lack in design and specifications of business rules but not as a general method of designing an application.
The systematic usage of deferrable constraints tend to prevent designers to ask themselves "do we cover all the cases? is there any case missing?". Even if in the end all the constraints are satisfied, this does not mean that the data will be correct for a business or client point of view. Remember there are real people behind the EMPLOYEE table.
In this case, for these statements, questions like "what does mean removing a manager for the department (and its employees)?", "what does mean removing a department for its employees?" should first raise and, I bet, it will appear that writing a procedure for these actions which implements the business rules would be the best approach of the issues and remove the need of deferrable constraint.
As you said, deferrable constraints give much more flexibility in such situations but flexibility should not be the first point to address. There are some cases where it can't be done without deferrable constraints but they're not in majority.
|
|
|
Re: Error encountered while deleting a record with foreign key relationship. [message #627777 is a reply to message #627774] |
Sun, 16 November 2014 05:24 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Well, your comments have made me think a bit. I suspect that my preference for deferrable constraints (which I now question) comes from my years working on short-term contracts: a few days to fix a problem, convert to RAC, tune some SQLs, and so on. This means that I rarely have the opportunity to see the long term results of a solution, and perhaps look for too many quick fixes. Thank you for making me think.
|
|
|
Re: Error encountered while deleting a record with foreign key relationship. [message #627779 is a reply to message #627766] |
Sun, 16 November 2014 06:33 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Asfakul wrote on Sun, 16 November 2014 01:31can somebody let men know how to do the same?
Rule (implemented via self-referencing FK on nullable MANAGER_ID column) on HR.EMPLOYEES table states you can't delete employee who is a manager and has other employees working for him. So you have two options:
1. All employees working for to be deleted manager will be working for a different manager
2. All employees working for to be deleted manager will be working without manager.
Case 1 splits into two:
a) New manager is existing employee. Then we update manager id in departments table, update manager id for all employees who work for to be deleted manager and delete to be deleted manager from employee table.
b) New manager is new employee. Then we insert new manager into employees table, update manager id in departments table, update manager id for all employees who work for to be deleted manager and delete to be deleted manager from employee table.
Case 2. We update manager id with NULL for all employees who work for to be deleted manager, update manager id with NULL in departments table and delete to be deleted manager from employee table.
SY.
|
|
|