On my database structure in SQL Server, I have 3 types of products which requires different information about the order. So, I created one Customers
table and three different orders tables: OrdersForProductAs
, OrdersForProductBs
, OrdersForProductCs
. All orders table has one to many relationship on Customers
table.
I also have another table which is Payments
and will hold the payment details inside. But I have doubts here on how to structure it.
As I have multiple product types and a customer may have orders for multiple products at the same time, I need to relate those three order tables to Payments
table.
The other issue is that a customer may have an order for only one type of product. So, the FK columns on Payments
table needs to be nullable
.
My question is whether those nullable
FK columns would be a headache for me on the long run or not? Generally speaking, would it be considered as a bad practice to have nullable FK columns on a table?
Best Answer
I'd question why you have
OrdersForProductX
tables at allIt's possible the FK problem you've asked about can be designed out...
If these tables have the same structure, then you simply need a
ProductType
column on someOrderProduct
table. ThenPayment
just links to that with one FKIf the table have different structures, I assume they have some common attributes. So, you can have a common
OrderProduct
table then specific child table per product type (see below) Again,Payment
just links to the commone table with one FKThis is the "superkey/subtype pattern"
(OrderID, ProductType)
OrderProduct
OrderProductA
OrderProductB