使用 Google Cloud 控制台建立及查詢資料庫

本快速入門導覽課程說明如何使用Google Cloud 控制台在 Spanner 中執行基本作業。在快速入門導覽課程中,您將可以:

  • 建立 Spanner 執行個體。
  • 建立資料庫。
  • 建立結構定義。
  • 插入及修改資料。
  • 執行查詢。

如要瞭解 Spanner 的使用費用,請參閱「定價」一文。

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. 選用:系統應會自動啟用 Spanner API。如果沒有,請手動啟用:

    啟用 Spanner API
  7. 如要取得建立執行個體和資料庫所需的權限,請要求管理員為您授予專案的 Cloud Spanner 管理員 (roles/spanner.admin) IAM 角色。

  8. 建立執行個體

    首次使用 Spanner 時,您必須建立做為資源分配單位的執行個體,其中的 Spanner 資料庫會使用個體內的資源。

    1. 前往 Google Cloud 控制台的「Spanner」頁面。

      前往 Spanner

    2. 選取或建立 Google Cloud 專案 (如果尚未建立)。

    3. 在「Spanner」頁面中,按一下「Create a provisioned instance」(建立已佈建的執行個體)

      如果您之前使用過 Spanner,系統會顯示 Spanner Instances 頁面,而非產品頁面。按一下「建立執行個體」

    4. 在「為執行個體命名」頁面中,輸入執行個體名稱,例如「Test Instance」

    5. 系統會根據執行個體名稱自動輸入執行個體 ID,例如 test-instance。視需要變更。按一下「繼續」

    6. 在「設定執行個體」頁面中,保留預設選項「區域」,然後從下拉式選單中選取設定。

      執行個體設定會決定系統要將執行個體儲存及複製到哪個地理位置。

    7. 按一下「繼續」

    8. 在「Allocate compute capacity」(配置運算資源) 頁面中,選取「Processing units (PUs)」(處理單元),並保留預設值 1000 個處理單元。

    9. 點選「建立」

      Google Cloud 控制台會顯示您建立的執行個體「總覽」頁面。

    建立資料庫

    1. 前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。

      前往 Spanner 執行個體

    2. 按一下您建立的執行個體,例如「Test Instance」

    3. 在開啟的執行個體「總覽」頁面中,按一下「建立資料庫」

    4. 在資料庫名稱一欄輸入名稱,例如 example-db

    5. 選取資料庫方言。

      如要瞭解 PostgreSQL 的支援情形,以及如何選擇方言,請參閱「PostgreSQL 介面」。 如果您選取 GoogleSQL,請在本快速入門導覽課程的下一節,在「Define your schema」(定義結構定義) 文字欄位中定義結構定義。

      您目前的資料庫建立頁面如下所示:

      更新後的資料庫建立視窗。

    6. 點選「建立」

      Google Cloud 控制台會顯示您建立的資料庫「總覽」頁面。

    建立資料庫的結構定義

    1. 在導覽選單中,按一下「Spanner Studio」

    2. 在「Spanner Studio」頁面中,按一下 「新增分頁」,或使用空白的編輯器分頁。

    3. 輸入:

      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
      );
      
    4. 按一下「執行」

      Google Cloud 控制台會返回資料庫的「總覽」頁面,並顯示「結構定義更新」正在進行中。更新完成後,頁面會顯示如下:

      GoogleSQL

      更新後的總覽頁面。

      PostgreSQL

      更新後的總覽頁面。

      請注意,PostgreSQL 會將資料表名稱轉換為小寫。

    插入及修改資料

    Google Cloud 控制台提供的介面可用於插入、編輯及刪除資料。

    插入資料

    1. 在資料庫「總覽」頁面的資料表清單中,按一下「Singers」資料表。

      Google Cloud 控制台會顯示 Singers 資料表的「結構定義」頁面。

    2. 在導覽選單中,按一下「資料」,顯示 Singers 資料表的「資料」頁面。

    3. 按一下 [插入]。

      控制台會顯示 Singers 資料表的 Spanner Studio 頁面,其中包含新的查詢分頁,內含 INSERTSELECT 陳述式,您可編輯這些陳述式,在 Singers 資料表中插入資料列,並查看插入結果: Google Cloud

      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
        (<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 會將資料欄名稱全部轉換為小寫。

    4. 編輯 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;
      
    5. 按一下「執行」

      Spanner 會執行這些陳述式。完成後,「結果」分頁會指出第一個陳述式插入了一列,並提供連結來查看資料表的資料。

    6. 在「結果」分頁中,按一下「表格」連結。「Singers」資料表現在會有一列:

      GoogleSQL

      更新後的 Singers 資料表資料,內含一列。

      PostgreSQL

      更新後的 Singers 資料表資料,內含一列。

    7. 按一下「插入」即可新增其他資料列。

      Google Cloud 控制台會再次顯示 Singers 資料表的 Spanner Studio 頁面,並顯示包含相同 INSERTSELECT 陳述式的新查詢分頁。

    8. 編輯 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;
      
    9. 按一下「執行」

      Spanner 執行陳述式後,「結果」分頁會再次顯示第一個陳述式插入了一列。

    10. 按一下「資料表」連結。「Singers」資料表現在會有兩列:

      GoogleSQL

      更新後的 Singers 資料表資料,內含兩列。

      PostgreSQL

      更新後的 Singers 資料表資料,內含兩列。

    輸入資料時,您也可以插入空字串值。

    1. 按一下「插入」即可新增資料列。

      Spanner 會再次顯示 Singers 資料表的「Spanner Studio」頁面,並在新的查詢分頁中包含相同的 INSERTSELECT 陳述式。

    2. 編輯範本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 值。

    3. 按一下「執行」

      Spanner 執行陳述式後,「結果」分頁會顯示第一個陳述式插入了一列。

    4. 按一下「資料表」連結。「Singers」資料表現在會有三列,且主鍵值為 3 的資料列在「LastName」欄會顯示空字串:

      GoogleSQL

      更新後的 Singers 資料表資料,內含三列。

      PostgreSQL

      更新後的 Singers 資料表資料,內含三列。

    編輯資料

    1. 在 Singers 資料表的「資料」頁面中,選取主鍵值為 3 的資料列核取方塊,然後按一下「編輯」

      Spanner 會顯示「Spanner Studio」頁面,並在新的分頁中顯示範本 UPDATESET 陳述式,供您編輯。請注意,兩個陳述式的 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';
      
    2. 編輯 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';
      
    3. 按一下「執行」

      Spanner 會執行這些陳述式。完成後,「結果」分頁會指出第一個陳述式更新了一列,並提供連結供您查看資料表的資料。

    4. 在「結果」分頁中,按一下「表格」連結。

      更新後的資料列現在有出生日期值。

      GoogleSQL

      更新後的 Singers 資料表資料,其中包含更新後的資料列。

      PostgreSQL

      更新後的 Singers 資料表資料,其中包含更新後的資料列。

    刪除資料

    1. 在 Singers 資料表的「Data」(資料) 頁面中,選取第一欄中含有 2 的資料列核取方塊,然後按一下「Delete」(刪除)
    2. 在隨即顯示的對話方塊中,按一下「確認」

      「Singers」資料表現在會有兩列:

      GoogleSQL

      更新後的 Singers 資料表資料,內含兩列 (SingerId 為 2 的資料列已遭刪除)。

      PostgreSQL

      更新後的 Singers 資料表資料,內含兩列 (SingerId 為 2 的資料列已遭刪除)。

    執行查詢

    1. 在資料庫的「總覽」頁面中,按一下導覽選單中的「Spanner Studio」

    2. 按一下「新增分頁」,建立新的查詢分頁。然後在查詢編輯器中輸入下列查詢:

      GoogleSQL

      SELECT * FROM Singers;
      

      PostgreSQL

      SELECT * FROM singers;
      
    3. 按一下「執行」

      Spanner 會執行查詢。完成後,「結果」分頁會顯示查詢結果:

      GoogleSQL

      查詢結果。

      PostgreSQL

      查詢結果。

    恭喜!您已成功建立 Spanner 資料庫,並使用查詢編輯器執行 SQL 陳述式。

    清除所用資源

    為避免系統向您的 Cloud Billing 帳戶收取額外費用,請刪除您建立的資料庫和執行個體。刪除執行個體時,也會自動刪除您在其中建立的所有資料庫。

    刪除資料庫

    1. 前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。

      前往 Spanner 執行個體

    2. 按一下要刪除資料庫的執行個體名稱,例如「Test Instance」

    3. 按一下要刪除的資料庫名稱,例如 example-db

    4. 在「Database details」(資料庫詳細資料) 頁面,按一下 「Delete database」(刪除資料庫)

    5. 輸入資料庫名稱,然後按一下「刪除」,確認要刪除資料庫。

    刪除執行個體

    1. 前往 Google Cloud 控制台的「Spanner Instances」(Spanner 執行個體) 頁面。

      前往 Spanner 執行個體

    2. 按一下要刪除的執行個體名稱,例如「Test Instance」

    3. 按一下 「Delete instance」(刪除執行個體)

    4. 輸入執行個體名稱,然後按一下「刪除」,確認要刪除執行個體。

    後續步驟