View Single Post

  #1 (permalink)  
Old 04-17-2007
Vojta
 
Posts: n/a
Default Organisation tree - prevent deletion of parent organisation

Please, can you advise? I store organisation tree in single table and I need
to prevent deletion of organisation that has child organisations. I store
organisation structure in following table:

CREATE TABLE Organisations (
Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
ParentId INT UNSIGNED,
Name VARCHAR(50)
)

ParentId points to parent organisation. If it is NULL then the organisation
is root organisation. Otherwise it is child of parent identified by
ParentId.

I need to prevent deletion of any organisation that has children
organisations. It means if there exists at least one record whose ParentId
equals to Id of record to delete, deletion should not be executed. Is it
good idea to use self referencing foreign key (reference within single
table) and how to do it? Or is there another better way?

Thanks a lot! Vojta


Reply With Quote