Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between SQL and nGQL

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the difference between SQL and nGQL". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between SQL and nGQL".

SQL (Structured Query Language) is a database language with multiple functions such as data manipulation and data definition. It is a purpose-specific programming language used to manage relational database management systems (RDBMS) or stream processing in relational flow data management systems (RDSMS).

NGQL is a declarative text query language similar to SQL. Compared with SQL, nGQL is a graph database query language that is extensible, supports graph traversal, pattern matching, and distributed transactions (under development).

Conceptual comparison item SQLnGQL point\ point edge\ edge type\ tag edge type\ edge type point ID primary key vid edge ID compound primary key start, end, rank column point or edge attribute row point or edge syntax comparison data definition language (DDL)

Data definition language (DDL) is used to create or modify the structure of a database, that is, schema.

Index comparison item SQLnGQL create index CREATE INDEXCREATE {TAG | EDGE} INDEX delete index DROP INDEXDROP {TAG | EDGE} INDEX list index SHOW INDEX FROMSHOW {TAG | EDGE} INDEXES refactoring index ANALYZE TABLEREBUILD {TAG | EDGE} INDEX [OFFLINE] data manipulation language (DML)

Data manipulation language (DML) is used to manipulate data in a database.

Data query language (DQL)

Data query language (DQL) statements are used to perform data queries. This section explains how to query data using SQL statements and nGQL statements.

SELECT [DISTINCT] select_expr [, select_expr]... [FROM table_references] [WHERE where_condition] [GROUP BY {col_name | expr | position}] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC]] GO [[TO] STEPS] FROM OVER [REVERSELY] [BIDIRECT] [WHERE where_condition] [YIELD [DISTINCT]] [| ORDER BY [ASC | DESC]] [| LIMIT [,]] [| GROUP BY {col_name | expr | position} YIELD] | [ .] | $- .id edge_type [, edge_type...] [AS] [, [AS]...] Data control language (DCL)

The data Control language (DCL) contains commands such as GRANT and REVOKE, which are mainly used to deal with permissions and other controls of the database system.

Contrast item SQLnGQL create user CREATE USERCREATE USER delete user DROP USERDROP USER change password SET PASSWORDCHANGE PASSWORD grant permission GRANT ON [object_type] TO GRANT ROLE ON TO delete permission REVOKE ON [object_type] TO REVOKE ROLE ON FROM data model

The query statement is based on the following data model:

RDBMS relation structure diagram

Nebula Graph minimum model diagram

This article will use the NBA dataset. The dataset contains two types of points, namely, two labels, player and team, and two types of edges, serve and follow.

In the relational data management system (RDBMS), we use tables to represent points and the edges associated with the points (join tables). Therefore, we created the following tables: player, team, serve, and follow. In Nebula Graph, the basic units of data are vertices and edges. Both can have properties, which are equivalent to those in RDBMS.

In Nebula Graph, the relationship between points is represented by edges. Each edge has a type, and in the NBA dataset, we use the edge types serve and follow to distinguish between the two types of edges.

Sample data inserts data in RDBMS

First, let's look at how to insert data into RDBMS. We first create some tables, and then insert data for those tables.

CREATE TABLE player (id INT, name VARCHAR, age INT); CREATE TABLE team (id INT, name VARCHAR); CREATE TABLE serve (player_id INT, team_id INT, start_year INT, end_year INT); CREATE TABLE follow (player_id1 INT, player_id2 INT, degree INT)

Then insert the data.

INSERT INTO playerVALUES (100,101,' Tony Parker', 36), (102,' LaMarcus Aldridge', 33), (103,' Rudy Gay',32), (104,' Marco Belinelli', 32), (105,' Danny Green', 31), (106,' Kyle Anderson', 25), (107,' Aron Baynes', 32), (108,' Boris Diaw', 36) (109,' Tiago Splitter', 34), (110,' Cory Joseph', 27) INSERT INTO teamVALUES (200,' Warriors'), (201,' Nuggets'), (202,03,' Rockets'), (203,' Trail'), (204,' Spurs'), (205,' Thunders'), (206,' Jazz'), (207, 'Clippers'), (208,' Kings') INSERT INTO serveVALUES (101pje 2007pr 1999), (102pjr 2001pr 2001pr 2005), (106pr 2001pr 2001pr 2009), (103pr 20191999pr 2018), (104pr 2016pr 20162015), (107pr 2017pr 2010), (108pr 2011pr 20102016), (109pr 2011pr 20112015), (105jue 202jue 2015je 2019), (109je 202je 2017rem 2019), (110memo 202remie 2007pr 2009) INSERT INTO followVALUES (1001, 101, 95), (100, 102, 91), (100), (101, 100, 95), (102, 91), (102), (102, 70), (104, 103, 50), (104, 105, 60), (105, 103, 83), (87), (106, 100, 88), (106, 81), (107, 106, 92), (107, 108, 97), (108, 109, 95), (109, 10, 10, 78), (110, 10, 72), (110, 10585); insert data in Nebula Graph

