日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql维表的代理键字段_mysql多维数据仓库指南--第三篇第12章(2)

發布時間:2025/3/8 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql维表的代理键字段_mysql多维数据仓库指南--第三篇第12章(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

賓夕法尼亞州地區客戶維

在本節我將用賓夕法尼亞州地區客戶的子集維度來解釋第二種維度子集的類型。我也將向你說明如何測試該子集維度。

相對的,一個向上鉆取的維包含了它基礎維的所有更高級別的數據。而一個特定子集維度則選擇了它基礎維的某個特定的數據集合。列表12-3所示的腳本產生并加載了賓夕法尼亞州(PA)地區客戶子集維。

注意到,有兩個事情是賓夕法尼亞州地區客戶子集維區別于月份子集維的地方。

npa_customer_dim表和customer_dim表的字段結構一樣,而month_dim表沒有 date_dim表中的日期字段。

npa_customer_dim表的代理鍵是客戶表的代理鍵。Month_dim表的代理鍵只屬于month_dim表,并不是來自日期維表。

列表12-3 PA的客戶:

/**********************************************************************/

/**/

/* pa_customer.sql*/

/**/

/**********************************************************************/

USE dw;

CREATE TABLE pa_customer_dim

( customer_sk INT NOT NULL AUTO_INCREMENT PRIMARY KEY

, customer_number INT

, customer_name CHAR (50)

, customer_street_address CHAR (50)

, customer_zip_code INT (5)

, customer_city CHAR (30)

, customer_state CHAR (2)

, shipping_address CHAR (50)

, shipping_zip_code INT (5)

, shipping_city CHAR (30)

, shipping_state CHAR (2)

, effective_date DATE

, expiry_date DATE )

;

INSERT INTO pa_customer_dim

SELECT

customer_sk

, customer_number

, customer_name

, customer_street_address

, customer_zip_code

, customer_city

, customer_state

, shipping_address

, shipping_zip_code

, shipping_city

, shipping_state

, effective_date

, expiry_date

FROM customer_dim

WHERE customer_state = 'PA'

;

/* end of script*/

為了測試PA子維腳本,你需要先用列表12-4的腳本來增加三個居住于俄亥俄州的客戶。

列表12-4:非PA客戶:

/**********************************************************************/

/**/

/* non_pa_customer.sql*/

/**/

/**********************************************************************/

/* default to dw*/

USE dw;

INSERT INTO customer_dim

( customer_sk

, customer_number

, customer_name

, customer_street_address

, customer_zip_code

, customer_city

, customer_state

, shipping_address

, shipping_zip_code

, shipping_city

, shipping_state

, effective_date

, expiry_date )

VALUES

(NULL, 10, 'Bigger Customers', '7777 Ridge Rd.', '44102',

'Cleveland', 'OH', '7777 Ridge Rd.', '44102', 'Cleveland',

'OH', CURRENT_DATE, '9999-12-31')

, (NULL, 11, 'Smaller Stores', '8888 Jennings Fwy.', '44102',

'Cleveland', 'OH', '8888 Jennings Fwy.', '44102',

'Cleveland', 'OH', CURRENT_DATE, '9999-12-31')

, (NULL, 12, 'Small-Medium Retailers', '9999 Memphis Ave.', '44102',

'Cleveland', 'OH', '9999 Memphis Ave.', '44102', 'Cleveland',

'OH', CURRENT_DATE, '9999-12-31')

;

/* end of script*/

用以下命令運行列表12-4中的腳本。

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

在你的控制臺上將得到下面的響應信息:

Database changed

Query OK, 3 rows affected (0.86 sec)

Records: 3Duplicates: 0Warnings: 0

現在你已經準備好運行列表12-3中的pa_customer.sql腳本,在你做這些之前,確定你的Msql數據庫的日期仍然是2007-03-02。

你可以用如下命令方式運行pa_customer.sql腳本。

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

你將在控制臺上看到:

Database changed

Query OK, 0 rows affected (0.20 sec)

Query OK, 18 rows affected (0.08 sec)

Records: 18 Duplicates: 0 Warnings: 0

為了確保三個OH客戶已經成功的載入,查詢customer_dim表:

mysql> select customer_name, customer_state, effective_date from customer_dim;

控制臺上將看到:

+---------------------------------+----------------+---------------+

| customer_name| customer_state | effective_date|

+---------------------------------+----------------+---------------+

| Really Large Customers| PA| 2005-03-01|

| Small Stores| PA| 2005-03-01|

| Medium Retailers| PA| 2005-03-01|

| Good Companies| PA| 2005-03-01|

| Wonderful Shops| PA| 2005-03-01|

| Extremely Loyal Clients| PA| 2005-03-01|

| Distinguished Agencies| PA| 2005-03-01|

| Extremely Loyal Clients| PA| 2007-03-01|

| Subsidiaries| PA| 2007-03-01|

| Really Large Customers| PA| 2007-03-02|

| Small Stores| PA| 2007-03-02|

| Medium Retailers| PA| 2007-03-02|

| Good Companies| PA| 2007-03-02|

| Wonderful Shops| PA| 2007-03-02|

| Extremely Loyal Clients| PA| 2007-03-02|

| Distinguished Agencies| PA| 2007-03-02|

| Subsidiaries| PA| 2007-03-02|

| Online Distributors| PA| 2007-03-02|

| Bigger Customers| OH| 2007-03-02|

| Smaller Stores| OH| 2007-03-02|

| Small-Medium Retailers| OH| 2007-03-02|

+---------------------------------+----------------+---------------+

21 rows in set (0.00 sec)

現在查詢pa_customer_dim表來確定只有PA的客戶在PA客戶維表中。

mysql> select customer_name, customer_state, effective_date from pa_customer_dim;

結果如下:

+---------------------------------+----------------+---------------+

| customer_name| customer_state | effective_date|

+---------------------------------+----------------+---------------+

| Really Large Customers| PA| 2004-01-01|

| Small Stores| PA| 2004-01-01|

| Medium Retailers| PA| 2004-01-01|

| Good Companies| PA| 2004-01-01|

| Wonderful Shops| PA| 2004-01-01|

| Extremely Loyal Clients| PA| 2004-01-01|

| Distinguished Agencies| PA| 2004-01-01|

| Extremely Loyal Clients| PA| 2005-11-01|

| Subsidiaries| PA| 2005-11-01|

| Really Large Customers| PA| 2005-11-03|

| Small Stores| PA| 2005-11-03|

| Medium Retailers| PA| 2005-11-03|

| Good Companies| PA| 2005-11-03|

| Wonderful Shops| PA| 2005-11-03|

| Extremely Loyal Clients| PA| 2005-11-03|

| Distinguished Agencies| PA | 2005-11-03|

| Subsidiaries| PA| 2005-11-03|

| Online Distributors| PA| 2005-11-03|

+---------------------------------+----------------+---------------+

18 rows in set (0.00 sec)

正如你所看到的,只有PA客戶進入到該表中,之前加入的OH客戶并沒有在其中。

修改定期裝載

當一個新的PA客戶資料加入到客戶維中時,為了能夠同時加入PA客戶維表,你需要把PA客戶子集維的裝載合并到數據倉庫的定期裝載過程中。修改后的定期裝載腳本將在列表12-5中列出。這個改變(新增)用粗體顯示。注意到,當每次你運行每日的定期裝載腳本時,該腳本重建(截斷,然后加入所有的PA客戶)了PA客戶子集維。

列表12-5:修正后的每日DW定期裝載

/******************************************************************/

/**/

/* dw_regular_12.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

WHEREb.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

WHEREa.customer_number = b.customer_number

AND a.expiry_date - '9999-12-31'

AND a.customer_name <> b.customer_name

;

/* ADD NEW CUSTOMER*/

INSERT INTO customer_dim

SELECT

NULL

, customer_number

, customer_name

, customer_street_address

, customer_zip_code

, customer_city

, customer_state

, shipping_address

, shipping_zip_code

, shipping_city

, shipping_state

, CURRENT_DATE

, '9999-12-31'

FROM customer_stg

WHERE customer_number NOT IN(

SELECT a.customer_number

FROM

customer_dim a

, customer_stg b

WHERE b.customer_number = a.customer_number )

/* RE-BUILD PA CUSTOMER DIMENSION*/

TRUNCATE pa_customer_dim;

INSERT INTO pa_customer_dim

SELECT

customer_sk

, customer_number

, customer_name

, customer_street_address

, customer_zip_code

, customer_city

, customer_state

, shipping_address

, shipping_zip_code

, shipping_city

, shipping_state

, effective_date

, expiry_date

FROM customer_dim

WHERE customer_state = 'PA'

;

/* END OF CUSTOMER_DIM POPULATION*/

/* product dimension loading*/

TRUNCATE product_stg

;

LOAD DATA INFILE 'product.txt'

INTO TABLE product_stg

FIELDS TERMINATED BY ''

OPTIONALLY ENCLOSED BY ''

LINES TERMINATED BY '\r\n'

IGNORE 1 LINES

( product_code

, product_name

, product_category )

;

/* PRODUCT_DIM POPULATION*/

/* SCD2 ON PRODUCT NAME AND GROUP*/

UPDATE

product_dim a

, product_stg b

SET

expiry_date = SUBDATE (CURRENT_DATE, 1)

WHERE

a.product_code = b.product_code

AND (a.product_name <> b.product_name

OR a.product_category <> b.product_category )

AND expiry_date = '9999-12-31'

;

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*/

測試修正后的定期裝載

現在你可以測試列表12-5的腳本。在你實施之前,先增加一些客戶數據,通過運行列表12-6的腳本增加一個PA客戶和一個OH客戶到客戶維中。

列表12-6:增加兩個客戶

/******************************************************************/

/**/

/* two_more_customers.sql*/

/**/

/******************************************************************/

/* default to dw*/

USE dw;

INSERT INTO customer_dim

( customer_sk

, customer_number

, customer_name

, customer_street_address

, customer_zip_code

, customer_city

, customer_state

, shipping_address

, shipping_zip_code

, shipping_city

, shipping_state

, effective_date

, expiry_date )

VALUES

(NULL, 13, 'PA Customer', '1111 Louise Dr.', '17050',

'Mechanicsburg', 'PA', '1111 Louise Dr.', '17050',

'Mechanicsburg', 'PA', CURRENT_DATE, '9999-12-31')

, (NULL, 14, 'OH Customer', '6666 Ridge Rd.', '44102',

'Cleveland', 'OH', '6666 Ridge Rd.', '44102',

'Cleveland', 'OH', CURRENT_DATE, '9999-12-31')

;

/* end of script*/

現在運行列表12-6中的腳本:

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

Mysql將顯示有兩個記錄生效。

Database changed

Query OK, 2 rows affected (0.06 sec)

Records: 2Duplicates: 0Warnings: 0

現在更改你的Msql數據庫的日期為2007-03-03以保證老的數據不會重新載入,然后運行dw_regular_12.sql腳本。

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

你將看到你的控制臺有如下顯示:

Database changed

Query OK, 9 rows affected (0.15 sec)

Query OK, 9 rows affected (0.14 sec)

Records: 9Deleted: 0Skipped: 0Warnings: 0

Query OK, 0 rows affected (0.05 sec)

Rows matched: 0Changed: 0Warnings: 0

Query OK, 0 rows affected (0.01 sec)

Records: 0Duplicates: 0Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Rows matched: 0Changed: 0Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Records: 0Duplicates: 0Warnings: 0

Query OK, 18 rows affected (0.04 sec)

Query OK, 19 rows affected (0.06 sec)

Records: 19Duplicates: 0Warnings: 0

Query OK, 4 rows affected (0.09 sec)

Query OK, 4 rows affected (0.07 sec)

Records: 4Deleted: 0Skipped: 0Warnings: 0

Query OK, 0 rows affected (0.06 sec)

Rows matched: 0Changed: 0Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Records: 0Duplicates: 0Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Records: 0Duplicates: 0Warnings: 0

Query OK, 0 rows affected (0.15 sec)

Records: 0 Duplicates: 0Warnings: 0

Query OK, 0 rows affected (0.17 sec)

Records: 0Duplicates: 0Warnings: 0

現在用該語句查詢pa_customer_dim表。你將看到新的PA客戶被插入到表中。

mysql> select customer_name, customer_state, effective_date from pa_customer_dim;

這是結果:

+-------------------------+----------------+---------------+

| customer_name| customer_state | effective_date|

+-------------------------+----------------+---------------+

| Really Large Customers| PA| 2005-03-01|

| Small Stores| PA| 2005-03-01|

| Medium Retailers| PA| 2005-03-01|

| Good Companies| PA| 2005-03-01|

| Wonderful Shops| PA| 2005-03-01|

| Extremely Loyal Clients | PA| 2005-03-01|

| Distinguished Agencies| PA| 2005-03-01|

| Extremely Loyal Clients | PA| 2007-03-01|

| Subsidiaries| PA| 2007-03-01|

| Really Large Customers| PA| 2007-03-02|

| Small Stores| PA| 2007-03-02|

| Medium Retailers| PA| 2007-03-02|

| Good Companies| PA| 2007-03-02|

| Wonderful Shops| PA| 2007-03-02|

| Extremely Loyal Clients | PA| 2007-03-02|

| Distinguished Agencies| PA| 2007-03-02|

| Subsidiaries| PA| 2007-03-02|

| Online Distributors| PA| 2007-03-02|

| PA Customer| PA| 2007-03-03|

+-------------------------+----------------+---------------+

19 rows in set (0.00 sec)

小結

在這章,你學習了兩種類型的子集維。月份子集維是向上鉆取維的一個例子,一個裝載自比它更為詳細的基礎維的具有更高級的維。PA客戶維是一個特定的子集維;只從它的基礎維選擇PA客戶數據進行裝載。

下一章,你將學習另外一個復用現有維的技術,叫角色扮演維度。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的mysql维表的代理键字段_mysql多维数据仓库指南--第三篇第12章(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。