本快速入門導覽課程適用於初次使用 Spanner 的開發人員和資料庫管理員。在 Cloud de Confiance 控制台佈建執行個體,並建立 GoogleSQL 或 PostgreSQL 方言資料庫。接著,您將使用 Cloud de Confiance 控制台中的查詢編輯器 Spanner Studio 定義結構定義、插入及修改資料,以及執行 SQL 查詢。Spanner Studio 會在瀏覽器中執行,因此您不必安裝或設定本機用戶端工具。
事前準備
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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 Cloud de Confiance project.
- 選用:建立 Spanner 執行個體時,系統會自動啟用 Cloud Spanner API。如果尚未啟用,請手動啟用: 啟用 Cloud Spanner API
-
如要取得建立執行個體和資料庫所需的權限,請要求管理員授予您專案的 Cloud Spanner 管理員 (roles/spanner.admin) IAM 角色。
使用 Cloud de Confiance 控制台建立執行個體
首次使用 Spanner 時,您必須建立執行個體,也就是 Spanner 資料庫在該執行個體中使用的資源分配。
前往 Cloud de Confiance 控制台的「Spanner」頁面。
在「Spanner」頁面,按一下「Create a provisioned instance」(建立佈建執行個體)。
如果您曾使用過 Spanner,系統會顯示 Spanner「Instances」(執行個體) 頁面,而非產品頁面。按一下「Create instance」(建立執行個體)。
在「Select an edition」(選取版本) 區段中,選取「Standard」,然後按一下「Continue」(繼續)。
在「Name your instance」(為執行個體命名) 區段,輸入「Instance name」(執行個體名稱),例如
Test Instance。Spanner 會根據執行個體名稱填入「Instance ID」(執行個體 ID),例如
test-instance。視需要變更「執行個體 ID」,然後按一下「繼續」。在「Configure your instance」(設定執行個體) 區段中,針對「Choose a configuration」(選擇設定),保留預設選項「Regional」(區域),然後從下拉式選單中選取設定位置。
執行個體設定會決定 Spanner 儲存及複製執行個體的地理位置。
按一下「繼續」。
在「Configure compute capacity」(設定運算能力) 區段中,「Select unit」(選取單位) 請選取「Processing units (PUs)」(處理單元 (PU))。在「Choose a scaling mode」(選擇資源調度模式) 部分,保留「Manual allocation」(手動分配) 和預設的處理單元「Quantity」(數量)
1000。選用:在「Backups」(備份) 區段中,如果不希望為這個測試執行個體建立預設的備份排程,請取消勾選「Enable default backup schedules」(啟用預設備份排程) 核取方塊。
點選「建立」。
Cloud de Confiance 控制台會顯示您建立的執行個體的「Overview」(總覽) 頁面。
建立資料庫
前往 Cloud de Confiance 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下您建立的執行個體,例如「Test Instance」。
在開啟的執行個體「總覽」頁面,點選「建立資料庫」。
輸入資料庫名稱,例如
example-db。選取資料庫方言。
如要瞭解 PostgreSQL 支援資訊和選擇方言的指南,請參閱「PostgreSQL 介面」。將「建立結構定義 (選用)」部分留空,在本快速入門導覽課程的下一節,您將在 Spanner Studio 中定義結構定義。
點選「建立」。
Cloud de Confiance 控制台會顯示您建立的資料庫「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」(更新完成)。
插入及修改資料
您可以透過 Cloud de Confiance 控制台介面插入、編輯及刪除資料。
插入資料
在 Singers 資料表中插入三個資料列,一次執行一個 INSERT 陳述式。每個陳述式都以 GoogleSQL THEN RETURN 子句或 PostgreSQL RETURNING 子句結尾,因此「Results」(結果) 分頁會在您插入資料列後立即顯示。插入全部三個資料列後,請預覽資料表,查看所有資料列。
如要插入第一個資料列,請在「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 (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」(查看動作),然後點選「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 (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 列),以及插入資料列的資料。
請注意,
LastName欄提供的值是空字串'',而非NULL值。如要預覽資料表中的所有三列,請在「Explorer」窗格中,按一下 「Singers」資料表的「View actions」(查看動作),然後按一下「Preview data」(預覽資料)。
按一下「 執行」。
「Results」(結果) 分頁會顯示
Singers資料表中的三個資料列。主鍵值為3的資料列在LastName欄中包含空字串。
編輯資料
如要更新主鍵值為
3的資料列出生日期,請按一下 「New tab」(新增分頁),然後輸入下列UPDATE陳述式: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」(預覽資料)。
按一下「 執行」。
「Results」(結果) 分頁會顯示
Singers資料表中的資料列,以及歌手3的更新出生日期。
刪除資料
如要刪除主鍵值為
2的資料列,請按一下 「New tab」(新增分頁),然後輸入下列DELETE陳述式:GoogleSQL
DELETE FROM Singers WHERE SingerId = 2;PostgreSQL
DELETE FROM singers WHERE singerid = 2;按一下「 執行」。
Spanner 執行陳述式後,「Results」(結果) 分頁會顯示「1 row deleted」(已刪除 1 列)。
Singers資料表包含兩個資料列。
在 Cloud de Confiance 控制台執行查詢
按一下「 New tab」(新增分頁) 開啟新的查詢分頁,然後在查詢編輯器中輸入下列查詢:
GoogleSQL
SELECT * FROM Singers;PostgreSQL
SELECT * FROM singers;按一下 「Run」(執行)。
Spanner 執行查詢後,「Results」(結果) 分頁會顯示查詢結果。
恭喜!您已成功建立新資料庫、定義結構定義、插入資料,並在 Spanner 查詢編輯器中執行查詢。
清除所用資源
為避免系統向您的 Cloud de Confiance by S3NS帳戶收取額外費用,請刪除您為這項快速入門導覽課程建立的執行個體,例如 Test Instance。刪除執行個體時,Spanner 也會刪除該執行個體中的所有資料庫,包括 example-db 資料庫。
刪除執行個體
前往 Cloud de Confiance 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除的執行個體名稱,例如「Test Instance」。
按一下「 刪除執行個體」。
輸入執行個體 ID (例如
test-instance),確認要刪除執行個體,然後按一下「Delete」(刪除)。
刪除資料庫
如果您是在要保留的現有執行個體中建立資料庫,請只刪除資料庫:
前往 Cloud de Confiance 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。
按一下要刪除的資料庫所在的執行個體名稱。
按一下要刪除的資料庫名稱,例如 example-db。
在資料庫「Overview」(總覽) 頁面中,按一下 「Delete database」(刪除資料庫)。
輸入資料庫名稱,然後按一下「Delete」(刪除),確認要刪除資料庫。
後續步驟
- 瞭解執行個體。
- 瞭解 Spanner 結構定義和資料模型。
- 進一步瞭解 GoogleSQL 資料定義語言 (DDL) 和 PostgreSQL DDL。
- 進一步瞭解查詢執行計畫。
- 瞭解如何搭配使用 Spanner 與 C++、C#、Go、Java、Node.js、PHP、Python、Ruby、REST 或 Google Cloud CLI。