pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

為 SQL Server 中的字段生成唯一哈希

Generate Unique hash for a field in SQL Server(為 SQL Server 中的字段生成唯一哈希)
本文介紹了為 SQL Server 中的字段生成唯一哈希的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在編寫用于我們現有會員基礎的會員提供程序.我使用 EF4.1 進行所有數據庫訪問,我遇到的問題之一是最初設置數據庫時,關系是以編程方式完成的,而不是在數據庫中完成.如果需要在并非所有用戶都需要的列上建立關系,但為了建立關系確實需要是唯一的(根據我的理解).

I'm in the process of writing a Membership Provider for use with our existing membership base. I use EF4.1 for all of my database access and one of the issued that I'm running into is when the DB was originally setup the relationships were done programmatically instead of in the db. One if the relationships needs to be made on a column that isn't required for all of our users, but in order to make the relationships does need to be unique (from my understanding).

我認為可行的解決方案是在 userid 字段上執行 MD5 哈希(這是唯一的......這將/應該保證該字段中的唯一值).我在 sql server 上遇到問題的部分是在不替換存儲在 employeeNum 字段(有問題的那個)中的現有值的情況下執行此操作的查詢.

My solution that I believe will work is to do an MD5 hash on the userid field (which is unique ...which would/should guarantee a unique value in that field). The part that I'm having issues with on sql server is the query that would do this WITHOUT replacing the existing values stored in the employeeNum field (the one in question).

簡而言之,我的問題是.在值不存在的所有行的 employeeNum 字段(可能基于 userid 字段的 md5 哈希)中獲取唯一值的最佳方法是什么?t 已經存在.此外,在次要/主要程度上……這聽起來是個好計劃嗎?

So in a nutshell my question is. What is the best way to get a unique value in the employeeNum field (possibly based on an md5 hash of the userid field) on all the rows in which a value isn't already present. Also, to a minor/major extent...does this sound like a good plan?

推薦答案

如果您的問題只是如何為 userid 生成哈希值,您可以使用計算列以這種方式完成(或生成此值作為插入過程).我不清楚您是否了解 HASHBYTES 函數或當您說最佳"時您正在查看的其他標準.

If your question is just how to generate a hash value for userid, you can do it this way using a computed column (or generate this value as part of the insert process). It isn't clear to me whether you know about the HASHBYTES function or what other criteria you're looking at when you say "best."

DECLARE @foo TABLE
(
  userid INT, 
  hash1 AS HASHBYTES('MD5',  CONVERT(VARCHAR(12), userid)),
  hash2 AS HASHBYTES('SHA1', CONVERT(VARCHAR(12), userid))
);

INSERT @foo(userid) SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 500;

SELECT userid, hash1, hash2 FROM @foo;

結果:

userid  hash1                               hash2
------  ----------------------------------  ------------------------------------------
1       0xC4CA4238A0B923820DCC509A6F75849B  0x356A192B7913B04C54574D18C28D46E6395428AB
2       0xC81E728D9D4C2F636F067F89CC14862C  0xDA4B9237BACCCDF19C0760CAB7AEC4A8359010B0
500     0xCEE631121C2EC9232F3A2F028AD5C89B  0xF83A383C0FA81F295D057F8F5ED0BA4610947817

在 SQL Server 2012 中,我強烈建議至少使用 SHA2_256,而不是上述任何一種.(您忘記提及您使用的版本 - 總是有用的信息.)

In SQL Server 2012, I highly recommend at least SHA2_256 instead of either of the above. (You forgot to mention what version you're using - always useful information.)

說了這么多,我仍然想提請注意我在評論中提出的觀點:這里的最佳"解決方案是修復模型.如果 employeeNum 是可選的,則不應讓 EF 認為它是必需的或唯一的,如果它實際上不是某種標識符,則不應在關系中使用它.如果您首先為關系使用正確的屬性,為什么用戶會關心 employeeNumuserid 之間的沖突?

All that said, I still want to call attention to the point I made in the comments: the "best" solution here is to fix the model. If employeeNum is optional, EF shouldn't be made to think it is required or unique, and it shouldn't be used in relationships if it is not, in fact, some kind of identifier. Why would a user care about collisions between employeeNum and userid if you're using the right attribute for the relationship in the first place?

EDIT 按照 OP 的要求

那么說UPDATE table SET EmployeeNum = 1000000 + UserID WHERE EmployeeNum IS NULL 有什么問題?如果 EmployeeNum 將保持在 1000000 以下,那么你就保證沒有沖突并且你完全避免了散列.

So what is wrong with saying UPDATE table SET EmployeeNum = 1000000 + UserID WHERE EmployeeNum IS NULL? If EmployeeNum will stay below 1000000 then you've guaranteed no collisions and you've avoided hashing altogether.

如果 employeeNum 可能包含一個字符串,您可以生成類似的填充,但又是 EF 促進了這些可怕的列名嗎?為什么帶有 Num 后綴的列除了數字之外不包含任何內容?

You could generate similar padding if employeeNum might contain a string, but again is it EF that promotes these horrible column names? Why would a column with a Num suffix contain anything but a number?

這篇關于為 SQL Server 中的字段生成唯一哈希的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 123悬赏网_发布悬赏任务_广告任务平台| 全国国际学校排名_国际学校招生入学及学费-学校大全网 | 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 江苏皓越真空设备有限公司 | YAGEO国巨电容|贴片电阻|电容价格|三星代理商-深圳市巨优电子有限公司 | 包头市鑫枫装饰有限公司| 上海道勤塑化有限公司| 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | 上海租奔驰_上海租商务车_上海租车网-矢昂汽车服务公司 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 蓝鹏测控平台 - 智慧车间系统 - 车间生产数据采集与分析系统 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 一礼通 (www.yilitong.com)-企业礼品解决方案一站式服务平台 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 滚筒线,链板线,总装线,流水线-上海体能机电有限公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 | 深圳市东信高科自动化设备有限公司 |