w3resource

Split Orders into North and South Region Partitions


Creating Partitions for a List-Partitioned Table

Write a PostgreSQL query to create partitions for Orders that store data for "North" and "South" regions.

Solution:

-- Create a partition named Orders_North as a partition of the Orders table
CREATE TABLE Orders_North PARTITION OF Orders
-- Specify that this partition includes rows where region is 'North'
FOR VALUES IN ('North');

-- Create a partition named Orders_South as a partition of the Orders table
CREATE TABLE Orders_South PARTITION OF Orders
-- Specify that this partition includes rows where region is 'South'
FOR VALUES IN ('South');

Explanation:

  • Purpose of the Query:
    • Divides order data into separate partitions for North and South regions.
  • Key Components:
    • FOR VALUES IN (...): Specifies the region values stored in each partition.
  • Real-World Application:
    • Helps businesses efficiently query and manage region-specific data.

Notes:

  • Queries can be optimized using partition pruning.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to create partitions for a List-Partitioned table for order statuses such as 'Pending', 'Shipped', and 'Delivered'.
  • Write a PostgreSQL query to create a partition for a List-Partitioned table that handles region values like 'East' and 'West'.
  • Write a PostgreSQL query to create partitions for a List-Partitioned table that stores product categories like 'Electronics', 'Clothing', and 'Furniture'.
  • Write a PostgreSQL query to create partitions for a List-Partitioned table that divides data based on a set of country codes.


Go to:


PREV : Creating a List-Partitioned Table.
NEXT : Creating a Hash-Partitioned Table.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.