My problem, simplified and translated to something any programmer can relate to, looks like this:
- I have an Issue Tickets table. Each entry (ticket) has a type: simple or complex. I use a numerical unique id as PK.
A complex ticket needs additional information compared to a simple ticket, so there's another table just for complex tickets. One of such info must reference a different ticket id. Imagine that this is because a complex ticket is linked to a pre-existing ticket issue from which it depends (always exactly only one and never its own!).
- The Complex Ticket table uses as PK a FK: the Issue Tickets PK. Since the complex ticket always needs to specify another ticket id (constraints: not its own), I need to use another FK to Issue Tickets as a 'normal' field.
Can I do something of the sort? Is it a bad pattern?
CREATE TABLE Issue_Tickets
(
ticket_id INT AUTO_INCREMENT NOT NULL,
ticket_type CHAR(8) NOT NULL,
PRIMARY KEY (ticket_id)
)
CREATE TABLE Complex_Tickets
(
ticket_id INT NOT NULL,
father_ticket_id INT NOT NULL,
FOREIGN KEY (ticket_id) REFERENCES Issue_Tickets(ticket_id),
FOREIGN KEY (father_ticket_id) REFERENCES Issue_Tickets(ticket_id),
PRIMARY KEY (ticket_id)
CONSTRAINT no_self_reference CHECK (ticket_id <> father_ticket_id)
)