15 pts.
 Recursive Association with compound primary key in SQL database
Hello, I'm afraid I'm very new to this and really struggling. I have a table with a compound primary key composing of Code, Date, Client to represent an attendee on a course. I need to include a 'Lead Client' column to represent a recursive relationship where one attendee can be nominated to lead others attending the same course – i.e. can lead other attendees with the same Code and Date only. The Code and Date need to be the same for the Lead Client as for the Client. At the moment I have set up a composite Foreign Key in the table which references the Primary Key of the same table and comprises of 3 columns: Lead_Code, Lead_Date and Lead_Client. I have set up the FK so that either all or none are null, and have set up the Lead_Code and Lead_Date so that their values must match the Code and Date of the same record. However - this does not seem to be a very tidy or efficient way of storing the data, as each row where a Lead_Client is listed will basically contain two columns containing the same Code, and two containing the same Date. It also means that anyone inserting data into the table will need to enter the Code and Date twice wherever a Lead_Client is listed. I can't help feeling that there must be a simpler solution but I can't work it out. Ideally I would like to simply include a 'Lead Client' column, but somehow force the table to only accept values where Lead_Client belongs to a record as the Client (as part of the PK) where the Code and Date match the Code and Date of the original Attendee record. Example: Code CRS1 Date 01/10 Client AA Lead_Client Null Code CRS1 Date 01/10 Client BB Lead_Client AA Code CRS2 02/10 Client CC Lead_Client AA = NOT ALLOWED as Client AA is not an attendee on course Code CRS2 on Date 02/10 Apologies if this doesn’t make sense – I’d be really grateful for any help on this as I’m really stuck. Thanks!

Software/Hardware used:
ASKED: January 1, 2011  11:53 PM
UPDATED: January 3, 2011  1:49 PM

Answer Wiki:
I see three options for you. 1. Change the foreign key so that it uses the same columns for the parent and the child. 2. Add computed columns to the table so that if the Lead_Client value is not null then the values are entered in automatically. 3. Change the front end so that the only people who show up in the Lead_Client drop down list are the ones who have the same Code and Date. Option #3 would be the best option as this will prevent the users from selecting incorrect leads (which they will if you let them).
Last Wiki Answer Submitted:  January 2, 2011  8:38 am  by  Denny Cherry   64,550 pts.
All Answer Wiki Contributors:  Denny Cherry   64,550 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Thank you so much!!

I have implemented option 2 for the meantime and it’s working fine – might have a go at option 3 later.

Really appreciate your help

 15 pts.