Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating temporary tables with random names in MySQL

I wanna create temporary tables with random names in a MySQL stored procedure. I also need to store the name in order to access the table from another stored procedure. I was thinking of using MD5 hashes:

SELECT md5(RAND()+CURRENT_TIMESTAMP());

I wonder if this will generate totally collision free strings or not?

like image 868
MahPeace Avatar asked Mar 22 '23 20:03

MahPeace


1 Answers

You could use uuid() and remove the dashes from the result... that is the closest thing I can think of that would give you anything reliably unique.

select concat("table_prefix_", replace(uuid(), '-', '')) as unique_name; 

it would end up being like this:

mysql> select concat("table_prefix_",replace(uuid(), '-', '')) as unique_name;
+-----------------------------------------------+
| unique_name                                   |
+-----------------------------------------------+
| table_prefix_39f14dd9418011e3bd86c0cb38cd4f18 |
+-----------------------------------------------+
1 row in set (0.00 sec)
like image 189
gloomy.penguin Avatar answered Apr 02 '23 19:04

gloomy.penguin