Docsity

Adding Columns-Data Warehouse-Lab Mannual, Lecture notes of Data Warehousing

Toggle sidebar
bg1
Chapter 10: Adding Columns
This chapter teaches you the most commonly encountered extension of a starter data warehouse:
adding columns to an existing dimension and the fact table. This chapter starts by discussing
what should happen to the schema if you need to add a new column or two. It then proceeds by
demonstrating how you can add a new column to the customer dimension and the sales order fact
and apply SCD2 to the new column.
Enhancing the Schema
In this section I show you how to revise the data warehouse schema. Figure 10.1 shows the
enhanced schema with new columns in the customer_dim table and the sales_order_fact table.
The new columns in customer_dim are shipping_address, shipping_zip_code, shipping_city,
and shipping_state. The sales_order_fact table has one new column: order_quantity.
The shipping_address.sql script in Listing 10.1 adds the new columns in the customer_dim and
customer_stg tables. Before you run the script, set your MySQL date to March 1, 2007.
Figure 10.1: New columns in customer_dim and sales_order_fact
Listing 10.1: Adding new columns to the customer dimension
/*****************************************************************/
/* */
/* shipping_address.sql */
/* */
/*****************************************************************/
USE dw;
ALTER TABLE customer_dim
ADD shipping_address CHAR (50) AFTER customer_state
, ADD shipping_zip_code INT (5) AFTER shipping_address
, ADD shipping_city CHAR (30) AFTER shipping_zip_code
, ADD shipping_state CHAR (2) AFTER shipping_city
;
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Adding Columns-Data Warehouse-Lab Mannual and more Lecture notes Data Warehousing in PDF only on Docsity!

Chapter 10: Adding Columns

This chapter teaches you the most commonly encountered extension of a starter data warehouse:

adding columns to an existing dimension and the fact table. This chapter starts by discussing

what should happen to the schema if you need to add a new column or two. It then proceeds by

demonstrating how you can add a new column to the customer dimension and the sales order fact

and apply SCD2 to the new column.

Enhancing the Schema

In this section I show you how to revise the data warehouse schema. Figure 10.1 shows the

enhanced schema with new columns in the customer_dim table and the sales_order_fact table.

The new columns in customer_dim are shipping_address, shipping_zip_code, shipping_city,

and shipping_state. The sales_order_fact table has one new column: order_quantity.

The shipping_address.sql script in Listing 10.1 adds the new columns in the customer_dim and

customer_stg tables. Before you run the script, set your MySQL date to March 1, 2007.

Figure 10.1: New columns in customer_dim and sales_order_fact

Listing 10.1: Adding new columns to the customer dimension

/* shipping_address.sql / / */ /*****************************************************************/ USE dw; ALTER TABLE customer_dim ADD shipping_address CHAR (50) AFTER customer_state , ADD shipping_zip_code INT (5) AFTER shipping_address , ADD shipping_city CHAR (30) AFTER shipping_zip_code , ADD shipping_state CHAR (2) AFTER shipping_city ;

ALTER TABLE customer_stg ADD shipping_address CHAR (50) AFTER customer_state , ADD shipping_zip_code INT (5) AFTER shipping_address , ADD shipping_city CHAR (30) AFTER shipping_zip_code , ADD shipping_state CHAR (2) AFTER shipping_city ; /* end of script */

You run the above script by using this command.

mysql> . c:\mysql\scripts\shipping_address.sql

This message will be printed on the MySQL console.

Database changed Query OK, 9 rows affected (0.73 sec) Records: 9 Duplicates: 0 Warnings: 0 Query OK, 8 rows affected (0.49 sec) Records: 8 Duplicates: 0 Warnings: 0

To confirm that the new columns were added, query the customer_dim table.

mysql> select * from customer_dim \G

Here is the query result.

*************************** 1. row *************************** customer_sk: 1 customer_number: 1 customer_name: Really Large Customers customer_street_address: 7500 Louise Dr. customer_zip_code: 17050 customer_city: Mechanicsburg customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2005 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 2. row *************************** customer_sk: 2 customer_number: 2 customer_name: Small Stores customer_street_address: 2500 Woodland St. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL

effective date: 2005 – 03 – 01 expiry_date: 2007 – 02 – 28 *************************** 7. row *************************** customer_sk: 7 customer_number: 7 customer_name: Distinguished Agencies customer_street_address: 9999 Scott St. customer_zip_code: 17050 customer_city: Mechanicsburg customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective date: 2005 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 8. row *************************** customer_sk: 8 customer_number: 6 customer_name: Extremely Loyal Clients customer_street_address: 7777 Ritter Rd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2007 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 9. row *************************** customer_sk: 9 customer_number: 8 customer_name: Subsidiaries customer_street_address: 10000 Wetline Blvd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2007 – 03 – 01 expiry_date: 9999 – 12 – 31 9 rows in set (0.00 sec)

Note The new columns are not populated yet, so their values are NULL.

The order_quantity.sql script in Listing 10.2 adds the order_quantity column to the

sales_prder_fact table.

