Data Masking Broke Your Joins? How to De-identify Data and Keep Table Relationships
· DataMint · 6 min read
You masked the personal data in the development database, opened a screen, and the customer detail shows no accounts at all. The data is clearly there, but the join returns zero rows. Anyone who has built test data by hand has run into this at least once.
To give the conclusion first, it is because the values were changed separately in each table. When the same original value becomes a different value in each table, the relationships that ran through that value are cut. The fix follows from that. Make the same value become the same value wherever it is.
Why joins break
Take an old system where customers and accounts are linked by the resident registration number (RRN, Korea's national ID number). Many systems now use a surrogate key such as a customer number, but in systems that have been running for more than ten years, RRNs, business registration numbers and phone numbers are still in join conditions.
| Table | Name | RRN | Account number |
|---|---|---|---|
| CUSTOMER | 홍길동 | 900101-1234568 | - |
| ACCOUNT | - | 900101-1234568 | 110-234-567890 |
Run the usual approach on this, a script that puts a random value into each column, and you get the following.
| Table | Name | RRN | Account number |
|---|---|---|---|
| CUSTOMER | 김○○ | 731122-2059174 | - |
| ACCOUNT | - | 850630-1740026 | 492-018-337215 |
SELECT c.name, a.acct_no
FROM customer c
JOIN account a ON a.rrn = c.rrn; -- 0 rowsMasking with asterisks is not much different. If you erase the second half, as in 900101-*******, everyone born on the same day gets the same value. This time the join does not break. Instead, unrelated rows attach to each other and the row count multiplies. It is less visible than zero rows, so it tends to be found even later.
It is not only joins that break
When relationships are cut, problems show up in places other than joins. In the order we have seen them most often:
- Foreign keys cannot be recreated. The child table now has values that do not exist in the parent, so creating the FK fails.
- Unique keys collide. If two different people happen to be changed to the same value, the UNIQUE constraint is violated. The larger the volume, the more likely it is.
- Systems do not match each other. If the core banking system is converted with script A and the information system with script B, the same customer becomes a different person in the two systems. Interface tests cannot be run.
- Values change every time you rebuild. The customer number in a test case registered last week does not exist in this week's database.
The fix: the same value always becomes the same value
Only one property is needed. If the originals are the same, the results must be the same. That holds whichever table the value is in, whichever database it is moved to, and whenever the job runs. If this property holds, there is no need to touch join conditions, because both sides change in the same way.
| Table | Name | RRN | Account number |
|---|---|---|---|
| CUSTOMER | 김서윤 | 900101-1947305 | - |
| ACCOUNT | - | 900101-1947305 | 110-871-204593 |
There are broadly two ways to do it.
Keeping a mapping table
Build a table that pairs each original value with its replacement, and apply that table to every table. It is easy to understand and easy to build yourself. The mapping table is the problem, though. It is one more table containing every original RRN, so in trying to remove personal data you have created a new personal data table. Once volumes reach the hundreds of millions, the time spent looking up the table is not small either.
Calculating with a key
Without a mapping table, the result is calculated from a secret key and the original value. The same key and the same input always give the same result, so there is no table to keep. Without the key, the original cannot be worked out from the result, and only the key needs to be kept safe. This is the method we use.
What to check if you build it yourself
If you implement this with scripts, we recommend checking the five points below. Most of them are found late, after long use.
- Is the same value stored in different notations?
9001011234568and900101-1234568are the same to a person but different values to a script. The same goes for leading and trailing spaces and full-width characters. - Do two different values collapse into the same value? Numbers have to be changed one to one for unique keys to hold.
- Are NULL and the empty string left as they are? Oracle treats the empty string as NULL, which is especially confusing when moving between databases.
- Are FKs created after all the data is loaded? Depending on the load order, the parent may not exist yet.
- Does running it again next month give the same result? If the key or seed changes, the test cases built so far become useless.
How DataMint Core does it
DataMint Core, the product we build, works by the "calculating with a key" method described above. It uses a different secret key for each customer, and the same value becomes the same value even across different tables and databases. A customer table in Oracle and an information system table in PostgreSQL still match each other when moved separately.

Keys are created the same as in production too. PKs and UNIQUE constraints, and FKs as well, are created as they are in the target database, with FKs created after all tables are loaded. If an FK cannot be created because a parent is missing, the run is not failed. Instead, the job log records which key on which table did not match.

Closing
If joins broke after masking, the problem is not the tool or the query but the way the values were changed. Instead of looking at columns one at a time, first look at where the same value is stored. If those values all change to the same value together, the relationships hold without being touched.
If you are still deciding how to build test data in the first place, also read how to automate the development database refresh.
- #data masking
- #de-identification
- #test data
- #referential integrity
- #development database
- #consistent masking