GQL schema statements

Graph Query Language (GQL) supports all GoogleSQL schema statements,including the following GQL-specific schema statements:

Statement list

NameSummary
CREATE PROPERTY GRAPH statementCreates a property graph.
DROP PROPERTY GRAPH statementDeletes a property graph.

CREATE PROPERTY GRAPH statement

Property graph definition

CREATE  [ OR REPLACE ]  PROPERTY GRAPH  [ IF NOT EXISTS ]property_graph_nameproperty_graph_content;property_graph_content:node_tables  [edge_tables ]node_tables:  NODE TABLESelement_listedge_tables:  EDGE TABLESelement_listelement_list:  (element[, ...])

Description

Creates a property graph.

Note: all GQL examples in the GQL reference use theFinGraph property graph example.To set up this property graph,seeSet up and query Spanner Graph.

Definitions

  • OR REPLACE: Replaces any property graph with the same name if it exists.If the property graph doesn't exist, creates the property graph. Can'tappear withIF NOT EXISTS.
  • IF NOT EXISTS: If any property graph exists with the same name, theCREATE statement has no effect. Can't appear withOR REPLACE.
  • OPTIONS: If you have schema options, you can add them when you createthe property graph. These options are system-specific and follow theSpannerHINT syntax
  • property_graph_name: The name of the property graph. This name can be apath expression. This name must not conflict with the name of an existingtable, view, or property graph.
  • property_graph_content: Add the definitions for the nodes and edges in theproperty graph.
  • node_tables: A collection of node definitions. A node definition defines anew type of node in the graph.

    The following example represents three node definitions:Account,Customer, andGeoLocation.

    NODETABLES(Account,CustomerLABELClientPROPERTIES(cid,name),LocationASGeoLocationDEFAULTLABELPROPERTIESALLCOLUMNS)
  • edge_tables: A collection of edge definitions. An edge definition definesa new type of edge in the graph. An edge is directed and connects a source anda destination node.

    The following example represents two edge definitions:Own andTransfer.

    EDGETABLES(OwnSOURCEKEY(cid)REFERENCESCustomer(cid)DESTINATIONKEY(aid)REFERENCESAccountNOPROPERTIES,TransferSOURCEKEY(from_id)REFERENCESAccount(aid)DESTINATIONKEY(to_id)REFERENCESAccount(aid)LABELTransferNOPROPERTIES)
  • element_list: A list of element (node or edge) definitions.

  • element: Refer toElement definition for details.

Element definition

element:element_name  [ ASelement_alias ]element_keys  [ {label_and_properties_list |element_properties } ]  [dynamic_label ]  [dynamic_properties ]element_keys:  {node_element_key |edge_element_keys }node_element_key:  [element_key ]edge_element_keys:  [element_key ]source_keydestination_keyelement_key:  KEYcolumn_name_listsource_key:  SOURCE KEYedge_column_name_list  REFERENCESelement_alias_reference [node_column_name_list ]destination_key:  DESTINATION KEYedge_column_name_list  REFERENCESelement_alias_reference [node_column_name_list ]edge_column_name_list:column_name_listnode_column_name_list:column_name_listcolumn_name_list:  (column_name[, ...])

Description

Adds an element definition to the property graph. For example:

CustomerLABELClientPROPERTIES(cid,name)

In a graph, labels and properties are uniquely identified by their names. Labelsand properties with the same name can appear in multiple node or edgedefinitions. However, labels and properties with the same name must follow theserules:

  • Properties with the same name must have the same value type.
  • Labels with the same name must expose the same set of properties.

