Answer the question
In order to leave comments, you need to log in
Generation of short unique keys in mysql how to implement?
Hello, I have a table like this in a database
CREATE TABLE links (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
url VARCHAR(2048) NOT NULL,
short_key VARCHAR(10) NOT NULL
);
Answer the question
In order to leave comments, you need to log in
Leave the database alone. She has nothing to do with this task at all.
All you need is reversible encryption with your ID key - so that the user sees the encrypted string, and you can get the ID back from it. That's all.
PS However, special encryption is not required for such a purpose. Collect the string {N1}{N2}{N3}, where N1 and N2 are random numbers of a given length, and N3 = (N1 * N2 + ID) XOR C - here is the finished short_key, from which we get back ID = (N3 XOR C ) - (N1*N2)
Since your goal is "needed so that smart-ass users cannot sort through all the records by id by changing the id in the link."
Leave the ID as AUTOINCREMENT INTEGER and just add one more field with a "salt", for the char(3) type example, where when inserting and store the random string value.
When you generate a URL, then make it according to the pattern ID + "-" + SALT, which will help you achieve the desired goal.
SQL does not know how to generate unique rows. Generate with php:unique();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question