To answer your question about understanding what is going on in the link you posted, the example is not saying that vehicle_type
is unique, it's saying that a combination of vin
and vehicle_type
is unique. The example is using the combination of these two columns as a key. However, I would recommend using explicit id's as keys (e.g. ComponentID
would be the primary key on the Components
table and the foreign key on each of the subcomponent tables.) Something like this:
CREATE TABLE Components
(
ComponentID int identity(1,1) not null,
// Other columns here
CONSTRAINT [PK_ComponentID] PRIMARY KEY CLUSTERED
(
[ComponentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
)
CREATE TABLE SubComponents1
(
SubComponent1ID int identity(1,1) not null,
ComponentID int not null,
// Other columns here
CONSTRAINT [PK_SubComponent1ID] PRIMARY KEY CLUSTERED
(
[SubComponent1ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
)
GO
ALTER TABLE SubComponents1 WITH NOCHECK ADD CONSTRAINT [FK_SubComponents1_Components] FOREIGN KEY(ComponentID)
REFERENCES Components (ComponentID)
GO
ALTER TABLE SubComponents1 CHECK CONSTRAINT [FK_SubComponents1_Components]
GO
I will ammend this answer to include more details pertaining to your problem if you can better explain what, specifically, your issue is (along with schemas of your child tables).