Skip to content

Graph DML

This section describes how to add vertices and edges to an AkasicDB graph, delete them from the graph, and update their properties.


Prepare Graph

-- Define a graph named 'community_graph'
SELECT akasicdb.define_graph('community_graph');

-- Add a vertex definition named 'person' to the 'community_graph' graph definition
SELECT akasicdb.define_vertex(
  'community_graph', 
  'person',
  ARRAY['name text', 'age int']
);

-- Add an edge definition named 'knows' to the 'community_graph' graph definition
SELECT akasicdb.define_edge(
  'community_graph', 'knows', 
  'person', 'person', 
  ARRAY['since timestamptz']
);

-- Create a graph based on the 'community_graph' graph definition
SELECT akasicdb.create_graph('community_graph');

Add Vertices and Edges

Warning

Adding vertices or edges with SQL INSERT does not work correctly. Use the provided functions instead.

Add Vertices

-- Add four 'person' vertices to the 'community_graph' graph
SELECT akasicdb.insert_vertex(
  'community_graph', 
  'person', 
  '{"name": "Alice", "age": 30}'
);
SELECT akasicdb.insert_vertex(
  'community_graph', 
  'person', 
  '{"name": "Bob", "age": 25}'
);
SELECT akasicdb.insert_vertex(
  'community_graph', 
  'person', 
  '{"name": "Charlie", "age": 42}'
);
SELECT akasicdb.insert_vertex(
  'community_graph', 
  'person', 
  '{"name": "Diana", "age": 28}'
);

Function arguments

akasicdb.insert_vertex(
    graph_name,     -- Name of the graph to which the vertex is added
    vertex_label,   -- Vertex label
    prop_kv         -- Vertex properties as a JSON object in {"column_name": property_value} key-value format
);

Creates a new vertex from the specified label and property values, then adds it to the graph.

The prop_kv parameter specifies the properties of the vertex to add. Each key must match a property name defined for the specified vertex label. Properties that are not specified are stored as NULL.

The vertex_id is assigned automatically and returned by the function.

Add Edges

-- Add a knows edge to the 'community_graph' graph that connects vertex IDs 1 and 2
-- In this example, the vertices correspond to 'Alice' and 'Bob'.
SELECT akasicdb.insert_edge(
  'community_graph', 
  'knows', 1, 2, 
  '{"since": "2020-01-01T00:00:00Z"}'
);

SELECT akasicdb.insert_edge(
  'community_graph', 
  'knows', 2, 4, 
  '{"since": "2018-09-01T00:00:00Z"}'
);

Function arguments

akasicdb.insert_edge(
    graph_name, -- Name of the graph to which the edge is added
    edge_label, -- Edge label
    src_vid,    -- Source vertex ID
    dst_vid,    -- Destination vertex ID
    prop_kv     -- Edge properties as a JSON object in {"column_name": property_value} key-value format
);

Creates a new edge from the specified label, property values, and source/destination vertex IDs, then adds it to the graph.

The source and destination vertex IDs must refer to vertices that already exist in the graph.

The prop_kv parameter specifies the properties of the edge to add. Each key must match a property name defined for the specified edge label. Properties that are not specified are stored as NULL.

AkasicDB automatically assigns an edge_id to the new edge and returns it as the function result.

Warning

AkasicDB Community Edition limits the number of vertices and edges in a graph. You can store up to 1,000,000 (1M) vertices or edges per vertex or edge label.

Delete Vertices and Edges

Warning

Deleting vertices or edges with SQL DELETE may not work as expected. Use the provided functions instead.

Delete Vertices

  • Delete a vertex conditionally
-- Delete the vertex with ID 3 and the person label from the 'community_graph' graph
-- Since the vertex has no connected edges, the operation succeeds without an error.
SELECT akasicdb.delete_vertex_restrict(
  'community_graph', 
  'person', 3
);

Function arguments

akasicdb.delete_vertex_restrict(
    graph_name,     -- Graph from which to delete the vertex
    vertex_label,   -- Vertex label
    vertex_id       -- ID of the vertex to delete
);

Finds the vertex with the specified label and ID, and deletes it only if it has no connected edges. If the vertex has connected edges, the function raises an exception.

If the vertex is successfully deleted, the function returns 1. If no matching vertex exists and no vertex is deleted, the function returns 0.

  • Delete a vertex with cascade
-- Delete the vertex with ID 1 and the person label from the 'community_graph' graph
-- The connected edge (knows, 1 -> 2) is also deleted.
SELECT akasicdb.delete_vertex_cascade(
  'community_graph', 
  'person', 1
);

Function arguments

akasicdb.delete_vertex_cascade(
    graph_name,     -- Graph from which to delete the vertex
    vertex_label,   -- Vertex label
    vertex_id       -- ID of the vertex to delete
);

Finds the vertex with the specified label and ID, and deletes it along with all connected edges.

If the vertex is successfully deleted, the function returns 1. If no matching vertex exists and no vertex is deleted, the function returns 0.

Delete Edges

-- Delete the edge with ID 2 and the knows label from the 'community_graph' graph
SELECT akasicdb.delete_edge(
  'community_graph', 
  'knows', 2
);

Function arguments

akasicdb.delete_edge(
    graph_name, -- Graph from which to delete the edge
    edge_label, -- Edge label
    edge_id     -- ID of the edge to delete
);

Finds and deletes the edge with the specified label and ID.

If the edge is successfully deleted, the function returns 1. If no matching edge exists and no edge is deleted, the function returns 0.

Update Vertex and Edge Properties

-- Update the properties of the vertex with ID 4 and the person label
-- in the 'community_graph' graph
UPDATE community_graph.person
SET name = 'Daniel'
WHERE vertex_id = 4;

You can update vertex and edge properties using the SQL UPDATE statement.

As with updating column values in a relational database table, specify the vertex or edge label, the property to update, the new value, and the condition used to select the target vertices or edges.

Warning

If you recreate a graph using the replace_if_exists option of akasicdb.create_graph(), all previously applied vertex and edge insertions, deletions, and property updates are lost.