如果要使用像C#这样的4GL实现树,并将其存储在数据库(如SQL Server 2008)中,那么架构/设计会是什么样的?
换句话说,数据库在这样的实现中扮演什么角色?
答案 0 :(得分:6)
有几种选择:
注意:对于后缀数组,您将不会存储任何字符,您只需存储描述每个元素的索引,如下所示:
CREATE TABLE SUFFIX_ARRAY (
ORDER INT PRIMARY KEY, -- Position in the suffix array.
START INT NOT NULL, -- Position of the starting character of the suffix within the target string.
LONGEST_COMMON_PREFIX INT NOT NULL -- If useful for your application.
)
您还必须单独存储“目标”字符串(例如,在另一个表中的CLOB中)。
答案 1 :(得分:4)
RDBMS中的树结构通常使用交叉引用表和递归查询的组合进行处理。
Text
================
id -- autoincrement
text -- varchar
Text_Suffix
=================
startingTextId -- fk reference to Text.id
suffixPartId -- fk reference to Text.id
So... with this example data -
Text
=================
1 | lay
2 | er
3 | ing
4 | s
Text_Suffix
==================
1 | 2
1 | 3
1 | 4
2 | 4
你会使用这样的查询:
WITH All_Suffixes (id, text) as (SELECT id, text
FROM Text as a
EXCEPTION JOIN Text_Suffix as b
ON b.suffixPartId = a.id
UNION ALL
SELECT b.suffixPartId, a.text + c.text
FROM All_Suffixes as a
JOIN Text_Suffix as b
ON b.startingTextId = a.id
JOIN Text as c
ON c.id = b.suffixPartId)
SELECT *
FROM All_Suffixes
哪个应该生成如此结果:
1 | lay
2 | layer
3 | laying
4 | lays
4 | layers