La funzione UUID
di MySQL restituisce un GUID UUIDv1. Sto cercando un modo semplice per generare GUID casuali (ad esempio UUIDv4) in SQL.Come generare un UUIDv4 in MySQL?
6
A
risposta
10
Ho passato un po 'di tempo a cercare una soluzione e ho creato la seguente funzione mysql che genera un UUID casuale (ad esempio UUIDv4) utilizzando le funzioni standard di MySQL . Sto rispondendo alla mia domanda per condividerla nella speranza che sia utile.
-- Change delimiter so that the function body doesn't end the function declaration
DELIMITER //
CREATE FUNCTION uuid_v4()
RETURNS CHAR(36)
BEGIN
-- Generate 8 2-byte strings that we will combine into a UUIDv4
SET @h1 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h2 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h3 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h6 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h7 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
SET @h8 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
-- 4th section will start with a 4 indicating the version
SET @h4 = CONCAT('4', LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));
-- 5th section first half-byte can only be 8, 9 A or B
SET @h5 = CONCAT(HEX(FLOOR(RAND() * 4 + 8)),
LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));
-- Build the complete UUID
RETURN LOWER(CONCAT(
@h1, @h2, '-', @h3, '-', @h4, '-', @h5, '-', @h6, @h7, @h8
));
END
//
-- Switch back the delimiter
DELIMITER ;
Nota: La generazione di numeri pseudo-casuali utilizzato (di MySQL RAND
) non è crittograficamente sicuro e quindi ha alcuni pregiudizi che possono aumentare il rischio di collisione .
Problemi correlati
- 1. generare pangrattato di categorie archiviati in MySQL
- 2. PHP & mySQL (i): come generare un ID utente casuale?
- 3. Come generare un file .po?
- 4. MySQL trigger per generare un hash per un indice auto_increment
- 5. Come generare un PDF?
- 6. Come generare un modello dal database esistente in Sails JS?
- 7. Impossibile generare il tipo enum in MySql dall'entità JPA
- 8. Come posso generare un GUID in Vim?
- 9. Come generare un ID univoco in Dart
- 10. Come posso informare sqlalchemy per generare un errore ogni volta che MySQL lancia un avvertimento?
- 11. Come generare un intervallo alternato?
- 12. Clojure: come generare un 'trie'?
- 13. Come generare target in un Makefile iterando su un elenco?
- 14. Come generare un'eccezione in VB.NET
- 15. Come generare un avviso R in modo sicuro in Rcpp
- 16. Come generare numeri casuali diversi in un ciclo in C++?
- 17. Come generare sorgenti in un plug-in sbt?
- 18. Come creare un generatore di righe in MySQL?
- 19. Come posso generare uno script SQL dal mio diagramma in MySQL Workbench?
- 20. Come posso generare un Javascript dinamico?
- 21. Come generare un codice client soap PHP?
- 22. Come generare un dizionario con Autofixture
- 23. Come generare un postback falso con javascript?
- 24. Come si alias un database in MySQL?
- 25. Come convertire un DB MySQL in XML?
- 26. Come rinominare un indice in MySQL
- 27. Come memorizzare un array in mysql?
- 28. Come creare un trigger MySQL in phpmyadmin
- 29. Come ciclare con un array in MySQL?
- 30. Come creare un limite dinamico in MySQL?