How to generate random 10 digit number and place into database table? - mysql

I am working with a MySQL database that has a table of customers. One of the columns consist of phone_numbers varchar(10). My question is how can I create a simple loop that goes through all the customers and inserts a random phone numbers?
customer table

Use an UPDATE statement with the RAND and LPAD functions (docs: RAND, LPAD).
Example (generates numbers from 0000000000 to 9999999999): UPDATE table SET phone_numbers = LPAD(FLOOR(RAND() * 10000000000), 10, '0')

Related

Unpredictable random number unique in table in MariaDB (not just incremental)

I have table of Users in MariaDB and I need to generate serviceNumber for each newly created user.
Example of this code: 865165
Only two requirements are:
Unique in User table
Unpredictable when creating user (Not based on AutoIncrement maybe?)
Is this possible with just database? Or I need to implement it in backend when creating user.
The only (theoretically) collision free solution would be to generate the serviceNumber with UUID() function (or maybe UUID_SHORT()).
The disadvantage of this solution is, that it can't be used in statement based replication, in this case you should create the value in your backend.
Steps 1-3 are setup steps:
Decide on how big you want the numbers to be. 6-digit numbers would let you have a million numbers.
Build a temp table t with all 6-digit numbers (t_num).
Build and populate a permanent table:
CREATE TABLE `u` (
id INT AUTO_INCREMENT NOT NULL,
t_num INT NOT NULL,
PRIMARY KEY(id)
);
INSERT INTO u (t_num)
SELECT t_num FROM t ORDER BY RAND();
4--Plan A You have an auto_inc id in the real table; simply join to u to get the random 6-digit number.
4--Plan B
BEGIN;
SELECT t_num FROM u ORDER BY id LIMIT 1 FOR UPDATE; -- to get the random id
DELETE FROM u ORDER BY id LIMIT 1; -- Remove it from the list of available ids
From MySQL reference manual:
To obtain a random integer R in the range i <= R < j, use the
expression FLOOR(i + RAND() * (j − i)). For example, to obtain a
random integer in the range the range 7 <= R < 12, use the following
statement: SELECT FLOOR(7 + (RAND() * 5));
Make the field holding serviceNumber uniqe and use the above math expression to generate serviceNumbers.

How to insert a random integer number, for a given column and several rows

I was wondering.. Is it possible, with a SQL query, to:
generate randomly, a list of integer number
insert this in a table, for a specific column, and for multiple rows
I didn't try anything, to be honest I was just asking myself the question.
EDIT: (from a comment)
Imagine that I have a table USERS, with a field named VOTES. For every users, I want to insert, in the column VOTES, a random integer number between 0 and 15. For the record, I'm working with MySQL 5.5 | MariaDB
Thanks for your answers
If you want to update the column named votes in the table users with random integers between 0 and 15, you could do it in the following:
UPDATE users
SET votes = FLOOR(RAND() * 16)

How to replace all fields in a table with value+rowID?

Trying to write a set of queries that replace specific table rows with a generated value of "sometext" + rowID. So for example, I have a table called "customer_entity" and a row called "email", I'd like to walk through all records in this table, and replace email with "someone_"+ROWID+"#someplace.com". This allows me to keep the values unique, but also identifiable.
The part I'm not sure how to do, is to update all rows in x table while inserting the matched rowID for that row.
Debian9 (turnkeylinux)
MySQL 5.7 (Magento 2.2 table base)
uft8 - innodb
final results is to replace each email with a sanitized email address.
You can emulate a ROWID this way:
SELECT #rowid:=0;
UPDATE `customer_entity` SET `email` = CONCAT('someone', #rowid:=#rowid+1, '#someplace.com');

How to create random unique (not repeated) and fix length (9 digits) primary key in mysql database?

As the title, how to create a 9 digits number primary key which is random, unique, not repeated and from range 100000000 to 999999999?
And this method must be work on the godaddy server, seems godaddy have so many limitation.
I can only think of two reliable ways of creating unique numbers.
Use a systematic process, such as auto-incrementing, where you now the numbers are unique.
Store generated numbers in a table.
You want random numbers, so the first method could be applied using a pseudo-random number generator. But the second is probably simpler to implement.
It goes something like this:
create table numbers (
numberid int auto_increment primary key,
n varchar(10) not null unique
);
Then you need to create numbers using a loop. Do the following until it succeeds:
insert into numbers (n)
select cast((rand(*) * 900000000) + 1000000000 as varchar);
You can use last_inserted_id() to then get the most recent number inserted.
If pseudo-random is OK for you, you could create a trigger like this:
create trigger tr_setid before insert on mytable for each row
set new.id := (
select mod ((count(*) ^ 42) * 479001599 + 714320596, 900000000)+100000000
from mytable);
This system is not good if you also delete records from your table, as this solution assumes count(*) is one larger every time this trigger runs.
The multiplier is a prime and not a divisor of 900000000, guaranteeing that no duplicate number will be generated before all possible numbers have been visited.
The ^ operator is just mapping the count(*) so to make the generated series a bit less predictable.
With this trigger the first 10 records in the table will get these id values:
232387754
711389353
174384556
653386155
348394150
827395749
290390952
769392551
900374962
479376561

Inserting random characters to MYSQL Database

I want to add 100 entry to users table numbers field, random characters length is 10, all entry should be unique as well. How can i achieve this using MYSQL query code ?
Or do i need to use PHP ?
Help me with code snippets please. Thanks.
in mysql u can do like :
insert into table ( SUBSTRING(MD5(RAND()) FROM 1 FOR 10) , field2 , field3) , ( SUBSTRING(MD5(RAND()) FROM 1 FOR 10) , field2 , field3) , .........
..............
in php see this 2 links :
Short unique id in php
What is the best way to generate a random key within PHP?
That may create duplicates and it's too long. This is 10 char long:
UPDATE users SET numbers = SUBSTRING(MD5(RAND()) FROM 1 FOR 10)
However, you could still get duplicate values.
So you could put a unique restraint on your column. Then try the update. If there are duplicates, you will get an error. Then just try again. You only have 100 entries, so it's probably fine.
Is this for passwords?
If so, I'd recommend encrypting the value. Of course you then have the problem of not knowing what the value is. So you could create a temporary table, insert the random values in there. Then encrypt the values as they are inserted from the temp table into the real table. You can then use the temp table for reference (giving the users their passwords, etc). Hope that helps.
UPDATE TABLE users SET number_field = MD5(RAND());
You can't generate an unique random number. Over time, the randomness will generate a number already stored. You need to make a "quasi-random" number, meaning that it's a number based on another data, but it just looks random. You can use the primary key on the table as the base number to generate the "fake-random" number
INSERT INTO myTable(primaryKey,quasiRandom)
SELECT IFNULL(MAX(primaryKey),0)+1, CAST(CONCAT(IFNULL(MAX(primaryKey),0)+1,CHAR(FLOOR(RAND()*26)+65),FLOOR(100+RAND()*(500-100)))
AS CHAR(10)) AS quasiRandom FROM myTable