使用 DML 刪除圖表資料
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 DML 刪除 Spanner Graph 資料。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
C++
如要瞭解如何安裝及使用 Spanner 的用戶端程式庫,請參閱這篇文章。
如要向 Spanner 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Go
如要瞭解如何安裝及使用 Spanner 的用戶端程式庫,請參閱這篇文章。
如要向 Spanner 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Spanner 的用戶端程式庫,請參閱這篇文章。
如要向 Spanner 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要瞭解如何安裝及使用 Spanner 的用戶端程式庫,請參閱這篇文章。
如要向 Spanner 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Delete graph data with DML\n\nDelete Spanner Graph data using DML.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Insert, update, or delete Spanner Graph data](/spanner/docs/graph/insert-update-delete-data)\n\nCode sample\n-----------\n\n### C++\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n void DeleteDataWithDml(google::cloud::spanner::Client client) {\n using ::google::cloud::StatusOr;\n namespace spanner = ::google::cloud::spanner;\n\n auto commit_result = client.Commit([&client](spanner::Transaction txn)\n -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto deleted = client.ExecuteDml(\n std::move(txn),\n spanner::SqlStatement(\n \"DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2\"));\n if (!deleted) return std::move(deleted).status();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n\n commit_result = client.Commit(\n [&client](spanner::Transaction txn) -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto deleted = client.ExecuteDml(\n std::move(txn),\n spanner::SqlStatement(\"DELETE FROM Account WHERE id = 2\"));\n if (!deleted) return std::move(deleted).status();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n\n std::cout \u003c\u003c \"Delete was successful [spanner_delete_graph_data_with_dml]\\n\";\n }\n\n### Go\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/spanner\"\n )\n\n func deleteGraphDataWithDml(w io.Writer, db string) error {\n \tctx := context.Background()\n \tclient, err := spanner.NewClient(ctx, db)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer client.Close()\n\n \t// Execute a ReadWriteTransaction to update the 'AccountTransferAccount'\n \t// table underpinning 'AccountTransferAccount' edges in 'FinGraph'. The\n \t// function run by ReadWriteTransaction executes an 'DELETE' SQL DML\n \t// statement. This has the effect of deleting the 'AccountTransferAccount'\n \t// edge where the source 'id' is 1 and the destination 'id' is 2 from the graph.\n \t_, err1 := client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {\n \t\tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{SQL: `DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2`}\n \t\trowCount, err := txn.Update(ctx, stmt)\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%d AccountTransferAccount record(s) deleted.\\n\", rowCount)\n \t\treturn nil\n \t})\n\n \tif err1 != nil {\n \t\treturn err1\n \t}\n\n \t// Execute a ReadWriteTransaction to update the 'Account' table underpinning\n \t//'Account' nodes in 'FinGraph'. In 'FinGraph', nodes can only be deleted\n \t// after any edges referencing the nodes have been deleted first. The function\n \t// run by ReadWriteTransaction executes an 'DELETE' SQL DML statement. This has\n \t// the effect of deleting the 'Account' node whose 'id' is 1 from the graph.\n \t_, err2 := client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {\n \t\tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{SQL: `DELETE FROM Account WHERE id = 2`}\n \t\trowCount, err := txn.Update(ctx, stmt)\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"%d Account record(s) deleted.\\n\", rowCount)\n \t\treturn nil\n \t})\n\n \treturn err2\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n static void deleteUsingDml(DatabaseClient dbClient) {\n dbClient\n .readWriteTransaction()\n .run(\n transaction -\u003e {\n String sql = \"DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2\";\n long rowCount = transaction.executeUpdate(Statement.of(sql));\n System.out.printf(\"%d AccountTransferAccount record(s) deleted.\\n\", rowCount);\n return null;\n });\n\n dbClient\n .readWriteTransaction()\n .run(\n transaction -\u003e {\n String sql = \"DELETE FROM Account WHERE id = 2\";\n long rowCount = transaction.executeUpdate(Statement.of(sql));\n System.out.printf(\"%d Account record(s) deleted.\\n\", rowCount);\n return null;\n });\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def delete_data_with_dml(instance_id, database_id):\n \"\"\"Deletes sample data from the database using a DML statement.\"\"\"\n\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n def delete_transfers(transaction):\n row_ct = transaction.execute_update(\n \"DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2\"\n )\n\n print(\"{} AccountTransferAccount record(s) deleted.\".format(row_ct))\n\n def delete_accounts(transaction):\n row_ct = transaction.execute_update(\"DELETE FROM Account WHERE id = 2\")\n\n print(\"{} Account record(s) deleted.\".format(row_ct))\n\n database.run_in_transaction(delete_transfers)\n database.run_in_transaction(delete_accounts)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=spanner)."]]