Question

Lets say you have two entity named Parent and Child. Child entity is DEPENDENT of Parent entity. A weak key of child entity is the NAMEOFCHILD. Is it possible for the Parent entity to have NAMEOFCHILD as a foreign key?

This idea has not been talked about in class. I was wondering is this possible in SQL? If so, should i just add

 FOREIGN KEY (NAMEOFCHILD) source CHILD 

in my table?

Was it helpful?

Solution

In the database schema, yes (if Child.NAMEOFCHILD has a unique index). In entity framework, no. EF doesn't support associations to unique indexes (yet). But this is just on the technical level. Whether it's meaningful is another question.

Also, beware of painting yourself in a corner. When both foreign keys are not nullable you'd never be able to insert data, because you can't insert two records at a time and sequential inserts always cause foreign key violations. You would be able to design the database schema but never get any data in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top