-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdc_sql.sql
187 lines (171 loc) · 6.73 KB
/
cdc_sql.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-------------------------------------------------------------------------------------------------------
-- Step 1: Setting Up the Environment
-- We will create two tables representing the old and new versions of a dataset and populate them with dummy data.
-- The products_old table represents the original dataset.
-- The products_new table represents the updated dataset.
-------------------------------------------------------------------------------------------------------
-- Create the old version of the dataset
CREATE TABLE products_old (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(100),
price DECIMAL(10, 2),
updated_at DATE
);
-- Insert some dummy data into the old table
INSERT INTO products_old (product_id, product_name, category, price, updated_at) VALUES
(1, 'Laptop', 'Electronics', 800.00, '2023-01-01'),
(2, 'Smartphone', 'Electronics', 600.00, '2023-01-01'),
(3, 'T-Shirt', 'Apparel', 20.00, '2023-01-01');
-- Create the new version of the dataset
CREATE TABLE products_new (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(100),
price DECIMAL(10, 2),
updated_at DATE
);
-- Insert some dummy data into the new table (simulating changes)
INSERT INTO products_new (product_id, product_name, category, price, updated_at) VALUES
(1, 'Laptop', 'Electronics', 850.00, '2023-02-01'), -- Updated price
(2, 'Smartphone', 'Electronics', 600.00, '2023-02-01'), -- No change
(4, 'Shoes', 'Footwear', 50.00, '2023-02-01'); -- New product added
-------------------------------------------------------------------------------------------------------
-- Step 2: Capturing Changes
-- We will now capture the changes (inserts, updates, and deletions) by comparing the two tables.
-- 2.1 Capturing Inserts
-- Inserts are rows that exist in the products_new table but not in products_old.
-- Using a LEFT JOIN, we identify rows that are present in products_new but missing in products_old. These rows are considered as inserts.
-------------------------------------------------------------------------------------------------------
-- Capture the inserted rows
SELECT
'INSERT' AS change_type,
new.product_id,
new.product_name,
new.category,
new.price,
new.updated_at
FROM
products_new AS new
LEFT JOIN
products_old AS old
ON
new.product_id = old.product_id
WHERE
old.product_id IS NULL;
-- Result:
-- | change_type | product_id | product_name | category | price | updated_at |
-- | INSERT | 4 | Shoes | Footwear | 50.00 | 2023-02-01 |
-------------------------------------------------------------------------------------------------------
-- 2.2 Capturing Updates
-- Updates are rows that exist in both products_old and products_new but have different data.
-- Using an INNER JOIN, we find matching rows between products_old and products_new but filter for cases where at least one column has changed.
-- An INNER JOIN ensures that only records with matching product_id values in both tables are included in the result. These records represent the data points that exist in both versions (old and new) but might have changes in some columns.
-------------------------------------------------------------------------------------------------------
-- Capture the updated rows
SELECT
'UPDATE' AS change_type,
new.product_id,
old.product_name AS old_product_name,
new.product_name AS new_product_name,
old.category AS old_category,
new.category AS new_category,
old.price AS old_price,
new.price AS new_price,
new.updated_at
FROM
products_old AS old
INNER JOIN
products_new AS new
ON
old.product_id = new.product_id
WHERE
old.product_name <> new.product_name
OR old.category <> new.category
OR old.price <> new.price;
-- Result:
-- | change_type | product_id | old_product_name | new_product_name | old_category | new_category | old_price | new_price | updated_at |
-- | UPDATE | 1 | Laptop | Laptop | Electronics | Electronics | 800.00 | 850.00 | 2023-02-01 |
-------------------------------------------------------------------------------------------------------
-- 2.3 Capturing Deletions
-- Deletions are rows that exist in products_old but not in products_new.
-- Using a LEFT JOIN, we identify rows that are present in products_old but missing in products_new. These rows are considered as deletions.
-------------------------------------------------------------------------------------------------------
-- Capture the deleted rows
SELECT
'DELETE' AS change_type,
old.product_id,
old.product_name,
old.category,
old.price,
old.updated_at
FROM
products_old AS old
LEFT JOIN
products_new AS new
ON
old.product_id = new.product_id
WHERE
new.product_id IS NULL;
-- Result:
-- | change_type | product_id | product_name | category | price | updated_at |
-- | DELETE | 3 | T-Shirt | Apparel | 20.00 | 2023-01-01 |
-------------------------------------------------------------------------------------------------------
-- Step 3: Combining All Changes
-- We can combine the above results using UNION ALL to create a complete change data capture (CDC) result.
-------------------------------------------------------------------------------------------------------
-- Combine all changes into a single result set
SELECT
'INSERT' AS change_type,
new.product_id,
new.product_name,
new.category,
new.price,
new.updated_at
FROM
products_new AS new
LEFT JOIN
products_old AS old
ON
new.product_id = old.product_id
WHERE
old.product_id IS NULL
UNION ALL
SELECT
'UPDATE' AS change_type,
new.product_id,
new.product_name,
new.category,
new.price,
new.updated_at
FROM
products_old AS old
INNER JOIN
products_new AS new
ON
old.product_id = new.product_id
WHERE
old.product_name <> new.product_name
OR old.category <> new.category
OR old.price <> new.price
UNION ALL
SELECT
'DELETE' AS change_type,
old.product_id,
old.product_name,
old.category,
old.price,
old.updated_at
FROM
products_old AS old
LEFT JOIN
products_new AS new
ON
old.product_id = new.product_id
WHERE
new.product_id IS NULL;
-- Result:
-- | change_type | product_id | product_name | category | price | updated_at |
-- | INSERT | 4 | Shoes | Footwear | 50.00 | 2023-02-01 |
-- | UPDATE | 1 | Laptop | Electronics| 850.00| 2023-02-01 |
-- | DELETE | 3 | T-Shirt | Apparel | 20.00 | 2023-01-01 |