本快速入門導覽課程說明如何使用Google Cloud 控制台在 Spanner 中執行基本作業。在快速入門導覽課程中,您將可以:
- 建立 Spanner 執行個體。
- 建立資料庫。
- 建立結構定義。
- 插入及修改資料。
- 執行查詢。
如要瞭解 Spanner 的使用費用,請參閱「定價」一文。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 Google Cloud project.
-
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 Google Cloud project.
- 選用:系統應會自動啟用 Spanner API。如果沒有,請手動啟用: 啟用 Spanner API
-
如要取得建立執行個體和資料庫所需的權限,請要求管理員為您授予專案的 Cloud Spanner 管理員 (roles/spanner.admin) IAM 角色。
前往 Google Cloud 控制台的「Spanner」頁面。
選取或建立 Google Cloud 專案 (如果尚未建立)。
在「Spanner」頁面中,按一下「Create a provisioned instance」(建立已佈建的執行個體)。
如果您之前使用過 Spanner,系統會顯示 Spanner Instances 頁面,而非產品頁面。按一下「建立執行個體」。
在「為執行個體命名」頁面中,輸入執行個體名稱,例如「Test Instance」。
系統會根據執行個體名稱自動輸入執行個體 ID,例如 test-instance。視需要變更。按一下「繼續」。
在「設定執行個體」頁面中,保留預設選項「區域」,然後從下拉式選單中選取設定。
執行個體設定會決定系統要將執行個體儲存及複製到哪個地理位置。
按一下「繼續」。
在「Allocate compute capacity」(配置運算資源) 頁面中,選取「Processing units (PUs)」(處理單元),並保留預設值 1000 個處理單元。
點選「建立」。
Google Cloud 控制台會顯示您建立的執行個體「總覽」頁面。
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下您建立的執行個體,例如「Test Instance」。
在開啟的執行個體「總覽」頁面中,按一下「建立資料庫」。
在資料庫名稱一欄輸入名稱,例如 example-db。
選取資料庫方言。
如要瞭解 PostgreSQL 的支援情形,以及如何選擇方言,請參閱「PostgreSQL 介面」。 如果您選取 GoogleSQL,請在本快速入門導覽課程的下一節,在「Define your schema」(定義結構定義) 文字欄位中定義結構定義。
您目前的資料庫建立頁面如下所示:
點選「建立」。
Google Cloud 控制台會顯示您建立的資料庫「總覽」頁面。
在導覽選單中,按一下「Spanner Studio」。
在「Spanner Studio」頁面中,按一下
「新增分頁」,或使用空白的編輯器分頁。輸入:
GoogleSQL
CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), SingerInfo BYTES(MAX), BirthDate DATE ) PRIMARY KEY(SingerId);
PostgreSQL
CREATE TABLE Singers ( BirthDate TIMESTAMPTZ, SingerId BIGINT PRIMARY KEY, FirstName VARCHAR(1024), LastName VARCHAR(1024), SingerInfo BYTEA );
按一下「執行」。
Google Cloud 控制台會返回資料庫的「總覽」頁面,並顯示「結構定義更新」正在進行中。更新完成後,頁面會顯示如下:
GoogleSQL
PostgreSQL
請注意,PostgreSQL 會將資料表名稱轉換為小寫。
在資料庫「總覽」頁面的資料表清單中,按一下「Singers」資料表。
Google Cloud 控制台會顯示 Singers 資料表的「結構定義」頁面。
在導覽選單中,按一下「資料」,顯示 Singers 資料表的「資料」頁面。
按一下 [插入]。
控制台會顯示 Singers 資料表的 Spanner Studio 頁面,其中包含新的查詢分頁,內含
INSERT
和SELECT
陳述式,您可編輯這些陳述式,在 Singers 資料表中插入資料列,並查看插入結果: Google CloudGoogleSQL
-- 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, BirthDate, FirstName, LastName, SingerInfo) VALUES (<SingerId>, -- type: INT64 <BirthDate>, -- type: DATE <FirstName>, -- type: STRING(1024) <LastName>, -- type: STRING(1024) <SingerInfo> -- type: BYTES(MAX) ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM Singers WHERE SingerId=<SingerId>;
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, birthdate, firstname, lastname, singerinfo) VALUES (<singerid>, -- type: bigint <birthdate>, -- type: timestamp with time zone <firstname>, -- type: character varying <lastname>, -- type: character varying <singerinfo> -- type: bytea ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM singers WHERE singerid=<singerid>;
請注意,PostgreSQL 會將資料欄名稱全部轉換為小寫。
編輯
INSERT
陳述式的VALUES
子句和SELECT
陳述式的WHERE
子句: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, BirthDate, FirstName, LastName, SingerInfo) VALUES (1, -- type: INT64 NULL, -- type: DATE 'Marc', -- type: STRING(1024) 'Richards', -- type: STRING(1024) NULL -- type: BYTES(MAX) ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM Singers WHERE SingerId=1;
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, birthdate, firstname, lastname, singerinfo) VALUES (1, -- type: bigint NULL, -- type: timestamp with time zone 'Marc', -- type: character varying 'Richards', -- type: character varying NULL -- type: bytea ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM singers WHERE singerid=1;
按一下「執行」。
Spanner 會執行這些陳述式。完成後,「結果」分頁會指出第一個陳述式插入了一列,並提供連結來查看資料表的資料。
在「結果」分頁中,按一下「表格」連結。「Singers」資料表現在會有一列:
GoogleSQL
PostgreSQL
按一下「插入」即可新增其他資料列。
Google Cloud 控制台會再次顯示 Singers 資料表的 Spanner Studio 頁面,並顯示包含相同
INSERT
和SELECT
陳述式的新查詢分頁。編輯
INSERT
陳述式的VALUES
子句和SELECT
陳述式的WHERE
子句: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, BirthDate, FirstName, LastName, SingerInfo) VALUES (2, -- type: INT64 NULL, -- type: DATE 'Catalina', -- type: STRING(1024) 'Smith', -- type: STRING(1024) NULL -- type: BYTES(MAX) ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM Singers WHERE SingerId=2;
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, birthdate, firstname, lastname, singerinfo) VALUES (2, -- type: bigint NULL, -- type: timestamp with time zone 'Catalina', -- type: character varying 'Smith', -- type: character varying NULL -- type: bytea ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM singers WHERE singerid=2;
按一下「執行」。
Spanner 執行陳述式後,「結果」分頁會再次顯示第一個陳述式插入了一列。
按一下「資料表」連結。「Singers」資料表現在會有兩列:
GoogleSQL
PostgreSQL
按一下「插入」即可新增資料列。
Spanner 會再次顯示 Singers 資料表的「Spanner Studio」頁面,並在新的查詢分頁中包含相同的
INSERT
和SELECT
陳述式。編輯範本
INSERT
聲明的VALUES
子句和SELECT
聲明的WHERE
子句: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, BirthDate, FirstName, LastName, SingerInfo) VALUES (3, -- type: INT64 NULL, -- type: DATE 'Kena', -- type: STRING(1024) '', -- type: STRING(1024) NULL -- type: BYTES(MAX) ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM Singers WHERE SingerId=3;
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, birthdate, firstname, lastname, singerinfo) VALUES (3, -- type: bigint NULL, -- type: timestamp with time zone 'Kena', -- type: character varying '', -- type: character varying NULL -- type: bytea ); -- Change values in the WHERE condition to match the inserted row. SELECT * FROM singers WHERE singerid=3;
請注意,提供給「姓氏」欄的值是空白字串
''
,而非NULL
值。按一下「執行」。
Spanner 執行陳述式後,「結果」分頁會顯示第一個陳述式插入了一列。
按一下「資料表」連結。「
Singers
」資料表現在會有三列,且主鍵值為3
的資料列在「LastName
」欄會顯示空字串:GoogleSQL
PostgreSQL
在 Singers 資料表的「資料」頁面中,選取主鍵值為
3
的資料列核取方塊,然後按一下「編輯」。Spanner 會顯示「Spanner Studio」頁面,並在新的分頁中顯示範本
UPDATE
和SET
陳述式,供您編輯。請注意,兩個陳述式的WHERE
子句都指出要更新的資料列,是主鍵值為3
的資料列。GoogleSQL
-- Change values in the SET clause to update the row where the WHERE condition is true. UPDATE Singers SET BirthDate='', FirstName='Kena', LastName='', SingerInfo='' WHERE SingerId=3; SELECT * FROM Singers WHERE SingerId=3;
PostgreSQL
-- Change values in the SET clause to update the row where the WHERE condition is true. UPDATE singers SET birthdate=NULL, firstname='Kena', lastname='', singerinfo=NULL WHERE singerid='3'; SELECT * FROM singers WHERE singerid='3';
編輯
UPDATE
陳述式的SET
子句,只更新出生日期:GoogleSQL
-- Change values in the SET clause to update the row where the WHERE condition is true. UPDATE Singers SET BirthDate='1961-04-01' WHERE SingerId=3; SELECT * FROM Singers WHERE SingerId=3;
PostgreSQL
-- Change values in the SET clause to update the row where the WHERE condition is true. UPDATE singers SET birthdate='1961-04-01 00:00:00 -8:00' WHERE singerid='3'; SELECT * FROM singers WHERE singerid='3';
按一下「執行」。
Spanner 會執行這些陳述式。完成後,「結果」分頁會指出第一個陳述式更新了一列,並提供連結供您查看資料表的資料。
在「結果」分頁中,按一下「表格」連結。
更新後的資料列現在有出生日期值。
GoogleSQL
PostgreSQL
- 在 Singers 資料表的「Data」(資料) 頁面中,選取第一欄中含有
2
的資料列核取方塊,然後按一下「Delete」(刪除)。 在隨即顯示的對話方塊中,按一下「確認」。
「Singers」資料表現在會有兩列:
GoogleSQL
PostgreSQL
在資料庫的「總覽」頁面中,按一下導覽選單中的「Spanner Studio」。
按一下「新增分頁」,建立新的查詢分頁。然後在查詢編輯器中輸入下列查詢:
GoogleSQL
SELECT * FROM Singers;
PostgreSQL
SELECT * FROM singers;
按一下「執行」。
Spanner 會執行查詢。完成後,「結果」分頁會顯示查詢結果:
GoogleSQL
PostgreSQL
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除資料庫的執行個體名稱,例如「Test Instance」。
按一下要刪除的資料庫名稱,例如 example-db。
在「Database details」(資料庫詳細資料) 頁面,按一下 delete「Delete database」(刪除資料庫)。
輸入資料庫名稱,然後按一下「刪除」,確認要刪除資料庫。
前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除的執行個體名稱,例如「Test Instance」。
按一下 delete「Delete instance」(刪除執行個體)。
輸入執行個體名稱,然後按一下「刪除」,確認要刪除執行個體。
- 瞭解執行個體。
- 瞭解 Spanner 結構定義與資料模型。
- 進一步瞭解 GoogleSQL 資料定義語言 (DDL)。
- 進一步瞭解查詢執行計劃。
- 瞭解如何搭配 C++、C#、Go、Java、Node.js、PHP、Python、Ruby、REST 或 gcloud 使用 Spanner。
建立執行個體
首次使用 Spanner 時,您必須建立做為資源分配單位的執行個體,其中的 Spanner 資料庫會使用個體內的資源。
建立資料庫
建立資料庫的結構定義
插入及修改資料
Google Cloud 控制台提供的介面可用於插入、編輯及刪除資料。
插入資料
輸入資料時,您也可以插入空字串值。
編輯資料
刪除資料
執行查詢
恭喜!您已成功建立 Spanner 資料庫,並使用查詢編輯器執行 SQL 陳述式。
清除所用資源
為避免系統向您的 Cloud Billing 帳戶收取額外費用,請刪除您建立的資料庫和執行個體。刪除執行個體時,也會自動刪除您在其中建立的所有資料庫。