SQL - CREATE EDGE
Creates a new edge between vertices in the database. An edge can connect from and to a vertex record, an array of vertex records, or a query result of vertex records.
Syntax
CREATE EDGE <type> [BUCKET <bucket>] FROM <rid>|[<rid>[,]*]|(<query>) TO <rid>|[<rid>[,]*]|(<query>)
[UNIDIRECTIONAL]
[IF NOT EXISTS]
[SET <field> = <expression>[,]*]|CONTENT {<JSON>}
[RETRY <retry> [WAIT <pauseBetweenRetriesInMs]]
[BATCH <batch-size>]
-
<type>defines the type name for the edge. -
<bucket>defines the bucket in which you want to store the edge. -
UNIDIRECTIONALcreates a unidirectional edge; by default edges are bidirectional. -
IF NOT EXISTSskips the creation of the edge if another edge already exists with the same direction (same from/to) and same edge type. -
JSONprovides JSON content to set as the record. Use this instead of entering data field by field. -
RETRYdefines the number of retries to attempt in the event of conflict (optimistic approach). -
WAITdefines the time to delay between retries in milliseconds. -
BATCHdefines whether it breaks the command down into smaller blocks and the size of the batches. This helps to avoid memory issues when the number of vertices is too high. By default, it is set to100.
Edges and Vertices form the main components of a Graph database. ArcadeDB supports polymorphism on edges.
When no edges fail to create, ArcadeDB throws a CommandExecutionException error.
This makes it easier to integrate edge creation in transactions.
In such cases, if the source or target vertices don’t exist, it rolls back the transaction.
Examples
-
Create a new edge type and an edge of the new type:
ArcadeDB> CREATE EDGE TYPE E1 ArcadeDB> CREATE EDGE E1 FROM #10:3 TO #11:4
-
Create an edge in a specific bucket:
ArcadeDB> CREATE EDGE E1 BUCKET EuropeEdges FROM #10:3 TO #11:4
-
Create an edge and define its properties:
ArcadeDB> CREATE EDGE FROM #10:3 TO #11:4 SET brand = 'fiat'
-
Create an edge of the type
E1and define its properties:
ArcadeDB> CREATE EDGE E1 FROM #10:3 TO #11:4 SET brand = 'fiat', name = 'wow'
-
Create multiple edges from a single source vertex to many destination records:
ArcadeDB> CREATE EDGE E FROM #1:0 TO [#4:0, #7:0];
-
Create no edges from a single source vertex (this is useful when using subqueries for the destination):
ArcadeDB> CREATE EDGE E FROM #1:0 TO [];
-
Create edges of the type
Watchedbetween all action movies in the database and the user Luca, using sub-queries:
ArcadeDB> CREATE EDGE Watched FROM (SELECT FROM account WHERE name = 'Luca') TO
(SELECT FROM movies WHERE type.name = 'action')
-
Create an edge using JSON content:
ArcadeDB> CREATE EDGE E FROM #22:33 TO #22:55 CONTENT { "name": "Jay",
"surname": "Miner" }
-
Create an edge only if not previously created:
ArcadeDB> CREATE INDEX Watched_out_in ON Watched (`@out`, `@in`) UNIQUE
ArcadeDB> CREATE EDGE Watched FROM (SELECT FROM account WHERE name = 'Luca') TO
(SELECT FROM movies WHERE type.name = 'action') IF NOT EXISTS
Why there is no UPSERT for edges
The UPDATE … UPSERT clause is supported on document and vertex types, but not on edge types: an edge always needs FROM and TO endpoints, which an UPDATE WHERE clause cannot supply. The idempotent equivalent for edges is CREATE EDGE … IF NOT EXISTS, ideally combined with a UNIQUE index on ( so the uniqueness check is atomic under concurrent writers (see the example above).@out, @in)
A common pattern is to upsert the two vertices first, then connect them:
ArcadeDB> UPDATE Person SET name = 'Luca', email = '[email protected]' UPSERT WHERE email = '[email protected]' ArcadeDB> UPDATE City SET name = 'London' UPSERT WHERE name = 'London' ArcadeDB> CREATE EDGE Lives FROM (SELECT FROM Person WHERE email = '[email protected]') TO (SELECT FROM City WHERE name = 'London') IF NOT EXISTS
For an end-to-end multi-statement version (with retry on conflict), see SQL Script.
For more information, see:
-
UPDATE(UPSERTclause)