2

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?

4
  • Why can't you use a regular identity column? Can you add some sample data and also better describe your table? Commented 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. Commented Nov 15, 2019 at 6:29
  • I still don't see why you can't logically add an identity column to your model. Commented 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. Commented Nov 15, 2019 at 15:21

1 Answer 1

3
import hashlib
def hasher(data):
  return hashlib.md5(str(data).encode()).hexdigest()

hasher('Some text!')
hasher('Some text!')

Output

'344cef0939c72102e99053c3adddd7dc'
'344cef0939c72102e99053c3adddd7dc'
Sign up to request clarification or add additional context in comments.

2 Comments

One instance will generate one hash for one string, the same string will generate a different hash on a different instance.
Nope, It's working fine! Can you please share some code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.