すべてのプロダクト
Search
ドキュメントセンター

PolarDB:パーティションテーブルの任意の列を主キーまたは外部キーとして設定する

最終更新日:Sep 26, 2024

PolarDB for PostgreSQL (Compatible with Oracle) では、パーティションテーブル内の任意の列をプライマリキーまたは外部キーとして指定できます。

前提条件

PolarDB for PostgreSQL (Oracle互換) クラスターのリビジョンバージョンはV1.1.35以降です。

使用上の注意

  • 指定した主キーにすべてのパーティションキーが含まれている場合、主キーのインデックスとしてローカルインデックスが使用されます。 それ以外の場合、グローバルインデックスが主キーのインデックスとして使用されます。

  • 指定した外部キーにすべてのパーティションキーが含まれている場合、一意の制約のインデックスとしてローカルインデックスが使用されます。 そうでない場合、グローバルインデックスが一意の制約のインデックスとして使用されます。

  • ALTER TABLE xxx ADD PRIMARY KEY USING index xxxステートメントでは、グローバルインデックスのみを使用できます。 ローカルインデックスは制約を満たさない可能性があるため、ステートメントでローカルインデックスを使用することはできません。 代わりに、一意のキーがステートメントに追加されます。

  • 外部キーを参照するテーブルのパーティション間でデータを更新すると、エラーが発生することがあります。 パーティション間でデータを更新すると、削除操作と挿入操作が実行されます。 削除操作中に、システムは行が参照されているかどうかをチェックします。

パーティションテーブルの主キーの指定

CREATE TABLE sale
(
  dept_no     number primary key,
  part_no     varchar2,
  country     varchar2(20),
  date        date,
  amount      number unique 
)
PARTITION BY RANGE(date)
  SUBPARTITION BY LIST(country)
  (
    PARTITION q1_2012
      VALUES LESS THAN('2012-Apr-01')
      (
        SUBPARTITION q1_europe VALUES ('FRANCE', 'ITALY'),
        SUBPARTITION q1_asia VALUES ('INDIA', 'PAKISTAN'),
        SUBPARTITION q1_americas VALUES ('US', 'CANADA')
      ),
  PARTITION q2_2012
    VALUES LESS THAN('2012-Jul-01')
      (
        SUBPARTITION q2_europe VALUES ('FRANCE', 'ITALY'),
        SUBPARTITION q2_asia VALUES ('INDIA', 'PAKISTAN'),
        SUBPARTITION q2_americas VALUES ('US', 'CANADA')
      ),
  PARTITION q3_2012
    VALUES LESS THAN('2012-Oct-01')
      (
        SUBPARTITION q3_europe VALUES ('FRANCE', 'ITALY'),
        SUBPARTITION q3_asia VALUES ('INDIA', 'PAKISTAN'),
        SUBPARTITION q3_americas VALUES ('US', 'CANADA')
      ),
  PARTITION q4_2012
    VALUES LESS THAN('2013-Jan-01')
      (
        SUBPARTITION q4_europe VALUES ('FRANCE', 'ITALY'),
        SUBPARTITION q4_asia VALUES ('INDIA', 'PAKISTAN'),
        SUBPARTITION q4_americas VALUES ('US', 'CANADA')
      )
);

\d sales
                    Data table "public.sales"
  Field | Type | Collation | Nullable | Default 
---------+-----------------------+----------+----------+------
 dept_no | numeric               |          | not null | 
 part_no | character varying     |          |          | 
 country | character varying(20) |          |          | 
 date    | date                  |          |          | 
 amount  | numeric               |          |          | 
Partition key value: RANGE (date) NULLS LAST
Index:
    "sales_pkey" PRIMARY KEY, btree (dept_no) GLOBAL
Number of partitions: 4 (you can use the \d command to list the partitions)

外部キーによるパーティション分割テーブルの一意の制約の参照

CREATE TABLE shipments
( order_id      NUMBER NOT NULL
, order_date    DATE NOT NULL
, delivery_date DATE NOT NULL
, customer_id   NUMBER NOT NULL
, sales_amount  NUMBER NOT NULL REFERENCES sales(amount)
, PRIMARY KEY (order_date, order_id, delivery_date)
)
PARTITION BY RANGE (order_date) 
(
  PARTITION p_2006_aug VALUES LESS THAN (TO_DATE('01-SEP-2006','dd-MON-yyyy'))
, PARTITION p_2006_sep VALUES LESS THAN (TO_DATE('01-OCT-2006','dd-MON-yyyy'))
, PARTITION p_2006_oct VALUES LESS THAN (TO_DATE('01-NOV-2006','dd-MON-yyyy'))
, PARTITION p_2006_nov VALUES LESS THAN (TO_DATE('01-DEC-2006','dd-MON-yyyy'))
, PARTITION p_2006_dec VALUES LESS THAN (TO_DATE('01-JAN-2007','dd-MON-yyyy'))
);

\d shipments
              Data table "public.shipments"
     Field | Type | Collation | Nullable | Default 
---------------+---------+----------+----------+------
 order_id      | numeric |          | not null | 
 order_date    | date    |          | not null | 
 delivery_date | date    |          | not null | 
 customer_id   | numeric |          | not null | 
 sales_amount  | numeric |          | not null | 
Partition key value: RANGE (order_date) NULLS LAST
Index:
    "shipments_pkey" PRIMARY KEY, btree (order_date, order_id, delivery_date)
Foreign key constraints:
    "shipments_sales_amount_fkey" FOREIGN KEY (sales_amount) REFERENCES sales(amount)
    "shipments_sales_amount_fkey1" FOREIGN KEY (sales_amount) REFERENCES sales_q1_2012(amount)
    "shipments_sales_amount_fkey10" FOREIGN KEY (sales_amount) REFERENCES sales_q3_americas(amount)
    "shipments_sales_amount_fkey11" FOREIGN KEY (sales_amount) REFERENCES sales_q3_europe(amount)
    "shipments_sales_amount_fkey12" FOREIGN KEY (sales_amount) REFERENCES sales_q3_asia(amount)
    "shipments_sales_amount_fkey13" FOREIGN KEY (sales_amount) REFERENCES sales_q4_2012(amount)
    "shipments_sales_amount_fkey14" FOREIGN KEY (sales_amount) REFERENCES sales_q4_americas(amount)
    "shipments_sales_amount_fkey15" FOREIGN KEY (sales_amount) REFERENCES sales_q4_europe(amount)
    "shipments_sales_amount_fkey16" FOREIGN KEY (sales_amount) REFERENCES sales_q4_asia(amount)
    "shipments_sales_amount_fkey2" FOREIGN KEY (sales_amount) REFERENCES sales_q1_americas(amount)
    "shipments_sales_amount_fkey3" FOREIGN KEY (sales_amount) REFERENCES sales_q1_europe(amount)
    "shipments_sales_amount_fkey4" FOREIGN KEY (sales_amount) REFERENCES sales_q1_asia(amount)
    "shipments_sales_amount_fkey5" FOREIGN KEY (sales_amount) REFERENCES sales_q2_2012(amount)
    "shipments_sales_amount_fkey6" FOREIGN KEY (sales_amount) REFERENCES sales_q2_americas(amount)
    "shipments_sales_amount_fkey7" FOREIGN KEY (sales_amount) REFERENCES sales_q2_europe(amount)
    "shipments_sales_amount_fkey8" FOREIGN KEY (sales_amount) REFERENCES sales_q2_asia(amount)
    "shipments_sales_amount_fkey9" FOREIGN KEY (sales_amount) REFERENCES sales_q3_2012(amount)
Number of partitions: 5 (you can use the \d command to list the partitions)