我正在尝试使用此处描述的物化路径模型实现树状结构:http://www.dbazine.com/oracle/or-articles/tropashko4.
是否可以在[path]字段上强制引用完整性?我不知道SQL如何做到这一点,我是否必须在DAL中手动完成?
在SQL Server中是否存在与CONNECT BY等效的子句.使用parentId字段构建类别树的要求.
我有一个像这样的分层类结构:
类别 - >模板 - >实例
一个类别包含多个模板,一个模板包含多个实例.
如果我通过所有3个表的连接查询数据库中的数据,则数据如下所示:
CategoryID | CategoryName | CategoryType | TemplateID | TemplateName | TemplateXYZ | InstanceID | InstanceName
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 1 | "InstA"
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 2 | "InstB"
1 | "CatA" | "TypeX" | 1 | "TempA" | "X" | 3 | "InstC"
1 | "CatA" | "TypeX" | 1 | "TempB" | "Y" | …
Run Code Online (Sandbox Code Playgroud) 我正在设计一个数据库,我对在关系数据库中使用Hierarchical数据模型有一些疑问.
如果我想处理类别,子类别和父类别,是否可以不在关系数据库中使用分层数据模型?换句话说,可以使用关系做事方式处理类别,子类别和父类别?
顺便说一下,我正在使用PostgreSQL.
对不起,我的英语不好.
最好的祝福,
我正在尝试学习MySQL,所以我创建了一个小博客系统.
我在MySQL中有3个表:
posts
:
id | title
----------------
1 | Post Title 1
2 | Post Title 2
Run Code Online (Sandbox Code Playgroud)
categories
:
id | title | parent
--------------------------------
10 | category10 | 0
11 | category11 | 0
12 | category12 | 10
Run Code Online (Sandbox Code Playgroud)
post_category_relations
:
id | post_id | category_id
----------------------------------
1 | 1 | 10
2 | 2 | 12
3 | 3 | 11
Run Code Online (Sandbox Code Playgroud)
每个帖子可以有多个类别,它们的关系存储在post_category_relations中:
因此,当我访问index.php?category = 10时,我想让每个帖子都category10
包含与其子文件夹中的帖子相关的内容category12
.
PHP中我未完成的片段
$folder_id = $_GET["category"]; // Get Category ID …
Run Code Online (Sandbox Code Playgroud) 我有一个自引用表,内容如下:
Self-referencing parent table
ID ParentID Name
---------------------
1 John
2 1 Mike
3 2 Erin
4 1 Janie
5 Eric
6 5 Peter
Run Code Online (Sandbox Code Playgroud)
树层次结构应如下所示
还有一个子表,它存储父表的叶子,如下所示:
ID Sales
3 100
3 100
4 200
4 200
6 300
6 300
6 300
Run Code Online (Sandbox Code Playgroud)
我正在尝试将叶子节点的总和汇总到层次结构,以便它将返回...
ID Name Sum
1 John 800
2 Mike 200
3 Erin 200
4 Janie 400
5 Eric 900
6 Peter 900
Run Code Online (Sandbox Code Playgroud)
有什么想法如何在sql 2008中实现这一点?提前致谢.
我知道有一些关于这个的帖子,但它们大约一年没有回复.实际上我们在PostgreSQL 8.4上使用Hibernate 4.2.1.Final.我们有两个像这样的实体
实体A(顶级层次结构类)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Polymorphism(type = PolymorphismType.EXPLICIT)
public class A {
@Id
@GeneratedValue
private Long id;
public A() {
}
public A(Long id) {
super();
this.id = id;
}
// Setters, getteres, hashCode and equals
}
Run Code Online (Sandbox Code Playgroud)
实体B(子类)
@Entity
public class B extends A{
String value;
public B(){
}
public B(String value) {
super();
this.value = value;
}
public B(Long id, String value) {
super(id);
this.value = value;
}
// Setters, getteres, hashCode and equals
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,实体使用注释 …
在查询数据库以查找嵌套在闭包表中的注释之后,比如Bill Karwin在这里建议什么是将平面表解析为树的最有效/优雅的方法?,我现在从SQL获得以下数据结构:
"comments": [
{
"id": "1",
"breadcrumbs": "1",
"body": "Bell pepper melon mung."
},
{
"id": "2",
"breadcrumbs": "1,2",
"body": "Pea sprouts green bean."
},
{
"id": "3",
"breadcrumbs": "1,3",
"body": "Komatsuna plantain spinach sorrel."
},
{
"id": "4",
"breadcrumbs": "1,2,4",
"body": "Rock melon grape parsnip."
},
{
"id": "5",
"breadcrumbs": "5",
"body": "Ricebean spring onion grape."
},
{
"id": "6",
"breadcrumbs": "5,6",
"body": "Chestnut kohlrabi parsnip daikon."
}
]
Run Code Online (Sandbox Code Playgroud)
使用PHP我想重构这个数据集,所以注释嵌套如下:
"comments": [
{
"id": "1",
"breadcrumbs": …
Run Code Online (Sandbox Code Playgroud) 我想在MySQL数据库中表示递归的父子关系.我想创建一个category
- subcategory
关系.一个类别可以有N个子类别,每个子类别可以有N个子类别,依此类推.我正在考虑让一个category
带有外键的表指向它自己.这就是我的意思:
CREATE TABLE `category` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`parent_category` int NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`parent_category`) REFERENCES `category` (`id`)
)
Run Code Online (Sandbox Code Playgroud)
parent_category
如果类别是顶级类别,则可以为null.
这是表示这样的关系的正确方法吗?在我的设计(性能,查询......)中还应该考虑其他事项吗?
我正在寻找一个解决方案Active Record
来获取单个查询中的分层数据.
我现在正在做的是首先获取所有数据,然后使用递归函数将数组转换为所需的数组.
$allUsers = User::find()
->asArray()
->all();
$arr = Yii::$app->TreeComponent->getUserChildren($allUsers, $userId, $userId);
Run Code Online (Sandbox Code Playgroud)
并在TreeComponent中
public function getUserChildren($src_arr, $currentId, $userId, $parentFound = false)
{
$cats = array();
foreach ($src_arr as $row) {
if ($row['id'] == $userId) {
$row['parent'] = "";
}
if ((!$parentFound && $row['id'] == $currentId) || $row['parent'] == $currentId) {
$rowData = array();
foreach ($row as $k => $v)
$rowData[$k] = $v;
$cats[] = $rowData;
if ($row['parent'] == $currentId) {
$cats = array_merge($cats, $this->fetchRecursive($src_arr, $row['id'], true));
} …
Run Code Online (Sandbox Code Playgroud)