This document describes how you can use Gemini Code Assist to get AI-powered assistance with the following in Spanner:
- Generate SQL queries using natural language prompts.
- Complete SQL statements in the query editor.
- Explain SQL queries in the query editor.
Learn how and when Gemini for Google Cloud uses your data.
This document is intended for database administrators and data engineers who are familiar with Spanner, SQL, and data analysis. If you're new to Spanner, see Create and query a database by using the Google Cloud console.
Before you begin
Optional: Set up Gemini Code Assist.
To complete the tasks in this document, ensure that you have the necessary Identity and Access Management (IAM) permissions.
In the Google Cloud console, go to the Spanner page.
Select an instance from the list.
Select a database.
In the navigation menu, click Spanner Studio.
In the taskbar, click pen_spark Gemini to view Gemini features in Spanner.
Select the Gemini features that you want to enable—for example, SQL completion and Comment-to-query generation. You can select and try features for yourself without affecting others working in your project.
Optional: If you want to follow along with the examples in this document, first create the
Singers
table as described in Create a schema for your database.
To disable Gemini features in Spanner, repeat these steps, and then deselect the Gemini features that you want to disable.
Required roles
To get the permissions that you need to complete the tasks in this document,
ask your administrator to grant you the
Gemini for Google Cloud User (roles/cloudaicompanion.user
) IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Generate SQL queries using natural language prompts
You can give Gemini natural language comments (or prompts) to generate queries that are based on your schema. For example, you can prompt Gemini to generate SQL in response to the following prompts:
- "Create a table that tracks customer satisfaction survey results."
- "Add a date column called birthday to the Singers table."
- "How many singers were born in the 90s?"
To generate SQL in Spanner with Gemini assistance, follow these steps:
In the Google Cloud console, go to the Spanner page.
Select an instance from the list.
Select a database.
In the navigation pane, click Spanner Studio. The Explorer pane displays a list of objects in your database.
To query your database, click the SQL generation is enabled.
New SQL editor tab. Make sure thatTo generate SQL, type a comment in the query editor starting with
--
followed by a single-line comment, and then pressReturn
.For example, if you enter the prompt
-- add a row to table singers
and pressReturn
, then Gemini generates SQL that's similar to the following:INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (1, Alex, 'M.', '1977-10-16');
To continue the example using the
Singers
table, if you enter the prompt-- show all singers born in the 70s
, then Gemini generates SQL that's similar to the following:SELECT * FROM Singers WHERE Singers.BirthDate BETWEEN '1970-01-01' AND '1979-12-31'
Review the generated SQL and take any of the following actions:
- To accept SQL generated by Gemini, press
Tab
, and then click Run to execute the suggested SQL. - To edit the SQL generated by Gemini, press
Tab
, edit the SQL, and then click Run. - To dismiss the suggestion, press
Esc
or continue typing.
- To accept SQL generated by Gemini, press
Complete SQL statements in the query editor
To help you write SQL code, Gemini in Spanner provides AI-assisted code completion suggestions. With Gemini enabled, when you enter text in the Spanner Studio query editor, Gemini predicts and autofills SQL code.
Inline code suggestions
To use inline code suggestions, follow these steps:
In the Google Cloud console, go to the Spanner page.
Select an instance from the list.
Select a database.
In the navigation pane, click Spanner Studio.
To query your database, click SQL completion is enabled.
New SQL editor tab. Make sure thatEnter a query and a space or newline at the end of the line. As you enter text, Gemini shows suggested SQL that's based on the supported objects in the selected database's schema.
For example, to add a row to the
Singers
table, begin to write the following SQL:INSERT INTO Singers
.Gemini suggests SQL that's similar to the following:
INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (1, 'Marc', 'Singer', '1970-03-24')
Review the SQL suggestion and take any of the following actions:
- To accept SQL generated by Gemini, press
Tab
, and then click Run to execute the suggested SQL. - To edit the SQL generated by Gemini, press
Tab
, edit the SQL, and then click Run. - To dismiss the suggestion, press
Esc
or continue typing.
- To accept SQL generated by Gemini, press
Help me code tool
To use the Help me code tool, follow these steps:
In the Google Cloud console, go to the Spanner page.
Select an instance from the list.
Select a database.
In the navigation pane, click Spanner Studio.
To query your database, click the
New tab.Click pen_spark Help me code next to the query editor.
In the Help me code window, enter a prompt. For example,
add a row to table singers
and click Generate.Gemini generates SQL that's similar to the following:
INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (1, Alex, 'M.', '1977-10-16');
Review the generated SQL and take any of the following actions:
- To accept SQL generated by Gemini, click Insert to insert the statement into the query editor. Then click Run to execute the suggested SQL.
- To ask Gemini to generate a new query, click Edit. After you've edited your prompt, click Update. You can then decide to accept the new generated statement or dismiss the suggestion.
- To dismiss the suggestion, close the Help me code window.
Explain SQL statements in the query editor
You can use Gemini in Spanner to explain SQL queries in natural language. This explanation can help you understand the syntax, underlying schema, and business context for complex or long queries.
In the Google Cloud console, go to the Spanner page.
Select an instance from the list.
Select a database.
In the navigation pane, click Spanner Studio.
To query your database, click the
New tab.In the query editor, paste the query.
Highlight the query that you want Gemini to explain, and then click spark Explain this query.
The SQL explanation appears in the Gemini pane.