Definitions

  • element_name: The name of the input table from which elements are created.
  • element_alias: An optional alias. You must use an alias if you use an inputtable for more than one element definition.
  • element_keys: The key for a graph element. This uniquely identifies a graphelement.

    • By default, the element key is the primary key of the input table.

    • Element keys can be explicitly defined with theKEY clause.

    • Columns with uniqueness constraints can be used as element keys.

  • node_element_key: The element key for a node.

    KEY(item1_column,item2_column)
  • edge_element_keys: The element key, source key, and destination keyfor an edge.

    KEY(item1_column,item2_column)SOURCEKEY(item1_column)REFERENCESitem_node(item_node_column)DESTINATIONKEY(item2_column)REFERENCESitem_node(item_node_column)
  • element_key: An optional key that identifies the node or edge element. Ifelement_key isn't provided, then the primary key of the table is used.

    KEY(item1_column,item2_column)
  • source_key: The key for the source node of the edge.

    SOURCEKEY(item1_column)REFERENCESitem_node(item_node_column)
  • destination_key: The key for the destination node of the edge.

    DESTINATIONKEY(item2_column)REFERENCESitem_node(item_node_column)
  • column_name_list: One or more columns to assign to a key.

    Incolumn_name_list, column names must be unique.

  • Reference column name lists:

    • node_column_name_list: One or more columns referenced from the node tables.

    • edge_column_name_list: One or more columns referenced from the edge tables.

    Referenced columns must exist in the corresponding node or edge table.

    Ifnode_column_name_list doesn't exist insource_key ordestination_key, then theelement_keys of the referenced node are used.In this case, the column order in theelement_keys must match the columnorder in theedge_column_name_list.

  • element_alias_reference: The alias of another element to reference.

  • label_and_properties_list: The list of labels and properties to add toan element. For more information, seeLabel and properties list definition.

  • dynamic_label: The name of the column that holds dynamic label values. Formore information, see theDynamic label definition.

  • dynamic_properties: The name of the column that holds dynamic propertiesvalues. For more information see theDynamic properties definition.

Label and properties list definition

label_and_properties_list:label_and_properties[...]label_and_properties:element_label  [element_properties ]element_label:  {    LABELlabel_name |    DEFAULT LABEL  }

Description

Adds a list of labels and properties to an element.

Definitions

  • label_and_properties: The label to add to the element and the propertiesexposed by that label. For example:

    LABELTouristPROPERTIES(home_city,home_country)

    Whenlabel_and_properties isn't specified, the following isapplied implicitly:

    DEFAULTLABELPROPERTIESAREALLCOLUMNS

    A property must be unique inlabel_and_properties.

  • element_label: Add a custom label or use the default label for theelement.label_name must be unique inelement.

    If you useDEFAULT LABEL,label_name is the same aselement_table_alias.

  • element_properties: The properties associated with a label. A propertycan't be used more than once for a specific label. For more information, seeElement properties definition.

Element properties definition

element_properties:  {    NO PROPERTIES |properties_are |derived_property_list  }properties_are:  PROPERTIES [ ARE ] ALL COLUMNS [ EXCEPTcolumn_name_list ]column_name_list:  (column_name[, ...])derived_property_list:  PROPERTIES (derived_property[, ...])derived_property:value_expression [ ASproperty_name ]

Description

Adds properties associated with a label.

Definitions

  • NO PROPERTIES: The element doesn't have properties.
  • properties_are: Define which columns to include as elementproperties.

    If you don't include this definition, all columns are included bydefault, and the following definition is applied implicitly:

    PROPERTIESAREALLCOLUMNS

    In the following examples, all columns in a table are included aselement properties:

    PROPERTIESAREALLCOLUMNS
    PROPERTIESALLCOLUMNS

    In the following example, all columns in a table except forhome_city andhome_country are included as element properties:

    PROPERTIESAREALLCOLUMNSEXCEPT(home_city,home_country)
  • column_name_list: A list of columns to exclude as element properties.

    Column names in theEXCEPT column_name_list must be unique.

  • derived_property_list: A list of element property definitions.

  • derived_property: An expression that defines a property and can optionallyreference the input table columns.

    In the following example, theid andname columns are included asproperties. Additionally, the result of thesalary + bonus expression areincluded as theincome property:

    PROPERTIES(id,name,salary+bonusASincome)

    A derived property includes:

    • value_expression: An expression that can be represented by simple constructssuch as column references and functions. Subqueries are excluded.

    • AS property_name: Alias to assign to the value expression. This isoptional unlessvalue_expression is a function.

    Ifderived_property has any column reference invalue_expression, thatcolumn reference must refer to a column of the underlying table.

    Ifderived_property doesn't defineproperty_name,value_expressionmust be a column reference and the implicitproperty_name is thecolumn name.

Dynamic label definition

dynamic_label:  DYNAMIC LABEL (dynamic_label_column_name)

Description