Inserting data into Nebula Graph is similar to the above. First, we need to define the data structure, that is, to create the schema. You can then choose to import the data manually or using Nebula Graph Studio (Nebula Graph's visualization tool). Here we add the data manually.

In the INSERT insert statement below, we insert player data into the graph space NBA (which is similar to inserting data in MySQL).

INSERT VERTEX player (name, age) VALUES100: ('Tim Duncan', 42), (' Tony Parker', 36), 102: ('LaMarcus Aldridge', 33), 103: (' Rudy Gay', 32), 104: ('Marco Belinelli', 32), 105: (' Danny Green', 31), 106: ('Kyle Anderson', 25), 107: (' Aron Baynes', 32), 108: ('Boris Diaw', 36), 109: (' Tiago Splitter', 34) ('Cory Joseph', 27)

Considering the space limit, we will skip the repetitive steps of inserting teams and edges here. You can click here to download the sample data and try it yourself.

Add, delete, modify and search (CRUD)

This section describes how to create (C), read (R), update (U), and delete (D) data using SQL and nGQL statements.

Insert data mysql > INSERT INTO player VALUES (100,' Tim Duncan', 42); nebula > INSERT VERTEX player (name, age) VALUES 100: ('Tim Duncan', 42); query data

Find a player with an ID of 100 and return their name property:

Mysql > SELECT player.name FROM player WHERE player.id = 100th Nebula > FETCH PROP ON player 100 YIELD player.name; update data mysql > UPDATE player SET name = 'Tim';nebula > UPDATE VERTEX 100 SET player.name = "Tim"; delete data mysql > DELETE FROM player WHERE name =' Tim';nebula > DELETE VERTEX 121 switch nebula > DELETE EDGE follow 100-> 200; index

Return players over the age of 36.

SELECT player.nameFROM playerWHERE player.age < 36

Using nGQL queries is somewhat different because you must create an index before filtering properties. See the indexing documentation for more information.

CREATE TAG INDEX player_age ON player (age); REBUILD TAG INDEX player_age OFFLINE;LOOKUP ON player WHERE player.age < 36; sample query

This section provides some sample queries for your reference.

Example 1

Query the player whose ID is 100 in the table player and return their name property.

SELECT player.nameFROM playerWHERE player.id = 100

Next, use Nebula Graph to find a player with an ID of 100 and return their name property.

FETCH PROP ON player 100 YIELD player.name

Nebula Graph uses the FETCH keyword to get the properties of a specific point or edge. In this case, the attribute is the name of point 100. The YIELD keyword in nGQL is equivalent to SELECT in SQL.

Example 2

Find the player Tim Duncan and return to all the teams he plays for.

SELECT a.id, a.name, c.nameFROM player aJOIN serve b ON a.id=b.player_idJOIN team c ON c.id=b.team_idWHERE a.name = 'Tim Duncan'

Use the following nGQL statement to do the same:

CREATE TAG INDEX player_name ON player (name); REBUILD TAG INDEX player_name OFFLINE;LOOKUP ON player WHERE player.name = = 'Tim Duncan' YIELD player.name AS name | GO FROM $- .VertexID OVER serve YIELD $- .name, $.team.name

It is important to note that the equals operation in nGQL uses C-style = = rather than SQL-style =.

Example 3

The following query is a little complicated, now let's query the teammates of the player Tim Duncan.

SELECT a.id, a.name, c.nameFROM player aJOIN serve b ON a.id=b.player_idJOIN team c ON c.id=b.team_idWHERE c.name IN (SELECT c.nameFROM player aJOIN serve b ON a.id=b.player_idJOIN team c ON c.id=b.team_idWHERE a.name = 'Tim Duncan')

NGQL uses pipes to take the result of the previous clause as input to the next clause.

GO FROM 100OVER serve YIELD serve._dst AS Team | GO FROM $- .Team OVER serve REVERSELY YIELD $$.play.name; thank you for reading, this is the content of "what is the difference between SQL and nGQL". After the study of this article, I believe you have a deeper understanding of the difference between SQL and nGQL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report