Spanner-Graphschema erstellen, aktualisieren oder löschen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Dokument erfahren Sie, wie Sie mit dem Beispielschema, das Sie unter Cloud Spanner Graph einrichten und abfragen erstellt haben, eine Property-Graph in Spanner Graph erstellen, aktualisieren oder löschen.
Mit Spanner lassen sich Schemas ohne Ausfallzeit aktualisieren. Das Schema einer vorhandenen Datenbank kann auf folgende Arten aktualisiert werden:
Die Google Cloud Console
Reichen Sie einen Befehl auf der Seite Spanner Studio ein.
Klicken Sie auf der Seite „Datenbankübersicht“ oder „Tabellenübersicht“ auf Spanner Studio, um die Seite Spanner Studio aufzurufen. Weitere Informationen zum Zugriff auf Spanner Studio finden Sie unter Daten über die Console verwalten Google Cloud .
Erstellen Sie die Knoteneingabetabellen Person und Account. Diese Tabellen werden als Eingabetabellen für die Knotendefinitionen im Beispiel-Property-Graphen verwendet.
Erstellen Sie die Edge-Eingabetabellen PersonOwnAccount und AccountTransferAccount. Diese Tabellen werden als Eingabetabellen für die Kantendefinitionen im Beispiel-Property-Graphen verwendet.
Definieren Sie die Property-Graph mithilfe der Beispielanweisung für CREATE PROPERTY GRAPH.
Im folgenden Beispiel wird eine Property-Graph mit dem Namen FinGraph mit den Knoten Account und Person sowie den Kanten PersonOwnAccount und AccountTransferAccount definiert.
Sie haben folgende Möglichkeiten, ein Property-Graph-Schema zu aktualisieren:
Fügen Sie neue Knoten- oder Kantendefinitionen hinzu.
Vorhandene Knoten- oder Kantendefinitionen aktualisieren
Entfernen Sie vorhandene Knoten- oder Kantendefinitionen.
In jedem Fall müssen Sie die Property-Graphen mit dem aktualisierten Schema neu erstellen.
Neue Knoten- oder Kantendefinitionen hinzufügen
So fügen Sie einen neuen Knoten und eine neue Kantendefinition hinzu:
Fügen Sie neue Eingabetabellen hinzu.
Definieren Sie die Eingabetabellen, die mit den neuen Graphelementdefinitionen verknüpft sind. Im folgenden Beispiel werden zwei neue Eingabetabellen Company und PersonInvestCompany hinzugefügt:
Aktualisieren Sie die Grafik mit CREATE OR REPLACE PROPERTY GRAPH. Im folgenden Beispiel wird das FinGraph-Schema aktualisiert, indem eine neue Knotendefinition Company und eine neue Kantendefinition PersonInvestCompany hinzugefügt werden:
Aktualisieren Sie das Property-Graph-Schema mit CREATE OR REPLACE PROPERTY GRAPH.
Im folgenden Beispiel wird der Knotendefinition Person mithilfe der Anweisung CREATE OR REPLACE PROPERTY GRAPH ein neues Attribut mailing_address hinzugefügt. In diesem Beispiel wird die geänderte Person-Tabellendefinition automatisch von der Person-Knotendefinition übernommen, da sich das Schema der Eingabetabelle geändert hat.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-11 (UTC)."],[],[],null,["# Manage a Spanner Graph schema\n\n| **Note:** This feature is available with the Spanner Enterprise edition and Enterprise Plus edition. For more information, see the [Spanner editions overview](/spanner/docs/editions-overview).\n\n\u003cbr /\u003e\n\nThis document provides a comprehensive guide on managing Spanner Graph\nproperty schemas, detailing the processes for\n[creating](#create-property-graph-schema),\n[updating](#update-property-graph-schema), and\n[dropping](#drop-property-graph-schema) schemas using DDL statements.\n\nFor more information about property graph schemas, see the\n[Spanner Graph schema overview](/spanner/docs/graph/schema-overview). If you\nencounter errors when you create a property graph schema, see\n[Troubleshoot Spanner Graph](/spanner/docs/graph/troubleshoot).\n\nCreate a property graph schema\n------------------------------\n\nTo create a property graph schema, do the following:\n\n1. [Create the node input tables](#create-node-input-tables).\n2. [Create the edge input tables](#create-edge-input-tables).\n3. [Define the property graph](#define-property-graph).\n\nWhen you create a property graph schema, be sure to consider the\n[best practices](/spanner/docs/graph/best-practices-designing-schema).\n\nThe following sections show how to create an example property graph schema:\n\n### Create node input tables\n\nThe following creates two node input tables, `Person` and `Account`, which serve\nas input for the node definitions in the example property graph: \n\n CREATE TABLE Person (\n id INT64 NOT NULL,\n name STRING(MAX),\n birthday TIMESTAMP,\n country STRING(MAX),\n city STRING(MAX),\n ) PRIMARY KEY (id);\n\n CREATE TABLE Account (\n id INT64 NOT NULL,\n create_time TIMESTAMP,\n is_blocked BOOL,\n nick_name STRING(MAX),\n ) PRIMARY KEY (id);\n\n### Create edge input tables\n\nThe following code creates two edge input tables, `PersonOwnAccount` and\n`AccountTransferAccount`, as input for the edge definitions in the example\nproperty graph: \n\n CREATE TABLE PersonOwnAccount (\n id INT64 NOT NULL,\n account_id INT64 NOT NULL,\n create_time TIMESTAMP,\n FOREIGN KEY (account_id) REFERENCES Account (id)\n ) PRIMARY KEY (id, account_id),\n INTERLEAVE IN PARENT Person ON DELETE CASCADE;\n\n CREATE TABLE AccountTransferAccount (\n id INT64 NOT NULL,\n to_id INT64 NOT NULL,\n amount FLOAT64,\n create_time TIMESTAMP NOT NULL,\n order_number STRING(MAX),\n FOREIGN KEY (to_id) REFERENCES Account (id)\n ) PRIMARY KEY (id, to_id, create_time),\n INTERLEAVE IN PARENT Account ON DELETE CASCADE;\n\n### Define a property graph\n\nThe following code defines the property graph using the `CREATE PROPERTY GRAPH`\nstatement. This statement defines a property graph named `FinGraph` with\n`Account` and `Person` nodes, and `PersonOwnAccount` and\n`AccountTransferAccount` edges: \n\n CREATE PROPERTY GRAPH FinGraph\n NODE TABLES (\n Account,\n Person\n )\n EDGE TABLES (\n PersonOwnAccount\n SOURCE KEY (id) REFERENCES Person (id)\n DESTINATION KEY (account_id) REFERENCES Account (id)\n LABEL Owns,\n AccountTransferAccount\n SOURCE KEY (id) REFERENCES Account (id)\n DESTINATION KEY (to_id) REFERENCES Account (id)\n LABEL Transfers\n );\n\nUpdate a property graph schema\n------------------------------\n\nAfter you create a property graph schema, you update it by using the `CREATE OR\nREPLACE PROPERTY GRAPH` statement. This statement applies the changes by\nrecreating the graph schema with the desired update.\n\nYou can make the following changes to a property graph schema:\n\n- [Add a node or edge definition](#add-new-node-or-edge): Create the new\n input tables for the nodes and edges, and then use the `CREATE OR REPLACE\n PROPERTY GRAPH` statement to add the new definitions to the graph.\n\n- [Update a node or edge definition](#update-node-or-edge): Update the\n underlying input table with new node and edge definitions. Then, use the\n `CREATE OR REPLACE PROPERTY GRAPH` statement to update the definitions in\n the graph.\n\n- [Remove a node or edge definition](#remove-node-or-edge): Use\n the `CREATE OR REPLACE PROPERTY GRAPH` statement and omit the definitions\n that you want to remove from the graph.\n\n### Add new node or edge definitions\n\nTo add a new node and a new edge definition, follow these steps:\n\n1. Add a new node definition input table, `Company`, and a new edge definition\n input table, `PersonInvestCompany`.\n\n CREATE TABLE Company (\n id INT64 NOT NULL,\n name STRING(MAX)\n ) PRIMARY KEY (id);\n\n CREATE TABLE PersonInvestCompany (\n id INT64 NOT NULL,\n company_id INT64 NOT NULL,\n FOREIGN KEY (company_id) REFERENCES Company (id)\n ) PRIMARY KEY (id, company_id),\n INTERLEAVE IN PARENT Person ON DELETE CASCADE;\n\n2. Update the `FinGraph` schema by adding the new `Company` node definition and\n the new `PersonInvestCompany` edge definition.\n\n CREATE OR REPLACE PROPERTY GRAPH FinGraph\n NODE TABLES (\n Person,\n Account,\n Company\n )\n EDGE TABLES (\n AccountTransferAccount\n SOURCE KEY (id) REFERENCES Account\n DESTINATION KEY (to_id) REFERENCES Account\n LABEL Transfers,\n PersonOwnAccount\n SOURCE KEY (id) REFERENCES Person\n DESTINATION KEY (account_id) REFERENCES Account\n LABEL Owns,\n PersonInvestCompany\n SOURCE KEY (id) REFERENCES Person\n DESTINATION KEY (company_id) REFERENCES Company\n LABEL Invests\n );\n\n### Update node or edge definitions\n\nTo update an existing node or edge definition, you first alter the underlying\ninput table, and then use the `CREATE OR REPLACE PROPERTY GRAPH` statement to\napply the schema changes to the graph. To customize the\nproperties exposed from the input tables, use the\n[`PROPERTIES clause`](/spanner/docs/reference/standard-sql/graph-schema-statements#element_table_property_definition).\nFor more information, see\n[Customize labels and properties](/spanner/docs/graph/schema-overview#customize-labels-properties).\n\nThe following steps show how to update the underlying table of a schema, then\napply the update to the schema.\n\n1. Add the `mailing_address` column to the `Person` underlying input table.\n\n ALTER TABLE Person\n ADD COLUMN mailing_address STRING(MAX);\n\n2. Apply the changes to the `Person` table to the schema. Use the `CREATE OR\n REPLACE PROPERTY GRAPH` statement. The `Person` node\n definition reflects the updated `Person` table definition because the input\n table schema changed.\n\n CREATE OR REPLACE PROPERTY GRAPH FinGraph\n NODE TABLES (\n Person,\n Account\n )\n EDGE TABLES (\n AccountTransferAccount\n SOURCE KEY (id) REFERENCES Account\n DESTINATION KEY (to_id) REFERENCES Account\n LABEL Transfers,\n PersonOwnAccount\n SOURCE KEY (id) REFERENCES Person\n DESTINATION KEY (account_id) REFERENCES Account\n LABEL Owns\n );\n\n### Remove node or edge definitions\n\nTo remove existing node or edge definitions, recreate the property graph without\nthose node or edge tables.\n\nThe following removes the `Person` node definition and the `PersonOwnAccount`\nedge definition by omitting them in the `CREATE OR REPLACE PROPERTY GRAPH`\nstatement. \n\n CREATE OR REPLACE PROPERTY GRAPH FinGraph\n NODE TABLES (\n Account\n )\n EDGE TABLES (\n AccountTransferAccount\n SOURCE KEY (id) REFERENCES Account\n DESTINATION KEY (to_id) REFERENCES Account\n LABEL Transfers\n );\n\nDrop a property graph schema\n----------------------------\n\nTo drop a graph schema from the underlying input tables, use the `DROP PROPERTY\nGRAPH` DDL statement. You can't delete the data from the underlying table when\nyou drop a schema.\n\nThe following code drops the `FinGraph` property graph schema: \n\n DROP PROPERTY GRAPH FinGraph;\n\nWhat's next\n-----------\n\n- [Manage Spanner Graph data](/spanner/docs/graph/insert-update-delete-data).\n- [Learn about Spanner Graph queries](/spanner/docs/graph/queries-overview).\n- [Learn best practices for tuning Spanner Graph queries](/spanner/docs/graph/best-practices-tuning-queries)."]]