I have a specialization hierarchiy with one parent table and two children. I am in a situation where one column in the child table is a pk, fk, fk and unique.
TICKET_NUMBER = pk, fk1, fk2, u1
This is how I have currently tried to create this table but im not too sure if I have done it right or not.
CREATE TABLE PART_ITEM
(
TICKET_NUMBER INT NOT NULL CONSTRAINT FK1_PART_ITEM_TICKET_NUMBER REFERENCES dbo.TICKET_LINES(TICKET_NUMBER),
TICKET_LINE_NUMBER INT NOT NULL,
LINE_TYPE VARCHAR(30) NOT NULL CONSTRAINT CHECK_PART_ITEM CHECK(LINE_TYPE = 'Part')--check to make sure that this is a part item line
FOREIGN KEY REFERENCES dbo.TICKET_LINES(LINE_TYPE),--reference TICKET_LINES table
PART_ID INT NOT NULL CONSTRAINT PART_ITEM_UI1 UNIQUE(PART_ID)-- set unique
FOREIGN KEY REFERENCES dbo.PARTS(PART_ID), --reference PARTS table
QUANTITY INT NOT NULL,
FOREIGN KEY(TICKET_NUMBER, TICKET_LINE_NUMBER) REFERENCES dbo.TICKET_LINES(TICKET_NUMBER, TICKET_LINE_NUMBER),
FOREIGN KEY (TICKET_NUMBER) REFERENCES dbo.TICKET_LINES(TICKET_LINE_NUMBER),
UNIQUE(TICKET_NUMBER),
PRIMARY KEY(TICKET_NUMBER, TICKET_LINE_NUMBER)
);
Cheers!