惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - Johnny shen

XP 32-bit Upgrade to Win7 32-bit Review 文章: 扩展 Reporting Services 2008 与 Reporting Services 2005:所获得的经验 Microsoft SQL Server 2008故障转移群集在Hyper-V虚拟机上的多种组建方式 Hyper-v与CPU 虚拟化典型故障一例 MS Dynamics CRM 4.0 Installation on SQL2008 error 多个微软Windows版本在未来一年内将渐渐退出并不再提供技术支持 微软拼音输入法2010 Beta2简评 - Johnny shen SQL Server 2005 OLAP技术研究与实现 自动备份多台服务器System Event Log解决方案 TCP/IP Troubleshooting Undo Windows Update Restriction - Johnny shen 苏州主讲[常见加密算法和身份验证协议探究] 上海主讲RMS信息保护 无锡主讲微软企业信息系统远程客户端安全技术和应用 AD Bulk import or export Enabling IP Routing for Windows XP On-site training for Roche Shanghai in Aug. 3 ASIA MVP Summit in Singapore ISA2004 EE Global Readness Training Camp
Storing Hierarchical Data in Oracle & MS SQL Database
Johnny shen · 2008-09-30 · via 博客园 - Johnny shen

As you may know, While hierarchical tree structures are commonly used in many applications, SQL Server has not made it easy to represent and store them in relational tables. After SQL Server2000, XML structures are excellent for managing hierarchical data relationships, of course in SQL Server 2005, developers can use recursive queries against data stored hierarchically, but it needs to use CTE(common table expressions).

In Oracle, there is one way to store hierarchical data is to create a self referencing table. It is called self referencing because the foreign key (column parent_id) references the same table as the table in which the foreign key is(same concept as we do laster in Ms Sql server 2008):

create table hierarchic_tbl (

 id        number primary key,

 parent_id references hierarchic_tbl, -- references itself

 descr      varchar2(20),

 active     number(1) not null check (active in (0,1))

);

This table is now filled with some values:

-- 1st level:

insert into hierarchic_tbl values ( 1, null, 'Language' , 1);

-- 2nd level:

insert into hierarchic_tbl values ( 2,   1, 'VB' , 1);

insert into hierarchic_tbl values ( 3,    1, 'C#' , 1);

insert into hierarchic_tbl values ( 4,    1, 'JAVA', 1);

-- 3rd level (below VB)

insert into hierarchic_tbl values ( 5,    2, 'VB6.0' , 1);

insert into hierarchic_tbl values ( 6,    2, 'VB.NET' , 1);

-- 4th level (below VB.NET)

insert into hierarchic_tbl values (7,    6, 'VB.NET2003' , 1);

insert into hierarchic_tbl values (8,    6, 'VB.NET2005' , 1);

Showing the tree with start with .. connect by:

select

 lpad(' ', (level-1)*2, ' ') || descr

from

 hierarchic_tbl

start with parent_id is null

connect by prior id = parent_id;

------------------------------------------------

Result:

Language

 VB

    VB6.0

    VB.NET

      VB.NET2003

      VB.NET2005

   C#

  Java

With the release of SQL Server 2008, now you can store hierarchical data better than before, and also provides new functions to query data without needing to use common table expressions. In SQL Server 2008, the HIERARCHYID data type has been added to help resolve this problem. It is designed to store values that represent the position of nodes of a hierarchal tree structure.

For example, the HIERARCHYID data type makes it easier to express these types of relationships without requiring multiple parent/child tables and complex joins.

l           Organizational structures

l           A set of tasks that make up a larger projects (like a GANTT chart)

l           File systems (folders and their sub-folders)

l           A classification of language terms

l           A bill of materials to assemble or build a product

l           A graphical representation of links between web pages

Unlike standard data types, the HIERARCHYID data type is a CLR user-defined type, and it exposes many methods that allow you to manipulate the date stored within it. For example, there are methods to get the current hierarchy level, get the previous level, get the next level, and many more. In fact, the HIERARCHYID data type is only used to store hierarchical data; it does not automatically represent a hierarchical structure. It is the responsibility of the application to create and assign HIERARCHYID values in a way that represents the desired relationship. Think of a HIERARCHYID data type as a place to store positional nodes of a tree structure, not as a way to create the tree structure.

Here’s a simple example of a product hierarchy structure (family, brand, category, subcategory, etc.) stored in a single table called ProductTree. First, the syntax to create the ProductTree table is as follows:

CREATE TABLE ProductTree
   (ProductTreePK int IDENTITY, 
    Description varchar(1000), 
    ProductLevelPK int, HierID hierarchyid)

Next, here’s a foreign table of descriptions for each ProductLevel:

CREATE TABLE  ProductLevels 
             (ProductLevelPK int IDENTITY, 
             Description varchar(50))
-- PK of 1 (All)
INSERT INTO ProductLevels VALUES ('All Products')
-- PK of 2 (Family)
INSERT INTO ProductLevels VALUES ('Family')
 -- PK of 3 (Brand)
INSERT INTO ProductLevels VALUES ('Brand')
-- PK of 4 (Category)
INSERT INTO ProductLevels VALUES ('Category')
-- PK of 5 (SubCategory)
INSERT INTO ProductLevels VALUES ('SubCategory')
-- PK of 6 (SKU)
INSERT INTO ProductLevels VALUES ('SKU') 

The HierarchyID data type contains methods to Get Descendants, Ancestors, as well as a root definition. So you can use the following functions as part of building an “API” for inserting into/retrieving from a hierarchical data type.

hierProductParentID.GetDescendant(@LastChild,NULL)
hierarchyid::GetRoot()
GetAncestor(1)

With an API for inserting, you can make creating data very easy.

exec InsertProductTree  null,'All Products', 1 

exec InsertProductTree  1 ,'Family A', 2 
exec InsertProductTree  1 ,'Family B', 2 
exec InsertProductTree  1 ,'Family C', 2 

declare @TempParent int = 
 (SELECT ProductTreePK FROM ProductTree WHERE 
   Description = 'Family A') 

exec InsertProductTree 
     @TempParent ,'Family A - Brand AA', 3

exec InsertProductTree  
     @TempParent ,'Family A - Brand AAA', 3

Then, you can run a query using GetAncestor to determine each parent and list Stored Hierarchical Data.