1

I need to generate UUID for MYSQL int and BigInt which needs to be in a given range and I need 10 digits integer UUID for which I have written a code

def getUniqueID():
    unique_id = uuid4().int & (0 << 32) - 1
    return unique_id

for example, SQL int should not exceed the limit of 2147483647.(unique_id < 2147483647).

def getUniqueID(min,max):

    unique_id = uuid4().int & (0 << 32) - 1
    return unique_id

SQL int Limitation found here,

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

Dhamodharan
  • 199
  • 10
  • 1
    That's not a UUID then. It's just a random number in a certain range. – deceze Nov 13 '19 at 14:15
  • `UUID` Universally Unique Identifier. If you specify new rules it is no longer a UUID as deceze said. Please ask a different question. – Error - Syntactical Remorse Nov 13 '19 at 14:18
  • Yes, but I am searching for an option to generate UUID somehow. In my schema data type in INT/BIGINT so MySql won't accept if its exceeds its limit. – Dhamodharan Nov 13 '19 at 14:19
  • You should probably just use a auto incrementing number for your SQL table. – Error - Syntactical Remorse Nov 13 '19 at 14:19
  • UUID is often stored as a string not an int.... Or a UUID type. – Error - Syntactical Remorse Nov 13 '19 at 14:19
  • See [Storing UUID Values in MySQL Tables](https://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/). In otherwords UUIDs should be stored in `VARCHAR(36)` – Error - Syntactical Remorse Nov 13 '19 at 14:20
  • Does this answer your question? [Store UUID v4 in MySQL](https://stackoverflow.com/questions/43056220/store-uuid-v4-in-mysql) – Error - Syntactical Remorse Nov 13 '19 at 14:21
  • *"In otherwords UUIDs should be stored in VARCHAR(36)"* Not really `binary(16)` is much better ,,, @SyntacticalRemorse ... – Raymond Nijland Nov 13 '19 at 14:23
  • @RaymondNijland Very True (though it does lose human readability). As long as no one needs to read it in the database I would definitely go with that option. This is covered in the duplicate question above. – Error - Syntactical Remorse Nov 13 '19 at 14:25
  • 1
    how often do you need to read it anyhow, just make a simple VIEW which can make it readable on the fly when you need it.. @Error-SyntacticalRemorse – Raymond Nijland Nov 13 '19 at 14:26
  • @RaymondNijland Yes, That's 100% true it should be in varchar. Here DB design not with me, that's why am searching for a solution. – Dhamodharan Nov 13 '19 at 14:51
  • And thanks for the suggestions my dear developers, I will look forward to schema changes itself. – Dhamodharan Nov 13 '19 at 14:55
  • *" That's 100% true it should be in varchar."* No `VARCHAR(36)` is actually the worst datatype to choose for storing UUID .. a `CHAR(36)` would then still make a (bit) more sense as it is using less bits on disk as the lenght of a UUID never changes.. VARCHAR uses extra bytes (one or two) on disk to define how much is used from that lenght making selections slower.,, Anyhow that `binary(16)` is much better as [charset/collate](https://dev.mysql.com/doc/refman/8.0/en/charset-general.html) rules do not slow it down when selecting on `binary(16)` or take up more space then needed.. – Raymond Nijland Nov 13 '19 at 15:03
  • @RaymondNijland Yes make sense, I go through some of the articles as well. Gonna use fixed-length UUID's so CHAR(36) is a wiser choice. – Dhamodharan Nov 13 '19 at 15:06
  • *"Gonna use fixed-length UUID's so CHAR(36) is a wiser choice"* if you going to use `CHAR(36)` atleast define it as `CHAR(36) CHARSET ascii` to get the best performing and least disk space needing charset/collate for what you need.. – Raymond Nijland Nov 13 '19 at 15:10

1 Answers1

0

First you can install shortuuid with:

python -m pip install shortuuid

then you can use it like this:

import shortuuid
print(shortuuid.ShortUUID().random(length=10))

the project is in github: https://github.com/skorokithakis/shortuuid

0xM4x
  • 460
  • 1
  • 8
  • 19