question

whitey_1963 avatar image
whitey_1963 asked

hierarchyid column on a table with multiple hierarchies?

I am designing a database which implements self-describing data. this has a "data_def" table which defines all my various data elements, e.g.:

data_id   description
=======   ===========
1         customers
2         employees
3         locations

etc. I then have another table which defines all the possible values of each of those data sets, e.g.

data_id  data_value_id   description
=======  =============   ===========
1        1               acme industries
1        2               acme widgets inc.
2        1               bob smith
2        2               john doe
2        3               robert bobert
3        1               planet earth
3        2               europe
3        3               poland
3        4               zurich

etc. Now as you can imagine, within each "data_id" the list of possible values is hierarchical, i.e. data_id=2 is a list of employees, and obviously an employee structure has parent-child relationships. Similarly data_id = 3 would have a hierarchy, e.g. Zurich would be under Switzerland under Europe under Planet Earth.

If I defined data_id as type INT and data_value_id as type herarchyid, will SQL Server "realise" that for each data_id, the data_value_id values form separate hierarchies, which are totally separate by data_id? Or does the hierarchy relationship apply across the entire table even if the unique clusterd index / primary key is data_id followed by data_value_id?



hierarchyid
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Jeff Moden avatar image
Jeff Moden answered

You wrote...

"f I defined data_id as type INT and data_value_id as type herarchyid, will SQL Server "realise" that for each data_id, the data_value_id values form separate hierarchies, which are totally separate by data_id?"

The answer is "NO". HierarchyID is nothing more than a special datatype. YOU have to determine the values of what HierarchyID is going to be and assign them accordingly. That should be in the documentation for HierarchyID.

https://learn.microsoft.com/en-us/sql/relational-databases/tables/tutorial-using-the-hierarchyid-data-type

https://learn.microsoft.com/en-us/sql/t-sql/data-types/hierarchyid-data-type-method-reference

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.