Subject | Re: Which way to set up Foreign Key? |
---|---|
Author | Adam |
Post date | 2006-09-13T02:52:12Z |
--- In firebird-support@yahoogroups.com, "Anthony Tanas" <anthony@...>
wrote:
there must exist a record in DOCTOR (matched between
DOCTORINSURANCE.DOCTORID and DOCTOR.DOCTORID).
If a doctor can be a member of multiple doctorinsurance relationships,
then the foreign key must go the other way. Your foreign key should
then rather constrain the DOCTORINSURANCE table.
alter table DOCTORINSURANCE
add constraint FK_DOCTORINSURANCE_DOCTOR
foreign key (DOCTORID)
references DOCTOR (DOCTORID)
on delete CASCADE
on update CASCADE;
If you need to enforce the existence of at least one DOCTORINSURANCE
record, then you could use a stored procedure to add both in one
operation.
Adam
wrote:
>each
> I need to set up Foreign Key constraints in my database.
>
> I'm not sure which table I should add the constraint to. Here is an
> example:
>
> I have a DOCTOR table.
> I have an INSURANCE table.
>
> They are joined via a DOCTORINSURANCE table that contains the PK for
> table and then adds certain credential information for a doctor for thatSo you are saying that for a DOCTORINSURANCE record to be 'valid',
> insurance company.
there must exist a record in DOCTOR (matched between
DOCTORINSURANCE.DOCTORID and DOCTOR.DOCTORID).
If a doctor can be a member of multiple doctorinsurance relationships,
then the foreign key must go the other way. Your foreign key should
then rather constrain the DOCTORINSURANCE table.
alter table DOCTORINSURANCE
add constraint FK_DOCTORINSURANCE_DOCTOR
foreign key (DOCTORID)
references DOCTOR (DOCTORID)
on delete CASCADE
on update CASCADE;
If you need to enforce the existence of at least one DOCTORINSURANCE
record, then you could use a stored procedure to add both in one
operation.
Adam