MYSQL to auto increment a 16 digit number for each entry - mysql

I am wanting MYSQL to assign a 16 digit number for each entry as it will be used for a private credit card service. How do I do this??
I had set the field which is called id to auto-increment and bigint but it still only assigns the numbers in order starting with 1.
I am needing the numbers to be 16 digits long and random.
Please help!

You can set the auto increment number to start at any number
ALTER TABLE MY_TABLE AUTO_INCREMENT = 1000000000000000; -- the first 16-digit number
The only restriction on the number specified is that it be not less than the current maximum number for the auto increment column for the specified table.
This however will not provide random numbers.
To create a random 16 digit number, you could use a trigger on insert that uses PASSWORD() function, which creates a 16-digit hexadecimal number, and convert it to a number if you need by calling UNHEX() and SUBSTR() on the result to capture 16 random digits.
For security, you should pass a seed with a value, eg the new id value concatenated with another piece of info from the record concatenated with a constant concatenated with perhaps the current time. It's up to you how far you want to go.
The random number could be done in your application code too, whcih may be a better option as it's easier to debug and control than database kung fu.

Related

get 10 random ids every single time from my database

I need 10 random ids at time. I need to get a new set of random ids every time I ask for a new set, but the new ones must not include any of the ones I already got from any number of previous times I asked for a new set Unless the process is reset. I may have a total of 100 or 1million ids in my database. I plan to use the ids to show 10 items on a webpage, with next and previous buttons. The pages already shown have to be consistent with the original items shown if the users goes back to any previously shown page
I have an idea that I select random numbers with seed 1000 times ,store it on redis server and pop out every 10 rows when a user enter the page. Are there any different ideas?
For a large set of non-repeating 'random' numbers you are probably better off using encryption. If numbers do not repeat, then they are not truly random because they are constrained not to repeat. Every time you pick a number the pool of available numbers shrinks. Hence the output is not truly random.
To implement, set up a counter: 0, 1, 2, 3, ... Pick a constant key. Then encrypt the counter using the key to get a non-repeating output. Then increment the counter ready to generate the next output. Because encryption is reversible then different inputs using the same key are guaranteed to give different outputs. Encryption is a one-to-one process.
AES will give you 128 non-repeating bits, DES only goes to 64 bits. If 128 bits are not enough then you will have to do some research on larger block ciphers, such as Rijndael.
You are looking for a repeatable random sort. In MySQL, you can do this by passing a seed to mathematical function rand(), as explained in the documentation:
for equal argument values, RAND(N) returns the same value each time, and thus produces a repeatable sequence of column values.
This gives you the first 10 records:
select t.*
from mytable t
order by rand(12345)
limit 10
You can then paginate; to get the "next" 10 records, you use the same seed to rand(), and offset the result:
select t.*
from mytable t
order by rand(12345)
limit 10 offset 10

Generating Random values for primary key in MySql

Is it sufficient to use UUID for generating random values in a column with Unique constraint?
Or should we append something like current timestamp to the UUID.
Or is there a better way to generate random yet unique values for a sql Column.
Generating random numbers in any language is a misnomer. The numbers are pseudo random. Meaning not entirely 100% random.
Also take into the scenario of generating a random number in the range of 1-5. You might get duplicates generating the same number. Or if you generate more than 5 numbers you absolutely WILL have duplicates.
Time stamp tends to be a good UUID if the fields are only updated from one place. But then you also have to worry about things such as making sure it is in the correct format. Especially when changing between languages/technologies.
With user ID why not just auto increment a column with the values? Start the column somewhere and increment from there. I would leave a few numbers at the beginning of the list so that you have some empty ones early on if you need them for test/admin.

Is the following possible in SQL query

Is the following possible, i been racking my brain to think of a solution.
I have an sql table, very simple table, few text columns and two int columns.
What i want to ideally do is allow user to add a row, but just the text columns and have the sql automatically put the numbers in the integer columns.
Ideally id like these numbers to random but not already exsist (so every row has a unique number) in the column. Also 10 digits long (but think that might be pushing it).
Is there anyway i can achieve this within the query itself?
Thanks
Sure - you pass the string as parameters to the Insert statement and the values as well - after you computed them. you can use SQL fucntion to generate the random number, or use the code you're calling from to generate them.
You can generate unique int numbers for a row with setting it AUTO_INCREMENT. However if you want something like a random hash, you need to do it in your backend. (or in a stored procedure)
Just a thought: if you generate long enough random strings you don't need to worry about having duplication usually. So it's safe to generate a random string, try to insert it and repeat until you get a duplicate entry error. Won't happen most of the time so it might be quicker than checking it first with a select.
You can generate a random number using MySQL. This will generate a random number between 0 and 10.000:
FLOOR(RAND() * 10001)
If you really want the numbers to always be 10 digits long you can generate a number between 1.000.000.000 and 9.999.999.999 like this:
FLOOR(RAND() * 9000000000) + 1000000000
The chance of the number not being unique is ~0.0000000001% and rising as you insert new rows. For a 0% chance of collision I'd suggest doing this the right way and handling this in code and not the database.
The random function explained:
What is happening is RAND() is generating a random decimal number between 0 and 1 (never actually 1). Then we multiply that number by the maximum number that we wish to produce plus 1. We add 1 because the biggest number produced for a set maximum number of 10 will be 9,XXXX and never actually 10 or above (remember I said that RAND() never generates 1), so we add plus one to produce the possibility of 10,XXXX which we later floor using FLOOR() to produce 10. In this case though we don't add 1 because 10.000.000.000 will become possible and it breaches our 10 digit boundary. Then we add the minimum number which we want produced (+ 1.000.000.000 in this case) while subtracting the same from the number we entered before (the maximum number).

Not have a minimum MySQL INT Value

When I add a number beginning with 0 into my MySQL database, it automatically gets converted to a single digit. These are mobile numbers, so I need to keep it starting with 0.
Store phone numbers as strings, not integers. (related: Common MySQL fields and their appropriate data types )
Try storing the numbers as varchars instead. When you retreive them from the database you could cast them using (int) if needed.

Padding the beginning of a mysql INT field with zeroes

I have an INT field in a large MySQL database containing incremental numbers in an INT field. These numbers are currently regular autoincrement numbers (1, 2, 3) but I need to pad them to three digits with zeroes at the beginning (so I get 001, 002, 003.. 010, 011, etc).
What commands can I run on my database to change this column into the format I need?
You can add a ZEROFILL attribute to the column to pad the data in the database or, when querying,
SELECT LPAD(CONVERT(`col`,VARCHAR(3)),3,'0')
to retreive the data formatted as a 3 digit number
There is no such thing as having leading zeroes on data in a numeric field in the database; the data just isn't stored that way, any more than it is stored in roman numerals. All you've got is the number three; so if you want to get the string "003" out, you've got two options:
Change to use a string field in the database: not recommended because you can't easily get incrementing numbers.
Format the number as you retrieve it from the database to add leading zeroes: better, but it has its own disadvantages - e.g. comparisons will be slower because they aren't indexed.