I have a parent/child relationship defined which, unfortunately, cannot be maintained via a foreign key. Parents and children are stored in the same table. The parent/child relationship is identified by a column "ITEM_ID". A child ITEM_ID consists of its parent ITEM_ID and then what we can effectively think of as the child's unique identifier within that parent item.
I'm implementing a PL/SQL procedure to delete records from this ITEM table. The first part of the procedure checks for the existence of any children; if so, it raises an application error (simulating a foreign key).
I'm wondering how to prevent child records from being inserted by another process between the time the cursor in my delete procedure is populated and locked "FOR UPDATE", and the point at which the parent record is actually deleted.
Do I need to lock this entire table during this process?
Or should I alter the insert procedure to select the parent record "FOR UPDATE"?
UPDATE: I created the example above just to describe the general situation, but to justify my foreign key/constraint issue I'll give my actual/more complicated structure below:
There are several tables in a remote database: COMPANY, BUILDING, FLOOR. In our organization, Floors belong to buildings and Buildings belong to Companies.
The application I'm working on associates roles with employees. The table that ties an Employee (Employee ID) with a Role (Role ID) also has a "Location" column. The location column corresponds to an ID one of the remote database tables, and we identify which table it belongs to based on the Type column in the Role table.
For example, here are a couple records from my table:
Role = Janitor
Type = BUILDING
Location = COMPANY1-BUILDING1
Parent Role = Manager
Role = Manager
Type = COMPANY
Location = COMPANY1
Parent Role = CEO
As you can guess, Floor identifiers have a format of company-building-floor in the remote database table.
Janitor is tied to the BUILDING level, and as such its Location column is a BUILDING identifier (which is actually the company identifier followed by a building identifier in the BUILDING table).