在MySQL中存储UUID v4 [英] Store UUID v4 in MySQL

查看:71
本文介绍了在MySQL中存储UUID v4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据在此处

现在,我要将其存储在MySQL数据库中.存储UUID v4的最佳/最有效的MySQL字段格式是什么?

Now I want to store that in a MySQL database. What is the best/most efficient MySQL field format for storing UUID v4?

我目前有varchar(256),但是我敢肯定这比必要的要大得多.我找到了很多几乎可以回答的内容,但是对于它们所指的UUID形式,它们通常是模棱两可的,所以我要询问具体的格式.

I currently have varchar(256), but I'm pretty sure that's much larger than necessary. I've found lots of almost-answers, but they're generally ambiguous about what form of UUID they're referring to, so I'm asking for the specific format.

推荐答案

将其存储为VARCHAR(36)(如果您希望完全适合),或者将其存储为VARCHAR(255),无论如何都要以相同的存储成本进行计算.这里没有理由大惊小怪.

Store it as VARCHAR(36) if you're looking to have an exact fit, or VARCHAR(255) which is going to work out with the same storage cost anyway. There's no reason to fuss over bytes here.

请记住,VARCHAR字段是可变长度,因此存储成本与其中的实际数据量成正比,而不与其中的数据量成正比.

Remember VARCHAR fields are variable length, so the storage cost is proportional to how much data is actually in them, not how much data could be in them.

将其存储为BINARY非常烦人,这些值不可打印,并且在运行查询时可能显示为垃圾.很少有理由使用文字二进制表示形式.可以将人类可读的值粘贴粘贴并轻松使用.

Storing it as BINARY is extremely annoying, the values are unprintable and can show up as garbage when running queries. There's rarely a reason to use the literal binary representation. Human-readable values can be copy-pasted, and worked with easily.

诸如Postgres之类的其他平台也具有适当的UUID列,该列以更紧凑的格式在内部存储它,但将其显示为便于人类阅读,因此您可以充分利用这两种方法.

Some other platforms, like Postgres, have a proper UUID column which stores it internally in a more compact format, but displays it as human-readable, so you get the best of both approaches.

这篇关于在MySQL中存储UUID v4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