Domanda

i'd got a general question.

I very much like URLs resulting in generated pages like that:

www.example.com/order/1e4fk678

Where 1e4fk678 is the variable posted to a php file generating the output.

My question is: What would be the best method to create the database table. An ID with a primary key auto incertment just generates IDs like 1, 2, 3, ....

So what is the best way to use a unique id that as well results in fast database tracking/traces?

Thanks

È stato utile?

Soluzione

Your DBMS already generates unique (auto_increment) identifiers. What you've not said in your question is whether you want the value presented in the URL to be non-predictable.

If not then you can simply do a base conversion on the generated id.

If you need the value to be non-predictable then pad it out as a string (most encryptions algorithms will do this automatically up to a multiple of the block size) and use reversible encryption to encode the auto-increment integer - and add some validation to detect brute force attacks. The method does not have to be reversible, but most methods of making the encryption non-reversible (generating a hash, using the data as an encrpyiton key, using a random encryption key) introduce a risk of collisions, requiring the encrypted data to be much larger.

If the reference is supplied remotely then it's simply a matter of adding a unique index on the data. But you still need to think about how you deal with collisions.

Using a random value has some merit for low data volumes - you can detect a collision at generation time - but the cost of verifying that the generated value does not collide increases at a rate of at least O(logN) (for an indexed field).

update

So using [a varchar instead of an integer] as a primary key will not slow down my database to medieval?

No - and that's the least of your worries.

Altri suggerimenti

You can actually use a String column with a unique constraint in mysql - or even as a primary key column. Just say:

`id` varchar(..) PRIMARY KEY

in your table definition.

Have a look at this question, it deals with strings as primary keys and their performance issues: MySQL - using String as Primary Key

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top