views:

191

answers:

5

How can i generate a unique key of length at most 12 character with these attributes: Name, Father's name, Mother's name, Date of birth, Place of birth.

Thanks in advance :)

+1  A: 

hmm.. which DBMS?

Anyway, Concatenate the aforementioned strings with the result of the UUID MySQL function, and perform a CRC32 function on the result.

Then, subtract the first 12 characters.

Dor
Oracle. And i want to make this key before inserting in the database. I will use c# in the front end.
Mr. Flint
If you want to make this key before it touches the database, then it ceases to be a database question.
Gary
+4  A: 

Hi

That's not a key you describe, but a short piece of structured data that you want to use instead of a key. Perhaps more helpfully:

-- do you want to be able to recover those attributes from a given 12-character key ?

or

-- will you be happy to only generate a key given those attributes ?

Regards

Mark

High Performance Mark
No, i dont want to recover those attributes. But i do not want to match every time user enter his name, father's name .... I want to match with the generated key.
Mr. Flint
+6  A: 

Even those columns probably aren't sufficient to be unique, and since a person's name can change (with marriage, choice, etc), I typically would not use that to generate a unique key.

I typically use a surrogate, like an autonumber/identity/sequence or a UUID/GUID, depending upon requirements.

Cade Roux
But i need to generate the same number every time the same user inputs.
Mr. Flint
You can generate a number that's the same for the same inputs (even a simple checksum can do that) - that's not a problem - avoiding collisions is the problem - even a CRC can have collisions. A 32-bit CRC will not be sufficient to not have collisions if you were to run it over the entire living population of the world (since it only has a space just over 4 billion).
Cade Roux
@Database: no, you don't need to generate an number; you need a database lookup that returns the number associated with that person, or create a new one if no value is found (and you're sure there are no typos, spaces, etc). See my answer for more detail.
Galghamon
+2  A: 

If you need same attributes to produce the same key, concatenate all attributes to a string, calculate a SHA1 hash and trim first 96 bits.

This is not guaranteed to be unique, but 50% probability of a hash collision will be at 3.3E+14 values which is more than enough.

If you don't need this, just use a surrogate ID.

Quassnoi
+7  A: 

The main, and in fact only, attribute of a unique identifier is that it is unique (within the scope of consideration, be that the table, the schema, or global). Incorporating any "real world" values into such an id does not make sense.

You should create a unique constraint on the attributes you mention, if you're sure that those attributes uniquely describe a person. This combination of attributes forms the "natural key" of the table.

The unique ID is a "surrogate key", a convenient handle that saves you from having to specify all the columns of your natural key in joins with that table. How you obtain a unique ID is database dependent. In Oracle, for instance you could use a sequence in combination with an insert trigger.

The unique constraint on the natural key will prevent you from inserting the same person twice, but you should really check the table first to see if the person already exists. If so, use the ID that was already assigned from that point on. You can, of course, combine this into a procedure where you give it your fields and it will return the ID, hiding the act of looking it up, or creating it if it's new and then returning the new ID.

Galghamon