如何在 Mysql 工作台中创建零或一对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25086363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:56:44  来源:igfitidea点击:

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

mysqlsqlmysql-workbench

提问by Redaa

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.

我想到的第一个想法是使外键可以为空,这样在创建订单时外键可以为空,但我不知道该怎么做。我正在研究一个模型,所以我可以将它呈现给我的“老师”,所以我需要知道如何使用 EER 图来做到这一点。

thanks in advance.

提前致谢。

采纳答案by K.A.F.

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.

像往常一样创建您的 1:n 关系。然后双击表格以显示详细信息。在新的 FK 字段上,取消选中“NN”列。现在您的 FK 可以为 NULL。

回答by gomi

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

“关系”选项卡中“外键”选项卡中的强制选项似乎是控制可选性的方式。