Membuat, memperbarui, atau menghapus skema Spanner Graph
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menunjukkan cara membuat, memperbarui, atau menghapus grafik properti di Spanner Graph menggunakan contoh skema yang Anda buat di Menyiapkan dan membuat kueri Spanner Graph.
Spanner memungkinkan Anda membuat pembaruan skema tanpa periode nonaktif. Anda dapat memperbarui skema database yang ada dengan salah satu cara berikut:
Google Cloud console
Kirim perintah di halaman Spanner Studio.
Untuk mengakses halaman Spanner Studio, klik
Spanner Studio dari halaman Ringkasan database atau Ringkasan
tabel. Untuk mengetahui informasi selengkapnya tentang cara mengakses Spanner Studio, lihat Mengelola data menggunakan konsol Google Cloud .
Buat tabel input tepi PersonOwnAccount dan
AccountTransferAccount. Tabel ini digunakan sebagai tabel input untuk
definisi tepi dalam contoh grafik properti.
Anda dapat memperbarui skema grafik properti dengan cara berikut:
Tambahkan definisi node atau edge baru.
Memperbarui definisi node atau edge yang ada.
Menghapus definisi node atau edge yang ada.
Dalam setiap kasus, Anda harus membuat ulang grafik properti dengan skema yang diperbarui.
Menambahkan definisi node atau edge baru
Untuk menambahkan node baru dan definisi tepi baru, ikuti langkah-langkah berikut:
Tambahkan tabel input baru.
Tentukan tabel input yang terkait
dengan definisi elemen grafik baru. Lihat contoh berikut, yang
menambahkan dua tabel input baru Company dan PersonInvestCompany:
Perbarui grafik dengan CREATE OR REPLACE PROPERTY GRAPH. Contoh
berikut memperbarui skema FinGraph dengan menambahkan definisi node
baru Company dan definisi tepi baru PersonInvestCompany:
Perbarui skema grafik properti dengan CREATE OR REPLACE PROPERTY GRAPH.
Contoh berikut menambahkan properti baru mailing_address ke
definisi node Person menggunakan pernyataan
CREATE OR REPLACE PROPERTY GRAPH. Dalam contoh ini, definisi node Person secara otomatis mengambil definisi tabel Person yang diubah karena skema tabel input berubah.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]