Wednesday, September 3, 2008

Examples of Star Schema

Here's an example of a star schema (in terms of its tables' DDL) that has an sales fact table and its four dimension tables: order, customer, product, and date.

CREATE TABLE sales_fact (order_sk INT, customer_sk INT, product_sk INT, date_sk INT, sales_amount DEC(10,2), order_quantity INT);
CREATE TABLE order_dim (order_sk INT, order_num INT);
CREATE TABLE customer_dim (customer_sk INT, customer_name CHAR(50));
CREATE TABLE product_dim (product_sk INT, product_code INT, product_name CHAR(50));
CREATE TABLE date_dim (date_sk INT, date DATE);

We can add another fact table (production_fact), which makes our schema a two-star schema:

CREATE TABLE production_fact (product_sk INT, factory_sk INT, date_sk INT, production_quantity INT);
CREATE TABLE factory_dim (factory_sk INT, factory_code INT, factory_name CHAR(50));

Note that this 2nd star (its production_fact table) shares the product_dim and date_dim tables from the 1st schema.

No comments: