Create and manage a Spanner Graph schema

Note: This feature is available with the Spanner Enterprise edition and Enterprise Plus edition. For more information, see theSpanner editions overview.

This document provides a guide on managing Spanner Graph schemas, detailingthe processes forcreating,updating, anddropping schemas using DDL statements.

For more information about property graph schemas, see theSpanner Graph schema overview. If youencounter errors when you create a property graph schema, seeTroubleshoot Spanner Graph.

Create a property graph schema

You can create a property graph schema using views or tables. To create aproperty graph schema using tables, do the following:

  1. Create the node input tables.
  2. Create the edge input tables.
  3. Define the property graph.

To learn how to use SQL views to create a property graph schema, seeCreate a Spanner Graph from a SQL view.

When you create a property graph schema, be sure to consider thebest practices. Thefollowing sections show how to use tables to create an example property graphschema:

Create node input tables

The following creates two node input tables,Person andAccount, which serveas input for the node definitions in the example property graph:

CREATETABLEPerson(idINT64NOTNULL,nameSTRING(MAX),birthdayTIMESTAMP,countrySTRING(MAX),citySTRING(MAX),)PRIMARYKEY(id);CREATETABLEAccount(idINT64NOTNULL,create_timeTIMESTAMP,is_blockedBOOL,nick_nameSTRING(MAX),)PRIMARYKEY(id);

Create edge input tables

The following code creates two edge input tables,PersonOwnAccount andAccountTransferAccount, as input for the edge definitions in the exampleproperty graph:

CREATETABLEPersonOwnAccount(idINT64NOTNULL,account_idINT64NOTNULL,create_timeTIMESTAMP,FOREIGNKEY(account_id)REFERENCESAccount(id))PRIMARYKEY(id,account_id),INTERLEAVEINPARENTPersonONDELETECASCADE;CREATETABLEAccountTransferAccount(idINT64NOTNULL,to_idINT64NOTNULL,amountFLOAT64,create_timeTIMESTAMPNOTNULL,order_numberSTRING(MAX),FOREIGNKEY(to_id)REFERENCESAccount(id))PRIMARYKEY(id,to_id,create_time),INTERLEAVEINPARENTAccountONDELETECASCADE;

Define a property graph

The following code uses tables and theCREATE PROPERTY GRAPH statement todefine a property graph. This statement defines a property graph namedFinGraph withAccount andPerson nodes, andPersonOwnAccount andAccountTransferAccount edges:

CREATEPROPERTYGRAPHFinGraphNODETABLES(Account,Person)EDGETABLES(PersonOwnAccountSOURCEKEY(id)REFERENCESPerson(id)DESTINATIONKEY(account_id)REFERENCESAccount(id)LABELOwns,AccountTransferAccountSOURCEKEY(id)REFERENCESAccount(id)DESTINATIONKEY(to_id)REFERENCESAccount(id)LABELTransfers);

Update a property graph schema

After you create a property graph schema, you update it by using theCREATE ORREPLACE PROPERTY GRAPH statement. This statement applies the changes byrecreating the graph schema with the desired update.

You can make the following changes to a property graph schema:

  • Add a node or edge definition: Create the newinput tables for the nodes and edges, and then use theCREATE OR REPLACEPROPERTY GRAPH statement to add the new definitions to the graph.

  • Update a node or edge definition: Update theunderlying input table with new node and edge definitions. Then, use theCREATE OR REPLACE PROPERTY GRAPH statement to update the definitions inthe graph.

  • Remove a node or edge definition: UsetheCREATE OR REPLACE PROPERTY GRAPH statement and omit the definitionsthat you want to remove from the graph.

Add new node or edge definitions

To add a new node and a new edge definition, follow these steps:

  1. Add a new node definition input table,Company, and a new edge definitioninput table,PersonInvestCompany.

    CREATETABLECompany(idINT64NOTNULL,nameSTRING(MAX))PRIMARYKEY(id);CREATETABLEPersonInvestCompany(idINT64NOTNULL,company_idINT64NOTNULL,FOREIGNKEY(company_id)REFERENCESCompany(id))PRIMARYKEY(id,company_id),INTERLEAVEINPARENTPersonONDELETECASCADE;
  2. Update theFinGraph schema by adding the newCompany node definition andthe newPersonInvestCompany edge definition.

    CREATEORREPLACEPROPERTYGRAPHFinGraphNODETABLES(Person,Account,Company)EDGETABLES(AccountTransferAccountSOURCEKEY(id)REFERENCESAccountDESTINATIONKEY(to_id)REFERENCESAccountLABELTransfers,PersonOwnAccountSOURCEKEY(id)REFERENCESPersonDESTINATIONKEY(account_id)REFERENCESAccountLABELOwns,PersonInvestCompanySOURCEKEY(id)REFERENCESPersonDESTINATIONKEY(company_id)REFERENCESCompanyLABELInvests);

Update node or edge definitions

To update an existing node or edge definition, you first alter the underlyinginput table, and then use theCREATE OR REPLACE PROPERTY GRAPH statement toapply the schema changes to the graph. To customize theproperties exposed from the input tables, use thePROPERTIES clause.For more information, seeCustomize labels and properties.

The following steps show how to update the underlying table of a schema, thenapply the update to the schema.

  1. Add themailing_address column to thePerson underlying input table.

    ALTERTABLEPersonADDCOLUMNmailing_addressSTRING(MAX);
  2. Apply the changes to thePerson table to the schema. Use theCREATE ORREPLACE PROPERTY GRAPH statement. ThePerson nodedefinition reflects the updatedPerson table definition because the inputtable schema changed.

    CREATEORREPLACEPROPERTYGRAPHFinGraphNODETABLES(Person,Account)EDGETABLES(AccountTransferAccountSOURCEKEY(id)REFERENCESAccountDESTINATIONKEY(to_id)REFERENCESAccountLABELTransfers,PersonOwnAccountSOURCEKEY(id)REFERENCESPersonDESTINATIONKEY(account_id)REFERENCESAccountLABELOwns);

Remove node or edge definitions

To remove existing node or edge definitions, recreate the property graph withoutthose node or edge tables.

The following removes thePerson node definition and thePersonOwnAccountedge definition by omitting them in theCREATE OR REPLACE PROPERTY GRAPHstatement.

CREATEORREPLACEPROPERTYGRAPHFinGraphNODETABLES(Account)EDGETABLES(AccountTransferAccountSOURCEKEY(id)REFERENCESAccountDESTINATIONKEY(to_id)REFERENCESAccountLABELTransfers);

Drop a property graph schema

To drop a graph schema from the underlying input tables, use theDROP PROPERTYGRAPH DDL statement. You can't delete the data from the underlying table whenyou drop a schema.

The following code drops theFinGraph property graph schema:

DROPPROPERTYGRAPHFinGraph;

What's next

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 2026-02-19 UTC.