

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 資料表空間的常見任務
<a name="db2-managing-tablespaces"></a>

您可以為 RDS for Db2 資料庫建立、更改、重新命名或捨棄資料表空間。建立、更改、重新命名或捨棄資料表空間需要更高層級的 `SYSADM` 授權，這些授權不適用於主要使用者。請改用 Amazon RDS 預存程序。

**Topics**
+ [建立資料表空間](#db2-creating-tablespace)
+ [更改資料表空間](#db2-altering-tablespace)
+ [將資料表空間重新命名](#db2-renaming-tablespace)
+ [捨棄資料表空間](#db2-dropping-tablespace)
+ [檢查資料表空間的狀態](#db2-checking-tablespaces-procedure)
+ [傳回資料表空間的詳細資訊](#db2-tablespaces-info-db2pd)
+ [列出資料表空間的狀態和儲存群組](#db2-state-storage-group-tablespace-sql)
+ [列出資料表的資料表空間](#db2-return-tablespaces-sql)
+ [列出資料表空間容器](#db2-listing-tablespace-containers)

## 建立資料表空間
<a name="db2-creating-tablespace"></a>

若要為您的 RDS for Db2 資料庫建立資料表空間，請呼叫 `rdsadmin.create_tablespace` 預存程序。如需詳細資訊，請參閱 IBM Db2 文件中的 [CREATE TABLESPACE 陳述式](https://www.ibm.com/docs/en/db2/11.5?topic=statements-create-tablespace)。

**重要**  
若要建立資料表空間，您必須具有相同頁面大小的緩衝集區，才能與資料表空間建立關聯。如需詳細資訊，請參閱[緩衝集區的常見任務](db2-managing-buffer-pools.md)。

**建立資料表空間**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 `rdsadmin` 資料庫。在下列範例中，將 *master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 "connect to rdsadmin user master_username using master_password"
   ```

1. 呼叫 `rdsadmin.create_tablespace` 來建立資料表空間。如需詳細資訊，請參閱[rdsadmin.create\$1tablespace](db2-sp-managing-tablespaces.md#db2-sp-create-tablespace)。

   ```
   db2 "call rdsadmin.create_tablespace(
       'database_name', 
       'tablespace_name',
       'buffer_pool_name', 
       tablespace_initial_size, 
       tablespace_increase_size, 
       'tablespace_type')"
   ```

## 更改資料表空間
<a name="db2-altering-tablespace"></a>

若要更改 RDS for Db2 資料庫的資料表空間，請呼叫 `rdsadmin.alter_tablespace` 預存程序。您可以使用此預存程序來變更資料表空間的緩衝集區、降低高浮水印，或讓資料表空間上線。如需詳細資訊，請參閱 IBM Db2 文件中的 [ALTER TABLESPACE 陳述式](https://www.ibm.com/docs/en/db2/11.5?topic=statements-alter-tablespace)。

**更改資料表空間**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 `rdsadmin` 資料庫。在下列範例中，將 *master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 "connect to rdsadmin user master_username using master_password"
   ```

1. 呼叫 `rdsadmin.alter_tablespace` 來更改資料表空間。如需詳細資訊，請參閱[rdsadmin.alter\$1tablespace](db2-sp-managing-tablespaces.md#db2-sp-alter-tablespace)。

   ```
   db2 "call rdsadmin.alter_tablespace(
       'database_name', 
       'tablespace_name', 
       'buffer_pool_name', 
       buffer_pool_size, 
       tablespace_increase_size, 
       'max_size', 'reduce_max', 
       'reduce_stop', 
       'reduce_value', 
       'lower_high_water', 
       'lower_high_water_stop', 
       'switch_online')"
   ```

## 將資料表空間重新命名
<a name="db2-renaming-tablespace"></a>

若要變更 RDS for Db2 資料庫的資料表空間之名稱，請呼叫 `rdsadmin.rename_tablespace` 預存程序。如需詳細資訊，請參閱 IBM Db2 文件中的 [RENAME TABLESPACE 陳述式](https://www.ibm.com/docs/en/db2/11.5?topic=statements-rename-tablespace)。

**將資料表空間重新命名**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 `rdsadmin` 資料庫。在下列範例中，將 *master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 "connect to rdsadmin user master_username using master_password"
   ```

1. 呼叫 `rdsadmin.rename_tablespace` 將資料表空間重新命名。如需詳細資訊，包括您可命名資料表空間的限制，請參閱 [rdsadmin.rename\$1tablespace](db2-sp-managing-tablespaces.md#db2-sp-rename-tablespace)。

   ```
   db2 "call rdsadmin.rename_tablespace(
       'database_name', 
       'source_tablespace_name',
       'target_tablespace_name')"
   ```

## 捨棄資料表空間
<a name="db2-dropping-tablespace"></a>

若要捨棄 RDS for Db2 資料庫的資料表空間，請呼叫 `rdsadmin.drop_tablespace` 預存程序。在捨棄資料表空間之前，請先捨棄資料表空間中的任何物件，例如資料表、索引或大型物件 LOB)。如需詳細資訊，請參閱 IBM Db2 文件中的[捨棄資料表空間](https://www.ibm.com/docs/en/db2/11.5?topic=spaces-dropping-table)。

**捨棄資料表空間**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 `rdsadmin` 資料庫。在下列範例中，將 *master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 "connect to rdsadmin user master_username using master_password"
   ```

1. 呼叫 `rdsadmin.drop_tablespace` 以捨棄資料表空間。如需詳細資訊，請參閱[rdsadmin.drop\$1tablespace](db2-sp-managing-tablespaces.md#db2-sp-drop-tablespace)。

   ```
   db2 "call rdsadmin.drop_tablespace(
       'database_name', 
       'tablespace_name')"
   ```

## 檢查資料表空間的狀態
<a name="db2-checking-tablespaces-procedure"></a>

 您可以使用 `cast` 函數來檢查資料表空間的狀態。

**檢查資料表空間的狀態**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 Db2 資料庫。在下列範例中，將 *rds\$1database\$1alias*、*master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 connect to rds_database_alias user master_username using master_password
   ```

1. 傳回摘要輸出。

   針對摘要輸出：

   ```
   db2 "select cast(tbsp_id as smallint) as tbsp_id,
   cast(tbsp_name as varchar(35)) as tbsp_name,
   cast(tbsp_type as varchar(3)) as tbsp_type,
   cast(tbsp_state as varchar(10)) as state,
   cast(tbsp_content_type as varchar(8)) as contents from table(mon_get_tablespace(null,-1)) order by tbsp_id"
   ```

## 傳回資料表空間的詳細資訊
<a name="db2-tablespaces-info-db2pd"></a>

您可以使用 `cast` 函數傳回一個成員或所有成員的資料表空間相關資訊。

**傳回資料表空間的詳細資訊**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 Db2 資料庫。在下列範例中，將 *rds\$1database\$1alias*、*master\$1username* 和 *master\$1password* 取代為您自己的資訊。

   ```
   db2 connect to rds_database_alias user master_username using master_password
   ```

1. 針對一個成員或所有成員，傳回資料庫中所有資料表空間的詳細資訊。

   針對一個成員：

   ```
   db2 "select cast(member as smallint) as member,
   cast(tbsp_id as smallint) as tbsp_id,
   cast(tbsp_name as varchar(35)) as tbsp_name,
   cast(tbsp_type as varchar(3)) as tbsp_type,
   cast(tbsp_state as varchar(10)) as state,
   cast(tbsp_content_type as varchar(8)) as contents,
   cast(tbsp_total_pages as integer) as total_pages,
   cast(tbsp_used_pages as integer) as used_pages,
   cast(tbsp_free_pages as integer) as free_pages,
   cast(tbsp_page_top as integer) as page_hwm,
   cast(tbsp_page_size as integer) as page_sz,
   cast(tbsp_extent_size as smallint) as extent_sz,
   cast(tbsp_prefetch_size as smallint) as prefetch_sz,
   cast(tbsp_initial_size as integer) as initial_size,
   cast(tbsp_increase_size_percent as smallint) as increase_pct,
   cast(storage_group_name as varchar(12)) as stogroup from table(mon_get_tablespace(null,-1)) order by member, tbsp_id "
   ```

   針對所有成員：

   ```
   db2 "select cast(member as smallint) as member
   cast(tbsp_id as smallint) as tbsp_id,
   cast(tbsp_name as varchar(35)) as tbsp_name,
   cast(tbsp_type as varchar(3)) as tbsp_type,
   cast(tbsp_state as varchar(10)) as state,
   cast(tbsp_content_type as varchar(8)) as contents,
   cast(tbsp_total_pages as integer) as total_pages,
   cast(tbsp_used_pages as integer) as used_pages,
   cast(tbsp_free_pages as integer) as free_pages,
   cast(tbsp_page_top as integer) as page_hwm,
   cast(tbsp_page_size as integer) as page_sz,
   cast(tbsp_extent_size as smallint) as extent_sz,
   cast(tbsp_prefetch_size as smallint) as prefetch_sz,
   cast(tbsp_initial_size as integer) as initial_size,
   cast(tbsp_increase_size_percent as smallint) as increase_pct,
   cast(storage_group_name as varchar(12)) as stogroup from table(mon_get_tablespace(null,-2)) order by member, tbsp_id "
   ```

## 列出資料表空間的狀態和儲存群組
<a name="db2-state-storage-group-tablespace-sql"></a>

您可以執行 SQL 陳述式，列出資料表空間的狀態和儲存群組。

若要列出資料表空間的狀態和儲存群組，請執行下列 SQL 陳述式：

```
db2 "SELECT varchar(tbsp_name, 30) as tbsp_name,
                  varchar(TBSP_STATE, 30) state,
                  tbsp_type,
                  varchar(storage_group_name,30) storage_group
FROM TABLE(MON_GET_TABLESPACE('',-2)) AS t"
```

## 列出資料表的資料表空間
<a name="db2-return-tablespaces-sql"></a>

您可以執行 SQL 陳述式來列出資料表的資料表空間。

若要列出資料表的資料表空間，請執行下列 SQL 陳述式。在下列範例中，將 *SCHEMA\$1NAME* 和 *TABLE\$1NAME* 取代為結構描述和資料表的名稱：

```
db2 "SELECT 
   VARCHAR(SD.TBSPACE,30) AS DATA_SPACE,
   VARCHAR(SL.TBSPACE,30) AS LONG_SPACE,
   VARCHAR(SI.TBSPACE,30) AS INDEX_SPACE
 FROM 
   SYSCAT.DATAPARTITIONS P 
   JOIN SYSCAT.TABLESPACES SD ON SD.TBSPACEID = P.TBSPACEID 
   LEFT JOIN SYSCAT.TABLESPACES SL ON SL.TBSPACEID = P.LONG_TBSPACEID
   LEFT JOIN SYSCAT.TABLESPACES SI ON SI.TBSPACEID = P.INDEX_TBSPACEID
 WHERE
    TABSCHEMA = 'SCHEMA_NAME'
 AND TABNAME   = 'TABLE_NAME'"
```

## 列出資料表空間容器
<a name="db2-listing-tablespace-containers"></a>

您可以使用 `cast` 命令來列出所有資料表空間容器或特定資料表空間容器。

**列出資料表空間的資料表空間容器**

1. 使用 RDS for Db2 資料庫執行個體的主使用者名稱和主密碼連線至 Db2 資料庫。在下列範例中，將 *rds\$1database\$1alias*、*master\$1username* 和 *master\$1password* 取代為您自己的資訊：

   ```
   db2 connect to rds_database_alias user master_username using master_password
   ```

1. 傳回資料庫中所有資料表空間容器或特定資料表空間容器的清單。

   針對所有資料表空間容器：

   ```
   db2 "select cast(member as smallint) as member,
   cast(tbsp_name as varchar(35)) as tbsp_name,
   cast(container_id as smallint) as id,
   cast(container_name as varchar(60)) as container_path, container_type as type from table(mon_get_container(null,-2)) order by member,tbsp_id,container_id"
   ```

   針對特定資料表空間容器：

   ```
   db2 "select cast(member as smallint) as member,
   cast(tbsp_name as varchar(35)) as tbsp_name,
   cast(container_id as smallint) as id,
   cast(container_name as varchar(60)) as container_path, container_type as type from table(mon_get_container('TBSP_1',-2)) order by member, tbsp_id,container_id"
   ```