Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zero or one to many relationship in Mysql workbench

In a database that have Orders, Customers tables, i need the relation between these two tables to be zero or one to many relationship, simply because in my application, an order can exist without being assigned to a customer.

One first idea that comes to my mind is to make the foreign key nullable, this way when creating an order the foreign key can be null, but i don't know how to do so. I'm working on a model so i can render it to my 'teacher', so i need to know how to do it using the EER diagram.

thanks in advance.

like image 975
Redaa Avatar asked Aug 01 '14 18:08

Redaa


Video Answer


3 Answers

Mandatory option in "Foreign Key" tab in "Relationship" tab appears to be the way to control optionality.

like image 103
gomi Avatar answered Oct 07 '22 06:10

gomi


Create your 1:n relationship as normal. Then double click the table to bring up the details. On your new FK field, uncheck the "NN" column. Now your FK can be NULL.

like image 33
K.A.F. Avatar answered Oct 07 '22 06:10

K.A.F.


Simply do it this way

  • Right click on the relationship and click on "Edit Relationship"
  • Switch to the "Foreign Key" tab by click on the tabs below the interface
  • Under Reference table section, uncheck the checkbox labeled "Mandatory"

There you go.enter image description here

like image 41
Henry Obiaraije Avatar answered Oct 07 '22 08:10

Henry Obiaraije