gsql/ Defining a Graph Schema
Last Updated: October 20, 2018

How to design and formalize vertices, edges, and graph containers using GSQL DDL.

Defining a Graph Schema

A graph schema is the formal dictionary that defines your entities (vertices) and relationships (edges).

1. Defining Vertices

Use CREATE VERTEX to define a global entity.

Syntax Options

  1. Using PRIMARY_ID (Standard):

    gsqlterminal
    CREATE VERTEX Person (PRIMARY_ID user_id STRING, name STRING, age INT)

    Note: Use WITH primary_id_as_attribute="true" if you need to access the ID like a regular attribute in queries.

  2. Using PRIMARY KEY:

    gsqlterminal
    CREATE VERTEX Book (id UINT PRIMARY KEY, title STRING)
  3. Composite Keys:

    gsqlterminal
    CREATE VERTEX Movie (id UINT, title STRING, year UINT, PRIMARY KEY (title, year, id))

2. Defining Edges

Edges connect a source vertex to a target vertex.

Undirected Edges (Bidirectional)

Useful for friendships or peer relationships.

gsqlterminal
CREATE UNDIRECTED EDGE Friend_Of (FROM Person, TO Person, on_date DATETIME)

Directed Edges (One-way)

Useful for hierarchical or flow relationships.

gsqlterminal
CREATE DIRECTED EDGE Sequel_Of (FROM Book, TO Book) WITH REVERSE_EDGE="Preceded_By"

Note: WITH REVERSE_EDGE automatically creates a corresponding edge in the opposite direction, which is essential for "backward" traversal performance.

Edge Discriminators

To allow multiple instances of the same edge type between two vertices, use a DISCRIMINATOR:

gsqlterminal
CREATE DIRECTED EDGE Study_At (FROM Person, TO University, DISCRIMINATOR(class_year INT), major STRING)

3. Defining the Graph

The CREATE GRAPH statement groups vertex and edge types into a usable database domain.

gsqlterminal
# Include specific types CREATE GRAPH MySocialGraph (Person, Friend_Of) # Include ALL defined types CREATE GRAPH EverythingGraph (*)

Working with Graphs

  • USE GRAPH <name>: Set the active graph for your session.
  • DROP GRAPH <name>: Deletes the graph definition and all local types (global types remain).
  • DROP ALL: Non-reversible command that wipes the entire graph store and catalog.

Global vs. Local Scope

  • Global Types: Created in global mode. Shared across multiple graphs. Any change to a global vertex is seen by all graphs using it.
  • Local Types: Created within a specific graph's schema change job. Visible only to that graph.