Question

I'm trying to create a hierarchy of components, fx.:

CREATE TABLE COMPONENTS(id, name, price, component_type)

CREATE TABLE SUB_COMPONENTS_1(...)

CREATE TABLE SUB_COMPONENTS_2(...)

Where the SUB_COMPONENTS tables is using the supertype's id column as foreign key, and they get 'populated' depending on what component_type it is. (i.e. the component_type column)

I've been trying to follow: http://bytes.com/topic/sql-server/answers/808389-design-question-type-heirarchy-supertype-queries But can't get my head around how he can make "vehicle_type" UNIQUE, and be able to create multiple entries in the vehicle table with the same "vehicle_type" ?

Any help understanding this, would be much appreciated!

Updated:

Relational schema's without relationships is about as far as I've come:

Components(component_id: serial, name: string, type: string, price: double)

Wheels(wheel_id=component_id, kind: string, price=component_price)

Motor(motor_id=component_id, kind: string, price=component_price)

Cars(name: string, total_price, wheels=component_id, motor=component_id....)

Was it helpful?

Solution

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).

OTHER TIPS

To achieve this you just need two tables

CREATE TABLE COMPONENTS(id, component_type)
CREATE TABLE SUB_COMPONENTS(s_id, name, Price)

  ID   Component_Type
  1   MIG
  2   SU

SUB_COMPONENT contains s_id as foreign key So the table values will be 

  1   MIG   $30
  1   MIG-15   $20
  1   MIG-17   $30
  2   SU-25   $30
  2   SU-25   $20

You can include as many subtypes you need in the sub_Components table with price. You can get the results with this query
SELECT * FROM SUB_COMPONENTS where s_id = 1 ( FOR MIG Type Or 2 for SU Type )
OR join the tables and order by name You get the list in hierarchy.


According to my understanding I hope this will answer your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top