I've a table in SQL Server that stores around 10 columns and all of them store nvarchar values. None of them are eligible for Primary Key constraint and I need a primary key which is not an identity column. I had an idea of storing the hash value of one of the unique columns using the hash() method in Python but it generates a different hash for the same string every time. How can I generate same hash value for the same Python string every time I call that hash() method?
-
Why can't you use a regular identity column? Can you add some sample data and also better describe your table?Tim Biegeleisen– Tim Biegeleisen2019-11-15 06:19:34 +00:00Commented Nov 15, 2019 at 6:19
-
I can't use identity column because I need a PK value that can be found by using one of the pre existing unique columns. For instance, my table is storing the data that is being scraped from various news websites. All of the columns store strings that have datatype as nvarchar(MAX). So I can't define any of them as my PK as SQL server doesn't allow that.Abhilash Kr– Abhilash Kr2019-11-15 06:29:40 +00:00Commented Nov 15, 2019 at 6:29
-
I still don't see why you can't logically add an identity column to your model.Tim Biegeleisen– Tim Biegeleisen2019-11-15 06:34:27 +00:00Commented Nov 15, 2019 at 6:34
-
I am maintaining a table which has thousands of records and everytime I insert a new row into the table I need to check whether this particular record already exists in my table or not. Now, because of the fact that all the other columns store really long strings(Nvarchar (MAX), I don't want to use any one of them to check for the existence of a record. As this checking will be done several times and it'll make the entire select query very expensive. So I need a column that stores a value that isn't as processor intensive to compare.Abhilash Kr– Abhilash Kr2019-11-15 15:21:16 +00:00Commented Nov 15, 2019 at 15:21
Add a comment
|
1 Answer
import hashlib
def hasher(data):
return hashlib.md5(str(data).encode()).hexdigest()
hasher('Some text!')
hasher('Some text!')
Output
'344cef0939c72102e99053c3adddd7dc'
'344cef0939c72102e99053c3adddd7dc'
2 Comments
Abhilash Kr
One instance will generate one hash for one string, the same string will generate a different hash on a different instance.