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

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

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

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

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

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

    Go to project selector

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

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

    启用 Spanner API
  1. 为了获得创建实例和数据库所需的权限, 请让管理员向您授予 Cloud Spanner 管理员 (`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) 并保留默认值 1,000 个处理单元

  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 页面中,点击 打开新的标签页或使用空白 编辑器标签页。

  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 表的 Data 页面。

  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 表的数据页面上,选中第一列中包含 2 的行对应的复选框,然后点击删除
  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. 输入实例名称并点击删除,确认要删除此实例。

后续步骤