I’m having a problem in an oracle SQL project I’m working on:
I have a (weak) entity ‘Ticket’ that has as primary key: (Customer_id,packet_id,project_id,ticket_id). Customer_id, packet_id and project_id are also foreign keys.
But, a ticket can only be for a packet OR a project, and because they are both in the primary key, they can’t be null, while one of them actually will always be null. But I need both because a ticket always belongs to a one of them.
I thought of a possible solution and I thought I could make 1 ID for all products, but then there is another problem, because if I want to implement this product_id as a foreign key, I don’t know to which entity I should let it reference to.
Is there a way to make ‘optional primary keys’ or work with if-statements when creating tables? Or a way to make an optional reference? I’ve tried if-statements and a case but it didn’t work.
A lot will depend on exactly what these entities represent. Since a ticket can be for either a packet or a project, that implies that there is some higher-level entity that combines both packets and projects. That could either be modeled as a new table that is the parent to both
PACKETandPROJECTor it could be modeled by combining thePACKETandPROJECTtable and adding aTYPEcolumn that differentiates between packets and projects. TheTICKETtable would then reference either the new parent table or the combined table.Taking a step back, though, if the
TICKETentity has aTICKET_ID, it seems likely that theTICKET_IDalone should be the primary key. It seems unusual that you would have a composite primary key if you have aTICKET_IDcolumn– do you really want to allow multiple rows in theTICKETtable with the sameTICKET_IDthat are related to different customers, packets, and projects? IfTICKET_IDalone is the primary key, then bothPACKET_IDandPROJECT_IDcan be nullable foreign keys and you can create aCHECKconstraint that ensures that exactly one of the two is non-NULL.