我有一个关系数据库模式,如:
的数据
DDL
CREATE TABLE Persons
(
Name VARCHAR2(100) NOT NULL,
CONSTRAINT Persons_PK PRIMARY KEY (Name)
);
CREATE TABLE Groups
(
Name VARCHAR2(100),
CONSTRAINT Groups_PK PRIMARY KEY (Name)
);
CREATE TABLE Person_Group_Connections
(
Person_Name VARCHAR2(100),
Group_Name VARCHAR2(100),
CONSTRAINT PG_Connection_PK PRIMARY KEY (Person_Name, Group_Name),
CONSTRAINT PG_Connection_Persons_FK FOREIGN KEY (Person_Name) REFERENCES Persons (Name),
CONSTRAINT PG_Connection_Groups_FK FOREIGN KEY (Group_Name) REFERENCES Groups (Name)
);
insert into Persons (name) values ('Person A');
insert into Persons (name) values ('Person B');
insert into Persons (name) values ('Person C');
insert into Persons (name) values ('Person D');
insert into Persons (name) values ('Person E');
insert into Persons (name) values ('Person F');
insert into Groups (name) values ('Group 1');
insert into Groups (name) values ('Group 2');
insert into Groups (name) values ('Group 3');
insert into Groups (name) values ('Group 4');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person A', 'Group 1');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person B', 'Group 1');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person B', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person C', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person D', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person D', 'Group 3');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person E', 'Group 3');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person F', 'Group 3');
字符串
然后我定义一个有顶点和边的Proerty图:
CREATE PROPERTY GRAPH my_graph
VERTEX TABLES (
Persons LABEL Person PROPERTIES ( name ),
Groups LABEL Group PROPERTIES ( name )
)
EDGE TABLES (
Person_Group_Connections as PG_Connection
SOURCE KEY ( person_name ) REFERENCES Persons ( name )
DESTINATION KEY (group_name) REFERENCES Groups ( name )
LABEL person_connected_to_group
) OPTIONS (PG_VIEW);
型
的
如果不知道跳数,如何编写将两个“Person”顶点链接在一起的查询?
例如:“Person A”如何连接到“Person F”我希望查询返回:"Person A" "Person B" "Person D" "Person F"
或"Person A" → "Group 1" → "Person B" → "Group 2" → "Person D" → "Group 3" → "Person F"
或者是否有更好的方法来定义属性图?
SELECT *
FROM MATCH (P1:Person)-/:person_connected_to_group*/->(g:Group)<-/:person_connected_to_group*/-(P2:Person) on my_graph
WHERE P1.name = 'Person 1' and P2.name = 'Person 2'
型
1条答案
按热度按时间6fe3ivhb1#
您能检查一下下面的查询是否符合您的要求吗?
使用PG视图:
个字符
图形服务器(PGX)还支持以下查询:
的字符串