Specifies a column that holds dynamic label values.

Definitions

  • dynamic_label_column_name: The name of the column that holds labelvalues. The column must use the STRING data type.

    • As a graph element is mapped from a row of an element table, an element'sdynamic label is the data that resides in thedynamic_label_column_name column.

    • There can be at most one node table and one edge table within a schemathat supports dynamic labels.

    • The dynamic label values must be stored in lower-case. When you access themin queries, they are case-insensitive.

    • Both defined labels and a dynamic label can be applied to an element.If the names of adefined label and dynamiclabel overlap, the defined label takes precedence over the dynamic one.

Dynamic properties definition

dynamic_properties:  DYNAMIC PROPERTIES (dynamic_properties_column_name)

Description

Specifies a column that holds dynamic properties values.

Definitions

  • dynamic_properties_column_name: The name of the column that holdsproperties values. The column must be of JSON type.

    • As a graph element is mapped from a row of an element table, an element'sdynamic properties are the data that resides in thedynamic_properties_column_name column.

    • Top-level JSON keys in thedynamic_properties_column_name column aremapped as dynamic properties.

    • The JSON key of each dynamic property must be stored in lower-case.When you access them in queries, they are case-insensitive.

    • Unlike dynamic labels, any number of nodes or edges within a schema cansupport dynamic properties.

    • Unlike theElement properties definition, dynamicproperties for an element are not exposed by a dynamic label and can evolveindependently.

    • If the names of a defined property and dynamic property overlap, the definedproperty takes precedence over the dynamic one.

FinGraph Examples

FinGraph with defined labels and defined properties

The following property graph,FinGraph, contains two nodedefinitions (Account andPerson) and two edge definitions(PersonOwnAccount andAccountTransferAccount).

Note: all GQL examples in the GQL reference use theFinGraph property graph example.To set up this property graph,seeSet up and query Spanner Graph.
CREATEORREPLACEPROPERTYGRAPHFinGraphNODETABLES(Account,Person)EDGETABLES(PersonOwnAccountSOURCEKEY(id)REFERENCESPerson(id)DESTINATIONKEY(account_id)REFERENCESAccount(id)LABELOwns,AccountTransferAccountSOURCEKEY(id)REFERENCESAccount(id)DESTINATIONKEY(to_id)REFERENCESAccount(id)LABELTransfers);

Once the property graph is created, you can use it inGQL queries. Forexample, the following query matches all nodes labeledPerson and then returnsthename values in the results.

GRAPHFinGraphMATCH(p:Person)RETURNp.name/*---------+ | name    | +---------+ | Alex    | | Dana    | | Lee     | +---------*/

FinGraph with dynamic label and dynamic properties

The following property graph,FinGraph, contains a unified node and unifiededge definition with dynamic label and dynamic properties to store all nodes andedges.

CREATEPROPERTYGRAPHFinGraphNODETABLES(GraphNodeDYNAMICLABEL(label)DYNAMICPROPERTIES(properties))EDGETABLES(GraphEdgeSOURCEKEY(id)REFERENCESGraphNode(id)DESTINATIONKEY(dest_id)REFERENCESGraphNode(id)DYNAMICLABEL(label)DYNAMICPROPERTIES(properties));

Compared to the previous example, to addAccount andPerson nodes in adynamic label model, insert entries intoGraphNode with the label asAccountorPerson to indicate which node type that entry specifies. Dynamic propertiesmust be added as JSON.

INSERTINTOGraphNode(id,label,properties)VALUES(1,"person",JSON'{"name": "Alex", "age": 33}');

Similarly, inserting entries toGraphEdge with values likePersonOwnAccountandAccountTransferAccount for thelabel column creates edges.

DROP PROPERTY GRAPH statement

DROP PROPERTY GRAPH [ IF EXISTS ]property_graph_name;

Description

Deletes a property graph.

Definitions

  • IF EXISTS: If a property graph of the specified name doesn't exist, then theDROP statement has no effect and no error is generated.
  • property_graph_name: The name of the property graph to drop.

Example

DROPPROPERTYGRAPHFinGraph;

INFORMATION SCHEMA

Use the SQLINFORMATION_SCHEMA to look upschemas created by theCREATE PROPERTY GRAPH statement.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.