Dieses Dokument richtet sich an Datenbankadministratoren und Data Engineers, die mit Spanner, SQL und Datenanalyse vertraut sind. Wenn Sie noch nicht mit Spanner vertraut sind, lesen Sie den Schnelleinstieg. Google Cloud
Klicken Sie im Navigationsmenü auf Spanner Studio.
Klicken Sie in der Taskleiste auf pen_sparkGemini, um Gemini-Funktionen in Spanner aufzurufen.
Wählen Sie die Gemini-Funktionen aus, die Sie aktivieren möchten, z. B. Generierung von Kommentaren zu Abfragen.
Sie können Funktionen auswählen und ausprobieren, ohne dass sich dies auf andere auswirkt, die an Ihrem Projekt arbeiten.
Optional: Wenn Sie die Beispiele in diesem Dokument nachvollziehen möchten, erstellen Sie zuerst die Tabelle Singers, wie unter Schema für Ihre Datenbank erstellen beschrieben.
Wenn Sie Gemini-Funktionen in Spanner deaktivieren möchten, wiederholen Sie diese Schritte und heben Sie dann die Auswahl der Gemini-Funktionen auf, die Sie deaktivieren möchten.
Erforderliche Rollen
Bitten Sie Ihren Administrator, Ihnen die IAM-Rolle Gemini für Google Cloud User (roles/cloudaicompanion.user) für das Projekt zuzuweisen, um die Berechtigungen zu erhalten, die Sie zum Ausführen der Aufgaben in diesem Dokument benötigen.
Weitere Informationen zum Zuweisen von Rollen finden Sie unter Zugriff auf Projekte, Ordner und Organisationen verwalten.
SQL-Abfragen mit Prompts in natürlicher Sprache generieren
Sie können Gemini Kommentare in natürlicher Sprache (oder Prompts) übergeben, um Abfragen zu generieren, die auf Ihrem Schema basieren. Beispielsweise können Sie Gemini auffordern, als Reaktion auf die folgenden Prompts SQL zu generieren:
"Erstellen Sie eine Tabelle, in der die Ergebnisse einer Umfrage zur Kundenzufriedenheit erfasst werden."
"Fügen Sie der Tabelle "Sänger" eine Datumsspalte mit dem Namen "Geburtsdatum" hinzu."
„Wie viele Sänger wurden in den 1990ern geboren?“
So generieren Sie SQL in Spanner mit Gemini-Unterstützung:
Rufen Sie in der Google Cloud Console die Seite Spanner auf.
Klicken Sie im Navigationsbereich auf Spanner Studio. Im Bereich Explorer wird eine Liste der Objekte in Ihrer Datenbank angezeigt.
Klicken Sie zum Abfragen der Datenbank auf addNeuer SQL-Editor-Tab. Prüfen Sie, ob die SQL-Generierung aktiviert ist.
Um SQL zu generieren, geben Sie im Abfrageeditor einen Kommentar ein, der mit -- beginnt, gefolgt von einem einzeiligen Kommentar, und drücken Sie dann Return.
Wenn Sie beispielsweise den Prompt -- add a row to table singers eingeben und Return drücken, generiert Gemini SQL-Code, der in etwa so aussieht:
Wenn Sie das Beispiel mit der Tabelle Singers fortsetzen und den Prompt -- show all singers born in the 70s eingeben, generiert Gemini SQL-Code, der in etwa so aussieht:
Prüfen Sie den generierten SQL-Code und führen Sie eine der folgenden Aktionen aus:
Wenn Sie den von Gemini generierten SQL-Code akzeptieren möchten, drücken Sie Tab und klicken Sie dann auf Ausführen, um den vorgeschlagenen SQL-Code auszuführen.
Wenn Sie den von Gemini generierten SQL-Code bearbeiten möchten, drücken Sie Tab, bearbeiten Sie den SQL-Code und klicken Sie dann auf Ausführen.
Um einen Vorschlag zu verwerfen, drücken Sie Esc oder fahren Sie mit der Eingabe fort.
SQL-Anweisungen im Abfrageeditor erklären
Sie können Gemini in Spanner verwenden, um SQL-Abfragen in natürlicher Sprache zu erklären. Diese Erklärung kann Ihnen helfen,
die Syntax,
das zugrunde liegende Schema und den Geschäftskontext
für komplexe oder lange Abfragen zu verstehen.
Rufen Sie in der Google Cloud Console die Seite Spanner auf.
[[["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-09-05 (UTC)."],[],[],null,["# Write SQL with Gemini assistance\n\n| **Preview\n| --- [Gemini](/gemini/docs/overview) in Spanner**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis document describes how you can use\n[Gemini Code Assist](/gemini/docs/databases/overview) to get AI-powered\nassistance with the following in Spanner:\n\n- [Generate SQL queries using natural language prompts.](#generate-sql-queries-using-natural-language-prompts)\n- [Explain SQL queries in the query editor.](#explain-sql-in-query-editor)\n\nLearn [how and when Gemini\nfor Google Cloud uses your data](/gemini/docs/discover/data-governance).\n\nThis document is intended for database administrators and data engineers who\nare familiar with Spanner, SQL, and data analysis. If you're new\nto Spanner, see\n[Create and query a database by using the Google Cloud console](/spanner/docs/create-query-database-console).\n| **Note** : Coding assistance is part of Gemini Code Assist and is available at no charge until it's included in Gemini Code Assist Standard edition. This change will be communicated at a later date. At that time, you will need to acquire a [Gemini Code Assist Standard edition license](/gemini/docs/discover/set-up-gemini#purchase-subscription) to continue to use coding assistance in Spanner Studio.\n\nBefore you begin\n----------------\n\n1. Optional: [Set up Gemini Code Assist](/gemini/docs/discover/set-up-gemini).\n\n2. To complete the tasks in this document, ensure that you have the\n [necessary Identity and Access Management (IAM) permissions](#required-roles).\n\n3. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n4. Select an instance from the list.\n\n5. Select a database.\n\n6. In the navigation menu, click **Spanner Studio**.\n\n7.\n In the taskbar, click pen_spark\n **Gemini** to view Gemini features in\n Spanner.\n\n8. Select the Gemini features that you want to enable---for\n example,\n [**Comment-to-query generation**](#generate-sql-queries-using-natural-language-prompts).\n You can select and try features for yourself without affecting others\n working in your project.\n\n9. Optional: If you want to follow along with the examples in this document,\n first create the `Singers` table as described in\n [Create a schema for your database](/spanner/docs/create-query-database-console#create-schema).\n\nTo disable Gemini features in Spanner, repeat\nthese steps, and then deselect the Gemini features that you want\nto disable.\n\n### Required roles\n\n\nTo get the permissions that\nyou need to complete the tasks in this document,\n\nask your administrator to grant you the\n\n\n[Gemini for Google Cloud User](/iam/docs/roles-permissions/cloudaicompanion#cloudaicompanion.user) (`roles/cloudaicompanion.user`)\nIAM role on the project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nGenerate SQL queries using natural language prompts\n---------------------------------------------------\n\n| As an early-stage technology, Gemini for Google Cloud\n| products can generate output that seems plausible but is factually incorrect. We recommend that you\n| validate all output from Gemini for Google Cloud products before you use it.\n| For more information, see\n| [Gemini for Google Cloud and responsible AI](/gemini/docs/discover/responsible-ai).\n\nYou can give Gemini natural language comments (or *prompts*) to\ngenerate queries that are based on your schema. For example, you can prompt\nGemini to generate SQL in response to the following prompts:\n\n- \"Create a table that tracks customer satisfaction survey results.\"\n- \"Add a date column called birthday to the Singers table.\"\n- \"How many singers were born in the 90s?\"\n\n| **Note:** When you enter a prompt for Gemini in Spanner, information about your database's schema is included with the prompt. This can include table and column names, data types, and column descriptions. Your database schema and data remain in Spanner and aren't sent to Gemini.\n\nTo generate SQL in Spanner with Gemini\nassistance, follow these steps:\n\n1. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n2. Select an instance from the list.\n\n3. Select a database.\n\n4. In the navigation pane, click **Spanner Studio** . The **Explorer**\n pane displays a list of objects in your database.\n\n5. To query your database, click the add\n **New SQL editor tab** . Make sure that\n [SQL generation is enabled](#before-you-begin).\n\n6. To generate SQL, type a comment in the query editor starting\n with `--` followed by a\n [single-line comment](/bigquery/docs/reference/standard-sql/lexical#comments),\n and then press `Return`.\n\n For example, if you enter the prompt `-- add a row to table singers` and\n press `Return`, then Gemini generates SQL that's\n similar to the following: \n\n INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate)\n VALUES (1, Alex, 'M.', '1977-10-16');\n\n To continue the example using the `Singers` table, if you enter the\n prompt `-- show all singers born in the 70s`, then Gemini\n generates SQL that's similar to the following: \n\n SELECT *\n FROM Singers\n WHERE Singers.BirthDate\n BETWEEN '1970-01-01' AND '1979-12-31'\n\n | **Note:** Gemini might suggest different syntax each time that you enter the same prompt.\n7. Review the generated SQL and take any of the following actions:\n\n - To accept SQL generated by Gemini, press `Tab`, and then click **Run** to execute the suggested SQL.\n - To edit the SQL generated by Gemini, press `Tab`, edit the SQL, and then click **Run**.\n - To dismiss the suggestion, press `Esc` or continue typing.\n\nExplain SQL statements in the query editor\n------------------------------------------\n\nYou can use Gemini in Spanner to explain SQL queries\nin natural language. This explanation can help you understand the syntax,\nunderlying schema, and business context for complex or long queries.\n\n1. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n2. Select an instance from the list.\n\n3. Select a database.\n\n4. In the navigation pane, click **Spanner Studio**.\n\n5. To query your database, click the add\n **New tab**.\n\n6. In the query editor, paste the query.\n\n7. Highlight the query that you want Gemini to explain, and\n then click spark\n **Explain this query**.\n\n The SQL explanation appears in the **Gemini** pane.\n\nWhat's next\n-----------\n\n- Read [Gemini for Google Cloud overview](/gemini/docs/overview).\n- Learn [how Gemini uses your\n data](/gemini/docs/discover/data-governance)."]]