简体   繁体   English

在SQL Server 2008中匿名化ID值的最佳方法是什么

[英]What is the best way to anonymize ID values in sql server 2008

I got 2 tables in sql 2008 我在SQL 2008中有2张表

Table1
Id    Name Surname City   
1000  Alex White   London
1001  John Brown   Brussels
..

Table2
Id  Surgeon  Room  aId
1   Mike J.  A104  1000
2   Jack S.  C144  1001
...

And I have a query like: 我有一个查询,如:

Select a.Id,b.Id,
       a.Name,a.Surname,a.City,b.Surgeon,b.Room
into #results
from Table1 a
inner join Table2 b on a.Id = b.aId

What I want to do is to anonymize the a.Id and b.Id values for privacy, by using dummy ones instead of the real ones. 我想要做的是通过使用虚拟值而不是真实值来匿名化a.Id和b.Id值的隐私。 I added a random mathematical operations before, like: 我之前添加了随机数学运算,例如:

Select aId = a.Id * 22 / 5 + 14 * 2
      ,bId = b.Id * 12 / 4 + 7 * 3
       ...

but honestly I am not really happy what I am doing here and I am looking for a more professinal way to provide this. 但老实说,我对自己在这里所做的事情并不满意,我正在寻找一种更专业的方法来提供此服务。 Any advice would be appreciated. 任何意见,将不胜感激。

If you don't need to be sure the anonymized IDs are unique and you don't need to find a real ID based on an anonymized ID, you could use the CheckSum() or HashBytes() function with the strings from your Table1 and Table2: 如果不需要确保匿名ID是唯一的,并且不需要基于匿名ID查找真实的ID,则可以将CheckSum()HashBytes()函数与Table1和表2:

Select aId = CheckSum(a.Name + a.Surname) % 10000
      ,bId = HashBytes('SHA1', b.Surgeon) % 10000
      ,a.Name,a.Surname,a.City,b.Surgeon,b.Room
into #results
from Table1 a
inner join Table2 b on a.Id = b.aId

If you need to be sure you have a unique value for each of the Id values in your table and you also need to find a real ID based on an anonymized ID, you can construct a lookup table as follows: 如果需要确保表中的每个Id值都有唯一的值,并且还需要基于匿名ID查找真实的ID,则可以按以下方式构造查找表:

CREATE TABLE Anon
    (
    ID        INTEGER NOT NULL PRIMARY KEY,
    AnonID    UNIQUEIDENTIFIER DEFAULT NewID()
    );

this can then be used in queries where the actual ID should not be returned: 然后可以在不返回实际ID的查询中使用它:

Select aID = Anona.AnonID,
       bID = Anonb.AnonID,
       a.Name,a.Surname,a.City,b.Surgeon,b.Room
   into #results
   from Table1 a inner join Table2 b on a.Id = b.aId
   inner join Anon Anona on a.Id = Anona.Id
   inner join Anon Anonb on b.Id = Anonb.Id

The Anon table would need to be maintained to ensure it contains all IDs from your Table1 and Table2. 将需要维护Anon表,以确保它包含表1和表2中的所有ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server 2008:插入大数据的最佳方法是什么? - SQL Server 2008 : What is the best way for inserting big chunk of data? 在 SQL Server 2005/2008 中存储历史数据的最佳方式是什么? - What is the best way to store historical data in SQL Server 2005/2008? 在SQL Server 2008中替换游标的最佳方法 - Best way to replace cursor in SQL Server 2008 在SQL Server 2008中合并2个表的最佳方法 - Best way to Merge 2 tables in SQL Server 2008 记录所有用户请求操作的最佳方法是什么:(在Sql Server 2008中插入,更新,删除? - What is the best way to log all user request operations: (inserts, updates, deletes in Sql Server 2008? 在SQL Server(2008)上,如果我想过滤以某事开头的字符串字段,那么最好的方法是什么? - On SQL Server (2008), if I want to filter a string field that starts with something, what is the best way? 在SQL Server 2008中查找SQL锁的最佳方法 - Best way to find SQL Locks in SQL Server 2008 为 SQL 插入生成 ID 的最佳方法是什么? - What is the best way to generate an ID for a SQL Insert? ASP.net和SQL 2008:当值具有不同的数量而不是在整个行中重复时,为列表存储多个值的最佳方法是什么? - ASP.net & SQL 2008: What is the best way to store multiple values for lists when values are of varying amounts and not repeated throughout rows? 将CSV文件加载到SQL Server 2008的最佳方法? - Best way to load CSV files into SQL Server 2008?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM