Q
Problem solve Get help with specific problems with your technologies, process and projects.

Restricting cyclical references in trees

I have an employee table with manager field and a foreign key (between manager and employee id) relation to itself. Now I want to prevent any circular relations, e.g suppose employee id 2 has manager (employee id 1). How can I now prevent someone to enter employee 2 as manager for employee 1. I know I can do it programatically. Is there a way I can do it in design. Can I use this in T-SQL:

where c.asset_id = v.parent_asset_id and
(c.asset_type,d.asset_type,v.assoc_type) in (select
m.asset_key,n.asset_key,o.association_key from dbo.t_association_rule o,
dbo.t_asset_master m, dbo.t_asset_master n) 

I believe the introduction of Assertions in the SQL standard allows for this type of "constraint" to be declared (as opposed to programmed). However, I don't believe any of the larger database vendors have implemented assertions yet. Frankly, I believe there are still some significant technological hurdles to be overcome before assertions can be implemented without resulting in horrific performance issues. This model of trees is popular, though clunky in today's relational databases. Oracle and DB2 have extensions to SQL that allow for the traversal of trees; I don't believe SQL Server does. Unfortunately, I believe that your only solutions are to implement your constraint programmatically through triggers (sorry) or alter your data design (sorry). With regard to altering your data design, I might recommend Joe Celko's Associative Model of Trees covered in his book, SQL for Smarties: Advanced SQL Programming.

For More Information


This was last published in July 2002

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close