This document describes how you can use Gemini , an AI-powered collaborator in Google Cloud, to help you do the following in Cloud SQL Studio:
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 SQL, data analysis, and Cloud SQL. If you're new to Cloud SQL, see Cloud SQL overview .
Before you begin
- To complete the tasks in this document, ensure that you have the necessary Identity and Access Management (IAM) permissions .
- Confirm access to Cloud SQL Studio. If you don't have access to Cloud SQL Studio, then see Cloud SQL Studio required roles and permissions
-
Optional: To follow along with the examples in this document, create a
Singers
table using the following schema:CREATE TABLE Singers ( SingerId BIGINT PRIMARY KEY , FirstName VARCHAR ( 1024 ), LastName VARCHAR ( 1024 ), SingerInfo MEDIUMBLOB , BirthDate DATETIME );
After you create the
Singers
table, click refresh Refreshto update your database schema.
Required roles and permissions
To get the permissions that
you need to complete the tasks in this document,
ask your administrator to grant you the Cloud SQL Admin
( roles/cloudsql.admin
)
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 .
You also need the cloudaicompanion.companions.generateChat
permission to use explain SQL statements in the query editor
and cloudaicompanion.companions.generateCode
to use help me code
.
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?"
If your schema changes, such as addition of a new table or column, then click refresh Refreshto update your schema before using Gemini.
To generate SQL in Cloud SQL with Gemini assistance, follow these steps:
-
In the Google Cloud console, go to the Cloud SQLpage.
-
Select an instance from the list.
-
In the navigation menu, click Cloud SQL Studio.
-
Sign in to Cloud SQL Studio using the name of your database, username, and password. The Explorerpane displays a list of objects in your database.
-
In the taskbar, click pen_spark Geminito view Gemini features in Cloud SQL.
-
Ensure that Comment-to-query generationis enabled.
-
To query your database, click New SQL editor tab.
-
To generate SQL, type a comment in the query editor starting with
--
followed by a single-line comment , and then press Return or Enter .For example, enter the following prompt:
-- add a row to table singers
Press Return or Enter . 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, enter the following prompt:-- show all singers born in the 70s
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 SQL suggestion and take any of the following actions:
- To view the options for accepting the SQL generated by Gemini,
hold the pointer over the query. The following options are displayed:
- Accept: To accept the suggested query, press
Tab
, then click Run. - Accept word: To accept the suggested query partially,
press
Control
+Right arrow
. (orCommand
+Right arrow
on macOS), then click Run.
- Accept: To accept the suggested query, press
- To edit the original SQL, press
Tab
, edit the SQL, and then click Run. - To dismiss the suggestion, press
Esc
or continue typing.
- To view the options for accepting the SQL generated by Gemini,
hold the pointer over the query. The following options are displayed:
Help me code tool
To use the Help me codetool, follow these steps:
-
In the Google Cloud console, go to the Cloud SQLpage.
-
Select an instance from the list.
-
In the navigation menu, click Cloud SQL Studio.
-
Sign in to Cloud SQL Studio using the name of your database, username, and password. The Explorerpane displays a list of objects in your database.
-
To query your database, click the New tab.
-
Click pen_spark Help me codenext to the query editor.
-
In the Help me codewindow, 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 Insertto insert the statement into the query editor. Then click Runto 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 codewindow.
Explain SQL statements in the query editor
You can use Gemini in Cloud SQL 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 Cloud SQLpage.
-
Select an instance from the list.
-
In the navigation menu, click Cloud SQL Studio.
-
Sign in to Cloud SQL Studio using the name of your database, username, and password. The Explorerpane displays a list of objects in your database.
-
To query your database, click the New tab.
-
In the query editor, paste the query you want explained.
-
Highlight the query that you want Gemini to explain, and then click spark Explain this query.
The explanation for the SQL query appears in the Gemini Chatwindow.
What's next
- Read Gemini for Google Cloud overview .
- Learn how Gemini uses your data .
- Learn how to analyze your data in Google Cloud using Gemini .