本快速入門導覽課程說明如何使用 Google Cloud 控制台,在 Spanner 中建立資料庫、插入資料及執行 SQL 查詢。
在快速入門導覽課程中,您將可以:
- 建立 Spanner 執行個體。
- 建立資料庫。
- 建立結構定義。
- 插入及修改資料
- 執行查詢。
如要瞭解 Spanner 的使用費用,請參閱「定價」。
事前準備
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 選用:Spanner API 應會自動啟用。如果沒有,請手動啟用: 啟用 Spanner API
-
如要取得建立執行個體和資料庫所需的權限,請要求管理員授予您專案的 Cloud Spanner 管理員 (roles/spanner.admin) IAM 角色。
使用 Google Cloud 控制台建立執行個體
首次使用 Spanner 時,必須建立執行個體,也就是 Spanner 資料庫所用的資源分配單位。
前往 Google Cloud 控制台的「Spanner」頁面。
選取或建立 Google Cloud 專案 (如果尚未建立)。
在「Spanner」頁面,點選「建立已佈建的執行個體」。
如果您曾使用過 Spanner,系統會顯示 Spanner「Instances」(執行個體) 頁面,而非產品頁面。按一下「Create instance」(建立執行個體)。
在「Select an edition」(選取版本) 區段中,選取「Standard」,然後按一下「Continue」(繼續)。
在「Name your instance」(為執行個體命名) 區段,輸入「Instance name」(執行個體名稱),例如
Test Instance。系統會根據執行個體名稱自動填入「Instance ID」(執行個體 ID),例如
test-instance。視需要變更,然後按一下「Continue」(繼續)。在「設定執行個體」部分,針對「選擇設定」保留預設選項「區域」,然後從下拉式選單中選取設定位置。
執行個體設定會決定執行個體的儲存和複製地理位置。
按一下「繼續」。
在「Configure compute capacity」(設定運算能力) 區段中,「Select unit」(選取單位) 請選取「Processing units (PUs)」(處理單元 (PU))。在「Choose a scaling mode」(選擇資源調度模式) 部分,保留「Manual allocation」(手動分配) 和預設的處理單元「Quantity」(數量)
1000。選用:在「Backups」(備份) 區段中,如果不希望為這個測試執行個體建立預設的備份排程,請取消勾選「Enable default backup schedules」(啟用預設備份排程) 核取方塊。
點選「建立」。
Google Cloud 控制台會顯示您建立的執行個體的「Overview」(總覽) 頁面。
建立資料庫
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下您建立的執行個體,例如「Test Instance」。
在開啟的執行個體「總覽」頁面中,點選「建立資料庫」。
輸入資料庫名稱,例如 example-db。
選取資料庫方言。
如要瞭解 PostgreSQL 支援資訊和選擇方言的指南,請參閱「PostgreSQL 介面」。將「建立結構定義 (選用)」部分留空,在本快速入門導覽課程的下一節,您將在 Spanner Studio 中定義結構定義。
點選「建立」。
Google Cloud 控制台會顯示您建立的資料庫「Overview」(總覽) 頁面。
建立資料庫的結構定義
在導覽選單中,點選「Spanner Studio」。
在「Spanner Studio」頁面中,按一下 「New tab」(新增分頁),或使用「Untitled query」(未命名的查詢) 分頁,然後根據資料庫的方言,輸入下列其中一項查詢:
GoogleSQL
CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), BirthDate DATE ) PRIMARY KEY(SingerId);PostgreSQL
CREATE TABLE Singers ( SingerId BIGINT PRIMARY KEY, FirstName VARCHAR(1024), LastName VARCHAR(1024), BirthDate TIMESTAMPTZ );按一下 「Run」(執行)。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「Update completed」(更新完成)。
插入及修改資料
您可以透過 Google Cloud 控制台介面插入、編輯及刪除資料。
插入資料
在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後按一下「Insert data」(插入資料)。
Spanner 會在 Spanner Studio 中開啟新的查詢分頁,其中包含範本
INSERT陳述式。編輯範本
INSERT陳述式的VALUES子句,在Singers資料表中插入資料列,並查看插入結果:GoogleSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (1, -- type: INT64 'Marc', -- type: STRING(1024) 'Richards', -- type: STRING(1024) NULL -- type: DATE ) THEN RETURN SingerId, FirstName, LastName, BirthDate;PostgreSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO singers (singerid, firstname, lastname, birthdate) VALUES (1, -- type: bigint 'Marc', -- type: character varying 'Richards', -- type: character varying NULL -- type: timestamp with time zone ) RETURNING singerid, firstname, lastname, birthdate;請注意,PostgreSQL 會將資料欄名稱轉換為全小寫。
按一下 「Run」(執行)。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「1 row inserted」(已插入 1 列) 和插入資料列的資料。
在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後按一下「Insert data」(插入資料)。
編輯
INSERT陳述式的VALUES子句:GoogleSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (2, -- type: INT64 'Catalina', -- type: STRING(1024) 'Smith', -- type: STRING(1024) NULL -- type: DATE ) THEN RETURN SingerId, FirstName, LastName, BirthDate;PostgreSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO singers (singerid, firstname, lastname, birthdate) VALUES (2, -- type: bigint 'Catalina', -- type: character varying 'Smith', -- type: character varying NULL -- type: timestamp with time zone ) RETURNING singerid, firstname, lastname, birthdate;按一下 「Run」(執行)。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「1 row inserted」(已插入 1 列) 和插入資料列的資料。
在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後點選「Preview data」(預覽資料)。
按一下 「Run」(執行)。「Results」(結果) 分頁會顯示
Singers資料表中的兩個資料列。
您也可在輸入資料時插入空字串值。
在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後按一下「Insert data」(插入資料)。
Spanner 會在 Spanner Studio 中開啟新的查詢分頁,其中包含範本
INSERT陳述式。編輯範本
INSERT陳述式的VALUES子句:GoogleSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate) VALUES (3, -- type: INT64 'Kena', -- type: STRING(1024) '', -- type: STRING(1024) NULL -- type: DATE ) THEN RETURN SingerId, FirstName, LastName, BirthDate;PostgreSQL
-- Add new values in the VALUES clause in order of the column list. -- Each value must be type compatible with its associated column. INSERT INTO singers (singerid, firstname, lastname, birthdate) VALUES (3, -- type: bigint 'Kena', -- type: character varying '', -- type: character varying NULL -- type: timestamp with time zone ) RETURNING singerid, firstname, lastname, birthdate;按一下 「Run」(執行)。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「1 row inserted」(已插入 1 列) 和插入資料列的資料。
請注意,姓氏欄的值是空字串
'',而非NULL值。在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後點選「Preview data」(預覽資料)。
按一下 「Run」(執行)。「Results」(結果) 分頁會顯示
Singers資料表中的三個資料列,而主鍵值為3的資料列在LastName資料欄中含有空字串。
編輯資料
按一下 「New tab」(新增分頁) 建立新的查詢分頁,然後輸入下列
UPDATE陳述式,更新主鍵值為3的資料列出生日期:GoogleSQL
UPDATE Singers SET BirthDate='1961-04-01' WHERE SingerId=3 THEN RETURN SingerId, FirstName, LastName, BirthDate;PostgreSQL
UPDATE singers SET birthdate='1961-04-01 00:00:00 -8:00' WHERE singerid=3 RETURNING singerid, firstname, lastname, birthdate;按一下「 執行」。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「1 row updated」(已更新 1 列) 和更新後的資料列。
在「Explorer」窗格中,按一下「Singers」資料表的 「View actions」(查看動作),然後點選「Preview data」(預覽資料)。
按一下 「Run」(執行)。「Results」(結果) 分頁會顯示
Singers資料表中的資料列,其中更新後的資料列現在有出生日期值。
刪除資料
按一下 「New tab」(新增分頁) 建立新的查詢分頁,然後輸入下列
DELETE陳述式,刪除主鍵值為2的資料列:GoogleSQL
DELETE FROM Singers WHERE SingerId = 2;PostgreSQL
DELETE FROM singers WHERE singerid = 2;按一下「 執行」。
Spanner 會執行陳述式。完成後,「Results」(結果) 分頁會顯示「1 row deleted」(已刪除 1 列)。
Singers資料表現在會有兩個列。
在 Google Cloud 控制台執行查詢
按一下「 新增分頁」,建立新的查詢分頁。接著,在查詢編輯器中輸入下列查詢:
GoogleSQL
SELECT * FROM Singers;PostgreSQL
SELECT * FROM singers;按一下 「Run」(執行)。
Spanner 會執行查詢。完成後,「Results」(結果) 分頁會顯示查詢結果。
恭喜!您已成功建立 Spanner 資料庫,並利用查詢編輯器執行 SQL 陳述式!
清除所用資源
為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除資料庫和執行個體。停用 Cloud Billing API 並不會停止計費。刪除執行個體時,當中的所有資料庫都會刪除。
刪除資料庫
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除的資料庫所在的執行個體名稱,例如「Test Instance」(測試執行個體)。
按一下要刪除的資料庫名稱,例如 example-db。
在「Database details」(資料庫詳細資料) 頁面中,按一下 delete「Delete database」(刪除資料庫)。
輸入資料庫名稱,然後按一下「Delete」(刪除),確認要刪除資料庫。
刪除執行個體
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除的執行個體名稱,例如「Test Instance」。
按一下「delete 刪除執行個體」。
輸入執行個體名稱,然後按一下「刪除」,確認要刪除執行個體。