How can I create a random id in a table

-1

I have always created tables with a id primary% autoincrementado and has not given me problems, but what interests me is having a id more difficult to remember and that does not take an order but does not allow duplicates.

I have this:

ID: 1, 2, 3, 4, 5, 6

I want this:

ID: 234434, 32323, 03043, 232345, 33124, 66674

How would you achieve it from the same phpmyadmin or any other more efficient method?

    
asked by Starking Elyos 14.02.2018 в 04:56
source

1 answer

2

In your web application you can use PHP's uniqid() function, for example:

<?php
    echo uniqid();
?>

And according to w3schools :

  

The uniqid () function generates a unique ID based on the microtime (current time in microseconds).

The uniqid() function generates a unique ID based on the micro-time (current time in microseconds). If you want to add more randomness you can use more_entropy , here a link to the official documentation that can be your interest.

Another option is to use the function UUID() or also UUID_SHORT() , both available in MySQL, here are some simple examples:

mysql> SELECT UUID();
    -> '6ccd780c-baba-1026-9564-5b8c656024db'

mysql> SELECT UUID_SHORT();
    -> 92395783831158784

To learn more details on how to generate both options, here the documentation:

I hope it serves you.

    
answered by 14.02.2018 в 08:27