Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing one-to-zero-or-one relation in SQL Server

I'm using Entity Framework 4.1 database first approach. I've used legacy database. In my edmx file which created entity classes based on tables in the legacy database, there is a one-to-zero-or-one association between some entities.

Although I explored the tables of database and relation between them I didn't find out how one-to-zero-or-one relation have been implemented in database.

For more information I put some screenshots of my database diagram and the property of its relation and correspondent entities in edmx file:

enter image description hereenter image description here

like image 357
Mostafa Avatar asked Oct 04 '11 06:10

Mostafa


People also ask

How will you implement one-to-many relationship in SQL?

How to implement one-to-many relationships when designing a database: Create two tables (table 1 and table 2) with their own primary keys. Add a foreign key on a column in table 1 based on the primary key of table 2. This will mean that table 1 can have one or more records related to a single record in table 2.

Do SQL columns start at 0 or 1?

Bookmark this question. Show activity on this post.

What is a zero to one relationship?

A zero to many optional relationship indicates that a person may have no phone, one phone, or many phones, and that the phone may not be "owned," but can only be owned by a maximum of one person. A zero to one relationship might indicate that a person may be a programmer, but a programmer must be a person.


1 Answers

The 1-0..1 relation in your database is directly visible. It is built between Course and OnlineCourse tables where Course is principal in relation (1) and OnlineCourse is dependent with FK configured on CourseID. FK is also PK of the OnlineCourse = it must be unique and because of that it is 0..1.

Database "always" uses 1 - 0..1 because real 1 - 1 cannot be effectively used for data insertion. 1 - 1 means that left must be inserted after right but right must be inserted after left = impossible. Because of that 1 - 0..1 is used where left is principal and must be inserted before right and right is dependent and must be inserted after left.

like image 105
Ladislav Mrnka Avatar answered Oct 31 '22 00:10

Ladislav Mrnka