Listing 10.2: Adding the order_quantity column

/* order_quantity.sql / / */

USE dw; ALTER TABLE sales_order_fact ADD order_quantity INT AFTER order_amount ; /* end of script */

You run the above script by using this command.

mysql> . c:\mysql\scripts\order_quantity.sql

Here is what you see on the console after you press Enter.

Database changed Query OK, 35 rows affected (0.56 sec) Records: 35 Duplicates: 0 Warnings: 0

To confirm that the new column was added, query the sales_order_fact table.

mysql> select order_sk osk, customer_sk csk, product_sk psk, order_date_sk odsk, -> order_amount amt, order_quantity qty -> from sales_order_fact;

Let’s also assume that the order quantity is made available in the sales orders source data and the

user agrees sales orders already in the data warehouse will not be updated.

Listing 10.3: Revised daily DW regular population

/* dw_regular_10.sql / / / /*****************************************************************/ USE dw; / CUSTOMER_DIM POPULATION / TRUNCATE customer_stg; LOAD DATA INFILE 'customer.csv' INTO TABLE customer_stg FIELDS TERMINATED BY ' , ' OPTIONALLY ENCLOSED BY ' " ' LINES TERMINATED BY '\r\n' IGNORE 1 LINES ( customer_number , customer_name , customer_street_address , customer_zip_code , customer_city , customer_state , shipping_address , shipping_zip_code , shipping_city , shipping_state ) ; / SCD 2 ON ADDRESSES */ UPDATE customer_dim a , customer_stg b SET a.expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.customer_number = b.customer_number AND ( a.customer_street_address <> b.customer_street_address OR a.customer_city <> b.customer_city OR a.customer_zip_code <> b.customer_zip_code OR a.customer_state <> b.customer_state OR a.shipping_address <> b.shipping_address OR a.shipping_city <> b.shipping_city OR a.shipping_zip_code <> b.shipping_zip_code OR a.shipping_state <> b.shipping_state OR a.shipping_address IS NULL OR a.shipping_city IS NULL OR a.shipping_zip_code IS NULL OR a.shipping_state IS NULL) AND expiry_date = '9999-12-31'

INSERT INTO customer_dim SELECT NULL , b.customer_number , b.customer_name , b.customer_street_address , b.customer_zip_code , b.customer_city , b.customer_state , b.shipping_address , b.shipping_zip_code , b.shipping_city , b.shipping_state , CURRENT_DATE , '9999-12-31' FROM customer_dim a , customer_stg b WHERE a.customer_number = b.customer_number AND ( a.customer_street_address <> b.customer_street_address OR a.customer_city <> b.customer_city OR a.customer_zip_code <> b.customer_zip_code OR a.customer_state <> b.customer_state OR a.shipping_address <> b.shipping_address OR a.shipping_city <> b.shipping_city OR a.shipping_zip_code <> b.shipping_zip_code OR a.shipping_state <> b.shipping_state OR a.shipping_address IS NULL OR a.shipping_city IS NULL OR a.shipping_zip_code IS NULL OR a.shipping_state IS NULL) AND EXISTS ( SELECT * FROM customer_dim x WHERE b.customer_number = x.customer_number AND a.expiry_date = SUBDATE (CURRENT_DATE, 1)) AND NOT EXISTS ( SELECT * FROM customer_dim y WHERE b.customer_number = y.customer_number AND y.expiry_date = '9999– 12 – 31') ; /* END OF SCD 2 / / SCD 1 ON NAME */ UPDATE customer_dim a, customer_stg b SET a.customer_name = b.customer_name WHERE a.customer_number = b.customer_number AND a.expiry_date = '9999– 12 – 31' AND a.customer_name <> b.customer_name ;

