This quickstart shows you how to perform basic operations in Cloud Spanner using the Google Cloud Console. In the quickstart, you will:
- Create a Cloud Spanner instance, database, and table.
- Add a schema.
- Write and modify data.
- Run a query.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
-
Enable the Cloud Spanner API for the project.
Create an instance
When you first use Cloud Spanner, you must create an instance, which is an allocation of resources that are used by Cloud Spanner databases in that instance.
Go to the Cloud Spanner Instances page in the Cloud Console.
Click Create instance.
For the instance name, enter Test Instance.
For the instance ID, enter test-instance.
Use a Regional configuration.
Choose any regional configuration from the drop-down menu.
Your instance configuration determines the geographic location where your instances are stored and replicated.
Use 1 for the node count.
Your instance creation page now looks like this:
Click Create.
Your instance appears in the instance list.
Create a database
Go to the Cloud Spanner Instances page in the Cloud Console.
Click the Test Instance instance.
Click Create database.
For the database name, enter example-db.
Your database creation page now looks like this:
Click Continue.
Skip the Define your database schema step for now. You'll define your schema in the next section of this quickstart.
Click Create.
The Cloud Console displays the Database details page after the database is created.
Create a schema for your database
The Cloud Console provides two ways to create, alter, and delete tables and indexes in a database:
- By using the default Database editor to specify each part of tables, columns, and indexes.
- By switching the editor to Edit as text mode and entering SQL statements in Cloud Spanner Data Definition Language (DDL) syntax.
This quickstart uses DDL.
- In the left pane of the Cloud Console, click the example-db database.
- Click Create table.
- Click the Edit as text toggle.
In the DDL statement field, enter:
CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), SingerInfo BYTES(MAX), BirthDate DATE, ) PRIMARY KEY(SingerId);
Click Create.
This process might take a few minutes. When the operation is complete, the Cloud Console displays the details of the table that you created:
Insert and modify data
The Cloud Console provides an interface for inserting, editing, and deleting data.
Insert data
- In the left pane of the Cloud Console, click Singers.
- Click Data.
- Click Insert.
Enter the following values:
Field Value SingerID:INT64 1
BirthDate:DATE [leave blank] FirstName:STRING Marc
LastName:STRING Richards
SingerInfo:BYTES [leave blank] Click Save.
The Singers table now has one row:
Click Insert to add an additional row.
Enter the following values:
Field Value SingerID:INT64 2
BirthDate:DATE [leave blank] FirstName:STRING Catalina
LastName:STRING Smith
SingerInfo:BYTES [leave blank] Click Save.
The Singers table now has two rows:
You can also insert empty string values when you enter data.
- In the left pane, click Singers.
- Click Data.
- Click Insert.
Enter the following values:
Field Value SingerID:INT64 3
BirthDate:DATE [leave blank] FirstName:STRING Kena
LastName:STRING [check Empty string] SingerInfo:BYTES [leave blank] Click Save.
The Singers table now has three rows, and the row for SingerID
3
has a LastName that is an empty string:
Edit data
- In the left pane of the Cloud Console, click Singers.
- Click Data.
- Check the box next to the row for SingerId
3
, then click Edit. Enter the following value:
Field Value BirthDate:DATE 1961-04-01
[all other fields] [leave as-is] Click Save.
The row for SingerId
3
in the Singers table now has a BirthDate value:
Delete data
- In the left pane of the Cloud Console, click Singers.
- Click Data.
- Check the box next to the row for SingerId
2
, then click Delete. In the dialog that appears, click Confirm.
The Singers table now has two rows:
Run a query
You can now execute a SQL statement on the query page.
- In the left pane of the Cloud Console, click example-db to navigate to the Database details page.
- On the Database details page, click Query.
For the query, enter:
SELECT * FROM Singers
Click Run.
The Cloud Console displays the result of your query:
Congratulations! You've successfully created a Cloud Spanner database and executed a SQL statement using the query editor!
Cleanup
To avoid additional charges to your Cloud Billing account, delete the database and the instance that you created.
Delete the database
Go to the Cloud Spanner Instances page in the Google Cloud Console.
Click Test Instance.
Click example-db.
In the Database details page, click delete DELETE DATABASE.
Confirm that you want to delete the database and click Delete.
Delete the instance
Deleting an instance automatically deletes all databases created in that instance.
Go to the Cloud Spanner Instances page in the Google Cloud Console.
Click Test Instance.
Click delete DELETE INSTANCE.
Confirm that you want to delete the instance and click Delete.
What's next
- Learn about Instances.
- Understand the Cloud Spanner Schema and Data Model.
- Learn more about Data Definition Language (DDL).
- Learn more about Query Execution Plans.
- Learn how to use Cloud Spanner with C++, C#, Go, Java, Node.js, PHP, Python, Ruby, REST, or gcloud.