创立 VERTEX LABEL:
CREATE VERTEX person (code text, name text, age int, PRIMARY KEY(code));CREATE VERTEX card (code text, PRIMARY KEY(code));
创立 EDGE LABEL:
CREATE EDGE owns (from person, to card);CREATE EDGE translate (from card, to card, id text, amount int, DISCRIMINATOR(id));
创立图:
创立图的时候能够指定不同存储类型的图,目前反对的类型有 magmaap,orc,创立的语法如下:
CREATE GRAPH graphname(vertex(person, card), edge(owns, translate))format ‘magmaap’;CREATE GRAPH graphname(vertex(person, card), edge(owns, translate))format ‘orc’;
创立图的时候默认是不创立主键的索引的,如果须要创立图的时候依据点和边的主键创立惟一索引,能够用如下语句:
CREATE GRAPH graphname(vertex(person, card), edge(owns, translate))format ‘magmaap’ with(primaryindex=false);
注:orc 格局的图不反对惟一索引
创立 index
能够对图中任一类型的点或边创立索引, 创立 vertex index 语法如下:
CREATE INDEX indexname ON graphname.person (age);CREATE UNIQUE indexname INDEX ON graphname.person (code);
创立 edge index 时,能够任意抉择 edge 的属性和源点、指标点的主键,源点和指标点的主键用 FROM 和 TO 示意:
CREATE INDEX indexname ON graphname.translate (FROM, TO, id);
这样索引有三列,(src_person.code, dst_person.code, id)
也能够建设反转 edge 的 index,如
CREATE INDEX indexname ON graphname.translate (TO, FROM, id);
该语句的索引列为 (dst_person.code, src_person.code, id)
注:创立 UNIQUE INDEX 时必须向左蕴含 primary key/DISCRIMINATOR。如给 person 创立 UNIQUE INDEX, 则必须蕴含 code,且 code 在最右边。
插入数据
INSERT INTO graphname.person VALUES(‘1234567’, ‘ 小李 ’, 24);INSERT INTO graphname.person VALUES(‘2345678’, ‘ 小王 ’, 25);INSERT INTO graphname.card VALUES(‘6666666’);INSERT INTO graphname.card VALUES(‘7777777’);INSERT INTO graphname.owns VALUES(‘1234567’, ‘6666666’);INSERT INTO graphname.owns VALUES(‘2345678’, ‘7777777’);INSERT INTO graphname.translate VALUES(‘6666666’, ‘7777777’,’t123′, 10000);
查问数据
独自查问 vertex:
SELECT * FROM graphname.person;
独自查问 edge:
SELECT * FROM graphname.translate
依据一条 edge 查问 vertex 的信息:
SELECT FROM (SELECT FROM graphname.translate WHERE id=’t123′)AS t1 INNER JOIN graphname.owns ON t1.src_code=graphname.owns.dst_code OR t1.dst_code=graphname.owns.dst_code INNER JOIN graphname.person ON graphname.owns.src_code=graphname.person.code;
删除 graph
创立出的图、点、边、索引均能够 drop
DROP GRAPH graphname;DROP VERTEX person;DROP VERTEX card;DROP EDGE owns;DROP EDGE translate;DROP INDEX indexname;