SQL 和 nGQL

基本概念对比

概念名称SQLnGQL
vertex\vertex
edge\edge
vertex type\tag
edge type\edge type
vertex identifier\vid
edge identifieredge id generated by defaultsrc, dst, rank
columncolumn\
rowrow\

语法对比

数据定义语言(DDL)

数据定义语言(DDL)用于定义数据库 schema。 DDL 语句可以创建或修改数据库的结构。

对比项SQLnGQL
创建图空间(数据库)CREATE DATABASE <database_name>CREATE SPACE <space_name>
列出图空间(数据库)SHOW DATABASESSHOW SPACES
使用图空间(数据库)USE <database_name>USE <space_name>
删除图空间(数据库)DROP DATABASE <database_name>DROP SPACE <space_name>
修改图空间(数据库)ALTER DATABASE <database_name> alter_option\
创建 tags/edges\CREATE TAG | EDGE <tag_name>
创建表CREATE TABLE <tbl_name> (create_definition,…)\
列出表列名SHOW COLUMNS FROM <tbl_name>\
列出 tags/edges\SHOW TAGS | EDGES
Describe tags/edge\DESCRIBE TAG | EDGE <tag_name | edge_name>
修改 tags/edge\ALTER TAG | EDGE <tag_name | edge_name>
修改表ALTER TABLE <tbl_name>\

索引

对比项SQLnGQL
创建索引CREATE INDEXCREATE {TAG | EDGE} INDEX
删除索引DROP INDEXDROP {TAG | EDGE} INDEX
列出索引SHOW INDEX FROMSHOW {TAG | EDGE} INDEXES
重构索引ANALYZE TABLEREBUILD {TAG | EDGE} INDEX <index_name> [OFFLINE]

数据操作语言(DML)

数据操作语言(DML)用于操作数据库中的数据。

对比项SQLnGQL
插入数据INSERT IGNORE INTO <tbl_name> [(col_name [, col_name] …)] {VALUES | VALUE} [(value_list) [, (value_list)]INSERT VERTEX <tag_name> (prop_name_list[, prop_name_list]) {VALUES | VALUE} vid: (prop_value_list[, prop_value_list])
INSERT EDGE <edge_name> ( <prop_name_list> ) VALUES | VALUE <src_vid> -> <dst_vid>[@<rank>] : ( <prop_value_list> )
查询数据SELECTGO, FETCH
更新数据UPDATE <tbl_name> SET field1=new-value1, field2=new-value2 [WHERE Clause]UPDATE VERTEX <vid> SET <update_columns> [WHEN <condition>]
UPDATE EDGE <edge> SET <update_columns> [WHEN <condition>]
删除数据DELETE FROM <tbl_name> [WHERE Clause]DELETE EDGE <edge_type> <vid> -> <vid>[@<rank>] [, <vid> -> <vid> …]
DELETE VERTEX <vid_list>
拼接数据JOIN|

数据查询语言(DQL)

数据查询语言(DQL)语句用于执行数据查询。本节说明如何使用 SQL 语句和 nGQL 语句查询数据。

  1. SELECT
  2. [DISTINCT]
  3. select_expr [, select_expr] ...
  4. [FROM table_references]
  5. [WHERE where_condition]
  6. [GROUP BY {col_name | expr | position}]
  7. [HAVING where_condition]
  8. [ORDER BY {col_name | expr | position} [ASC | DESC]]
  1. GO [[<M> TO] <N> STEPS ] FROM <node_list>
  2. OVER <edge_type_list> [REVERSELY] [BIDIRECT]
  3. [WHERE where_condition]
  4. [YIELD [DISTINCT] <return_list>]
  5. [| ORDER BY <expression> [ASC | DESC]]
  6. [| LIMIT [<offset_value>,] <number_rows>]
  7. [| GROUP BY {col_name | expr | position} YIELD <col_name>]
  8. <node_list>
  9. | <vid> [, <vid> ...]
  10. | $-.id
  11. <edge_type_list>
  12. edge_type [, edge_type ...]
  13. <return_list>
  14. <col_name> [AS <col_alias>] [, <col_name> [AS <col_alias>] ...]

数据控制语言(DCL)

数据控制语言(DCL)包含诸如 GRANTREVOKE 之类的命令,这些命令主要用来处理数据库系统的权限,其他控件。

对比项SQLnGQL
创建用户CREATE USERCREATE USER
删除用户DROP USERDROP USER
更改密码SET PASSWORDCHANGE PASSWORD
授予权限GRANT <priv_type> ON [object_type] TO <user>GRANT ROLE <role_type> ON <space> TO <user>
删除权限REVOKE <priv_type> ON [object_type] TO <user>REVOKE ROLE <role_type> ON <space> FROM <user>

数据模型

查询语句基于以下数据模型:

MySQL

image

Nebula Graph

image

增删改查(CRUD)

本节介绍如何使用 SQL 和 nGQL 语句创建(C)、读取(R)、更新(U)和删除(D)数据。

插入数据

  1. mysql> INSERT INTO player VALUES (100, 'Tim Duncan', 42);
  2. nebula> INSERT VERTEX player(name, age) VALUES 100: ('Tim Duncan', 42);

查询数据

Find the player whose id is 100 and output the name property:

  1. mysql> SELECT player.name FROM player WHERE player.id = 100;
  2. nebula> FETCH PROP ON player 100 YIELD player.name;

更新数据

  1. mysql> UPDATE player SET name = 'Tim';
  2. nebula> UPDATE VERTEX 100 SET player.name = "Tim";

删除数据

  1. mysql> DELETE FROM player WHERE name = 'Tim';
  2. nebula> DELETE VERTEX 121;
  3. nebula> DELETE EDGE follow 100 -> 200;

示例查询

示例 1

返回年龄超过 36 岁的球员。

  1. mysql> SELECT player.name
  2. FROM player
  3. WHERE player.age < 36;

使用 nGQL 查询有些不同,因为您必须在过滤属性之前创建索引。更多信息请参见 索引文档

  1. nebula> CREATE TAG INDEX player_age ON player(age);
  2. nebula> REBUILD TAG INDEX player_age OFFLINE;
  3. nebula> LOOKUP ON player WHERE player.age < 36;

示例 2

查找球员 Tim Duncan 并返回他效力的所有球队。

  1. mysql> SELECT a.id, a.name, c.name
  2. FROM player a
  3. JOIN serve b ON a.id=b.player_id
  4. JOIN team c ON c.id=b.team_id
  5. WHERE a.name = 'Tim Duncan';
  1. nebula> CREATE TAG INDEX player_name ON player(name);
  2. nebula> REBUILD TAG INDEX player_name OFFLINE;
  3. nebula> LOOKUP ON player WHERE player.name == 'Tim Duncan' YIELD player.name AS name | GO FROM $-.VertexID OVER serve YIELD $-.name, $$.team.name;

示例 3

查找球员 Tim Duncan 的队友。

  1. mysql> SELECT a.id, a.name, c.name
  2. FROM player a
  3. JOIN serve b ON a.id=b.player_id
  4. JOIN team c ON c.id=b.team_id
  5. WHERE c.name IN (SELECT c.name
  6. FROM player a
  7. JOIN serve b ON a.id=b.player_id
  8. JOIN team c ON c.id=b.team_id
  9. WHERE a.name = 'Tim Duncan');

在 nGQL 中,我们使用管道将上一条语句的输出作为下一条语句的输入。

  1. nebula> GO FROM 100 OVER serve YIELD serve._dst AS Team | GO FROM $-.Team OVER serve REVERSELY YIELD $$.player.name;