Criar, atualizar ou excluir um esquema do Spanner Graph
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Este documento mostra como criar, atualizar ou excluir um gráfico de propriedades
no Spanner Graph usando o esquema de exemplo que você criou em
Configurar e consultar o Spanner Graph.
Com o Spanner, é possível fazer atualizações de esquema sem tempo de inatividade. É possível atualizar
o esquema de um banco de dados atual das seguintes maneiras:
Console do Google Cloud
Envie um comando na página do Spanner Studio.
Para acessar a página Spanner Studio, clique em Spanner Studio na página de visão geral do banco de dados ou da tabela. Para mais informações sobre como acessar o Spanner
Studio, consulte
Gerenciar seus dados usando o Google Cloud console.
Para criar um esquema de gráfico de propriedade, siga estas etapas:
Crie as tabelas de entrada de nó Person e Account. Essas tabelas são usadas
como tabelas de entrada para as definições de nó no exemplo de gráfico de propriedade.
Crie as tabelas de entrada de borda PersonOwnAccount e
AccountTransferAccount. Essas tabelas são usadas como tabelas de entrada para as
definições de aresta no exemplo de gráfico de propriedade.
É possível atualizar um esquema de gráfico de propriedades das seguintes maneiras:
Adicione novas definições de nó ou aresta.
Atualize as definições de nó ou aresta.
Remova as definições de nó ou aresta atuais.
Em cada caso, é necessário recriar o gráfico de propriedades com o esquema atualizado.
Adicionar novas definições de nó ou aresta
Para adicionar um novo nó e uma nova definição de borda, siga estas etapas:
Adicionar novas tabelas de entrada.
Defina as tabelas de entrada associadas
às novas definições de elementos do gráfico. Confira o exemplo a seguir, que
adiciona duas novas tabelas de entrada Company e PersonInvestCompany:
Atualize o gráfico com CREATE OR REPLACE PROPERTY GRAPH. O
exemplo a seguir atualiza o esquema FinGraph adicionando uma nova definição de nó
Company e uma nova definição de aresta PersonInvestCompany:
Atualize o esquema do gráfico de propriedades com CREATE OR REPLACE PROPERTY GRAPH.
O exemplo a seguir adiciona uma nova propriedade mailing_address à
definição do nó Person usando a instrução
CREATE OR REPLACE PROPERTY GRAPH. Neste exemplo, a definição do nó Person
capta automaticamente a definição da tabela Person alterada
porque o esquema da tabela de entrada mudou.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 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)."]]