INSERT INTO product_dim SELECT NULL , b.product_code , b.product_name , b.product_category , CURRENT_DATE , '9999– 12 – 31' FROM product_dim a , product_stg b WHERE a.product_code = b.product_code AND ( a.product_name <> b.product_name OR a.product_category <> b.product_category ) AND EXISTS ( SELECT * FROM product_dim x WHERE b.product_code = x.product_code AND a.expiry_date = SUBDATE (CURRENT_DATE, 1) AND NOT EXISTS ( SELECT * FROM product_dim y WHERE b.product_code = y.product_code AND y.expiry_date = '9999– 12 – 31') ; /* END OF SCD 2 / / ADD NEW PRODUCT / INSERT INTO product_dim SELECT NULL , product_code , product_name , product_category , CURRENT_DATE , '9999– 12 – 31' FROM product_stg WHERE product_code NOT IN( SELECT y.product_code FROM product_dim x, product_stg y WHERE x.product_code = y.product_code ) ; / END OF PRODUCT_DIM POPULATION / / ORDER_DIM POPULATION */ INSERT INTO order_dim ( order_sk , order_number , effective_date , expiry_date

SELECT

NULL

, order_number , order_date , '9999– 12 – 31' FROM source.sales_order WHERE entry_date = CURRENT_DATE ; /* END OF ORDER_DIM POPULATION / / SALES_ORDER_FACT POPULATION / INSERT INTO sales_order_fact SELECT order_sk , customer_sk , product_sk , date_sk , order_amount , order_quantity FROM source.sales order a , order_dim b , customer_dim c , product_dim d , date_dim e WHERE a.order_number = b.order_number AND a.customer_number = c.customer_number AND a.order_date >= c.effective_date AND a.order_date <= c.expiry_date AND a.product_code = d.product_code AND a.order_date >= d.effective_date AND a.order_date <= d.expiry_date AND a.order_date = e.date AND a.entry_date = CURRENT_DATE ; / end of script */

The next section provides instructions on how to prepare the data required for the script in

Listing 10.3 to run successfully.

Testing

Before you can run the script in Listing 10.3, there are a few things you need to do.

First, prepare the customer data as shown below and save it as customer.csv. The changes from

the previous customer data are:

Database changed Query OK, 37 rows affected (0.39 sec) Records: 37 Duplicates: 0 Warnings: 0

Now that the sales order source has the order_quantity column, you can add sales order test

data. The script in Listing 10.5 adds nine sales orders into the sales_order table. Note that these

sales orders have order quantities and their order dates are March 2, 2005.

Listing 10.5: Adding nine sales orders with order quantities

/* sales_order_quantity_data.sql / / / /*****************************************************************/ USE source; INSERT INTO sales_order VALUES (38, 1, 1, '2007-03-02', '2007-03-02', 1000, 10) , (39, 2, 2, '2007-03-02', '2007-03-02', 2000, 20) , (40, 3, 3, '2007-03-02', '2007-03-02', 4000, 40) , (41, 4, 4, '2007-03-02', '2007-03-02', 6000, 60) , (42, 5, 1, '2007-03-02', '2007-03-02', 2500, 25) , (43, 6, 2, '2007-03-02', '2007-03-02', 5000, 50) , (44, 7, 3, '2007-03-02', '2007-03-02', 7500, 75) , (45, 8, 4, '2007-03-02', '2007-03-02', 1000, 10) , (46, 9, 1, '2007-03-02', '2007-03-02', 1000, 10) ; / end of script */

Next, you need to set your MySQL date to the order date of your test data, which is March 2,

Now, you are ready to run the dw_regular_10.sql script in Listing 10.3. You can invoke it using

this command.

mysql> . c:\mysql\scripts\dw_regular_10.sql

Here is what you'll see on the console.

Database changed Query OK, 8 rows affected (0.05 sec) Query OK, 9 rows affected (0.06 sec) Records: 9 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 8 rows affected (0.06 sec) Rows matched: 8 Changed: 8 Warnings: 0 Query OK, 8 rows affected (0.05 sec) Records: 8 Duplicates: 0 Warnings: 0

Query OK, 0 rows affected (0.01 sec) Rows matched: 0 Changed: 0 Warnings: 0 Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 4 rows affected (0.05 sec) Query OK, 4 rows affected (0.06 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 Query OK, 9 rows affected (0.07 sec) Records: 9 Duplicates: 0 Warnings: 0 Query OK, 9 rows affected (0.10 sec) Records: 9 Duplicates: 0 Warnings: 0

You can confirm if the revised regular population script was successfully executed by querying

the customer dimension using this SQL statement.

mysql> select customer_number no, customer_name name, -> shipping_city, shipping_zip_code zip, shipping_state st, -> effective_date eff, expiry_date exp -> from customer_dim \G

A successful execution of the revised regular population script gives you the following result.

******************************1.row************************ no: 1 name: Really Large Customers shipping_city: NULL zip: NULL st: NULL eff: 2005-03- exp: 2007-03- ******************************2. row ************************* no: 2 name: Small Stores shipping_city: NULL zip: NULL st: NULL eff: 2005-03- exp: 2007-03- ******************************3. row ************************* no: 3

shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 11. row *************************** no: 2 name: Small Stores shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 12. row *************************** no: 3 name: Medium Retailers shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 13. row *************************** no: 4 name: Good Companies shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 14. 4 row *************************** no: 5 name: Wonderful Shops shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 15. 5 row *************************** no: 6 name: Extremely Loyal Clients shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 16. row *************************** no: 7 name: Distinguished Agencies shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 17. row *************************** no: 8 name: Subsidiaries shipping_city: Pittsburgh

zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 18. row *************************** no: 9 name: Online Distributors shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- 18 rows in set (0.00 sec)

Note The new records of all existing customers have shipping addresses. The older (expired)

records do not. Customer number 9 is added and it has a shipping address.

To confirm that the sales data has been populated successfully, query the sales_order_fact table

using this statement.

mysql> select order_sk o_sk, customer_sk c_sk, product_sk p_sk, order_date_sk od_sk, -> order_amount amt, order_quantity qty -> from sales_order_fact;

Summary

In this chapter you learned to extend a data warehouse by adding new columns to both dimension

and fact tables. In the next chapter you will look into another type of population, on-demand

population, which you likely need to implement in response to the growth of a data warehouse.