OrientDB中关联数据的查看方法

How to view linked data in OrientDB

我有两个table,描述如下。

Table 1: 国家

c_id, int
c_name, varchar(20) (PK)

此 table 中的示例记录是。

c_id | c_name
1 | USA
2 | UK
3 | PAK

Table 2:移民

i_id, int
i_name, varchar(20)
i_country, int (FK)

此 table 中的示例记录是。

i_id | i_name | i_country
1 | John | 1
2 | Graham | 2
3 | Ali | 3

问题一:

我想在 OrientDB 中创建两个 类 (tables),但我需要知道 FK 字段的数据类型应该是什么以及要在其中插入什么。我的意思是我应该在查询中写什么来插入 PK table 的 id。是否需要@rid?怎么样?

问题 2:

生成以下输出的 OrientDB SQL 是什么。

i_id | i_name | i_country | c_id | c_name
1 | John | 1 | 1 | USA
2 | Graham | 2 | UK
3 | Ali 3 | PAK 

OrientDB 不支持连接。

您应该将 class Immigrant 中的 i_country 字段声明为 Link to Country class,或者如果您需要双向关系,则可以使用图形模型。

如果您想使用直接 link

你可以这样插入移民

insert into Immigrant set id=1, name ='John', country = (select from country where id = 1 )

然后

select id,name,country.id,country.name form Immigrant

使用 OrientDB,您可以通过在记录之间使用直接边 links 来避免创建 FK 字段和连接操作。例如:

create class Country extends V;
create class Immigrant extends V;
create class comesFrom extends E;

create property Country.c_id integer;
create property Country.c_name String;
create property Immigrant.i_id integer;
create property Immigrant.i_name String;

insert into Country(c_id, c_name) values (1, USA);
insert into Country(c_id, c_name) values (2, UK);
insert into Country(c_id, c_name) values (3, PAK);

insert into Immigrant(i_id, i_name) values (1, John);
insert into Immigrant(i_id, i_name) values (2, Graham);
insert into Immigrant(i_id, i_name) values (3, Ali);

现在你可以直接连接你想要的记录(我使用了一个名为 'comesFrom' 的边缘和子查询到 link id 字段,但你也可以直接使用 @RID 字段)

create edge comesFrom from (select from Immigrant where i_id = 1) to (select from Country where c_id = 1);
create edge comesFrom from (select from Immigrant where i_id = 2) to (select from Country where c_id = 2);
create edge comesFrom from (select from Immigrant where i_id = 3) to (select from Country where c_id = 3);

终于不用join操作就可以查询自己想要的字段了:

select i_id, i_name, out('comesFrom').c_id as c_id, out('comesFrom').c_name as c_name 
from Immigrant unwind c_id, c_name

----+------+----+------+----+------
#   |@CLASS|i_id|i_name|c_id|c_name
----+------+----+------+----+------
0   |null  |1   |John  |1   |USA
1   |null  |2   |Graham|2   |UK
2   |null  |3   |Ali   |3   |PAK
----+------+----+------+----+------

在 Orient Studio 中,您应该获得如下图表:

您可以使用这样的 javascript 函数来加速插入和 link 操作:

所以当你决定创建一个新人时,边会自动创建。你也可以决定把人放在哪里:

对你有帮助吗?

我认为您创建了文档数据库,因为我通过创建文档数据库而不是图形数据库重现了该问题,并且我和您一样遇到了同样的异常。这是因为如果您想使用顶点和边,您必须使用图形数据库类型。