使用 Google Cloud 控制台创建和查询数据库

本快速入门介绍如何使用 Google Cloud 控制台在 Spanner 中执行基本操作。在快速入门中,您将执行以下操作:

  • 创建 Spanner 实例。
  • 创建数据库。
  • 创建架构。
  • 插入和修改数据。
  • 运行查询。

如需了解 Spanner 的使用费用,请参阅价格

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. 可选:Spanner API 应该会自动启用。如果未启用,请手动启用:

    启用 Spanner API
  1. 如需获取创建实例和数据库所需的权限,请让管理员授予您项目的 Cloud Spanner Admin (`roles/spanner.admin`) IAM 角色。

创建实例

首次使用 Spanner 时,您必须创建一个实例,用于分配该实例中 Spanner 数据库所使用的资源。

  1. 在 Google Cloud 控制台中,转到 Spanner 页面。

    转到 Spanner

  2. 选择或创建 Google Cloud 项目(如果尚未创建)。

  3. Spanner 页面上,点击创建预配的实例

    如果您之前使用过 Spanner,将会看到 Spanner 实例页面,而不是产品页面。点击创建实例

  4. 为实例命名页面中,输入实例名称,例如测试实例

  5. 系统会根据实例名称自动输入实例 ID,例如 test-instance。请根据需要进行更改。点击继续

  6. 配置实例页面中,保留默认选项区域,然后从下拉菜单中选择配置。

    您的实例配置决定了您的实例会存储和复制到什么地理位置。

  7. 点击继续

  8. 分配计算容量页面中,选择处理单元 (PU),并保留默认值(1000 个处理单元)。

  9. 点击创建

    Google Cloud 控制台会显示您创建的实例的概览页面。

创建数据库

  1. 在 Google Cloud 控制台中,前往 Spanner 实例页面。

    转到 Spanner 实例

  2. 点击您创建的实例,例如测试实例

  3. 在打开的实例概览页面中,点击创建数据库

  4. 请输入一个名称,例如 example-db 作为数据库名称。

  5. 选择数据库方言。

    如需了解对 PostgreSQL 的支持以及有关选择方言的指导,请参阅 PostgreSQL 界面。如果您选择了 GoogleSQL,则需要在本快速入门的下一部分的定义您的架构文本字段中定义架构。

    数据库创建页面现在如下所示:

    更新后的数据库创建窗口。

  6. 点击创建

    Google Cloud 控制台会显示您创建的数据库的概览页面。

创建数据库的架构

  1. 在导航菜单中,点击 Spanner Studio

  2. Spanner Studio 页面中,点击 New tab,或使用空的编辑器标签页。

  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. 在导航菜单中,点击 Data(数据)以显示 Singers 表的数据页面。

  3. 点击插入

    Google Cloud 控制台会显示 Singers 表的 Spanner Studio 页面,其中包含一个新的查询标签页,其中包含 INSERTSELECT 语句,您可以修改这些语句以在 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,
        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 实例页面。

    转到 Spanner 实例

  2. 点击要删除的数据库所在的实例的名称,例如测试实例

  3. 点击要删除的数据库的名称,例如 example-db

  4. 数据库详细信息页面中,点击 删除数据库

  5. 输入数据库名称并点击删除,以确认您要删除该数据库。

删除实例

  1. 在 Google Cloud 控制台中,前往 Spanner 实例页面。

    转到 Spanner 实例

  2. 点击要删除的实例的名称,例如测试实例

  3. 点击 删除实例

  4. 输入实例名称并点击删除,以确认您要删除此实例。

后续步骤