0

I have a MariaDB and I would like to create a table where one of the columns is a random unique GUID generated by default when inserting a new row. For example, if we would use MySQL Server to create this column we would have:

GUID UNIQUEIDENTIFIER DEFAULT newsequentialid() NOT null

However, if we have MariaDB how can I do the same? I'm writing my app in python, if it is necessary to know.

I have seen that GUID can be stored as BINARY(16) instead of UNIQUEIDENTIFIER, but I'm not sure on how to replicate newsequentialid() and if it could be default too.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Learning from masters
  • 2,032
  • 3
  • 29
  • 42
  • What MariaDB version are you using? 10.7+ has a [uuid type](https://mariadb.org/10-7-preview-feature-uuid-data-type/). – danblack Mar 23 '22 at 22:44
  • 10.6.7 I'm using the one provided by AWS – Learning from masters Mar 23 '22 at 22:49
  • Is the guid an indexed value or primary key? In which true random can slow down insert speed. What is the random requirement/why isn't unique sufficient? [UUID()](https://mariadb.com/kb/en/uuid/) can be used like [this](https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=289604cdd3d8e06c959ff0bb8f11fec7). – danblack Mar 23 '22 at 23:11
  • No, it's not. I just wanted a random unique guid. If UUID is unique it's fine with me! So what do I have to write in the SQL when inserting it? UUID()? that's all? – Learning from masters Mar 23 '22 at 23:45
  • You can `UUID()` as a value or as the default value like the linked [this](https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=289604cdd3d8e06c959ff0bb8f11fec7) above. You can also use python's [uuid](https://docs.python.org/3/library/uuid.html) and using `.bytes` /`.bytes_le` insert into a `BINARY(16)` column. – danblack Mar 24 '22 at 00:52

0 Answers0