mysqlcardinality

MySQL How to create one to Zero or Many relation


i have entity customer and entity order, a customer can have 0 or more orders but an order can only have 1 customer.

I already tried a lot of things like making the foreign key unchecked at NN but I cant get a foreign key order at customer

EDIT: Using mySQL ERD workbench


Solution

  • Your customer table will have a unique customer_id column. In the MySQL world we often use autoincrementing primary keys for this kind of id column.

    Your order table will have a customer_id column that's a foreign key to customer.customer_id.

    This allows the order table to have any number of rows relating to a particular customer_id: none, one, or many. The foreign key relationship, when enforced--checked--simply prevents an order from having a customer_id value that references no valid customer.

    Classic data design tools with their distinction between logical and physical design can drive ya nuts when you're trying to do easy stuff like this.

    Pro tip if you name your id columns the same way everywhere they're used, data design tools tend to work better, especially when "reverse-engineering" your tables. That's why I suggested column names like customer.customer_id and order.customer_id rather than customer.id and order.customer_id.