我们可以使用Guid作为Sqlite数据库中的主键

时间:2021-12-21 16:24:53

Is is possible to use GUID as primary Keys in SQLITE Database?If Possible which datatype can be used?

是否可以将GUID用作SQLITE数据库中的主键?如果可以,可以使用哪种数据类型?

2 个解决方案

#1


30  

SQLite itself does not support GUID as internal type.

SQLite本身不支持GUID作为内部类型。

Except that, it does! (sort of). Remember, in SQLite any string can be used as type name, and that includes GUID or UUID (read more about SQLite datatypes).

除此之外,确实如此! (有点)。请记住,在SQLite中,任何字符串都可以用作类型名称,并且包括GUID或UUID(了解有关SQLite数据类型的更多信息)。

According to those rules, GUID type has affinity NONE, which is the same as for BLOB fields. With this in mind, you can create column of GUID type, and use following rules to access it:

根据这些规则,GUID类型具有亲和力NONE,这与BLOB字段相同。考虑到这一点,您可以创建GUID类型的列,并使用以下规则来访问它:

  • Store it as string like X'01020304050607080910111213141516' (X notation is used to represent 16 byte BLOB value). To insert, use:

    将其存储为字符串,如X'01020304050607080910111213141516'(X表示法用于表示16字节的BLOB值)。要插入,请使用:

    INSERT INTO mytable (uuid)
    VALUES (X'01020304050607080910111213141516');
    
  • Read it as 16-byte BLOB. quote(uuid) can be used to format output using X notation:

    将其读作16字节BLOB。 quote(uuid)可用于使用X表示法格式化输出:

    SELECT quote(uuid)
    FROM mytable
    

Such column can be also used as primary key. Unfortunately, there is no AUTOINCREMENT functionality like it exists for integer primary keys - you will have to handle it yourself. You can use something as simple as randomblob(16) for that, but it is not quite UUID as defined by standard.

这种列也可以用作主键。不幸的是,没有像整数主键那样存在的AUTOINCREMENT功能 - 你必须自己处理它。你可以使用像randomblob(16)那样简单的东西,但它不是标准定义的UUID。

Confusingly, you can also store text representation of UUID in the same field (SQLite won't stop you from doing that), but it will take at least 2x more space: BLOB is 16 bytes, UUID as text is at least 32 bytes.

令人困惑的是,您还可以将UUID的文本表示存储在同一个字段中(SQLite不会阻止您这样做),但它将占用至少2倍的空间:BLOB是16个字节,UUID作为文本至少是32个字节。

#2


9  

sqlite3 does not have a native UUID 128-bit format, per se.

sqlite3本身没有本机UUID 128位格式。

However, GUIDs can be used as keys in SQLite as either a TEXT or a binary BLOB representation.

但是,GUID可以在SQLite中用作TEXT或二进制BLOB表示的键。

Based on the performance numbers posted in answer to a similar question, both binary and string UUIDs can be efficient in SQLite for Create and Query when indexed.

基于回答类似问题时发布的性能数字,二进制和字符串UUID在索引时可以在SQLite中用于创建和查询。

see table in: https://*.com/a/11337522/3103448

请参阅表:https://*.com/a/11337522/3103448

SQLite can genarate either BLOB or TEXT UUIDs with randomblob(16) and hex(X) For example: lower(hex(randomblob(16)))

SQLite可以使用randomblob(16)和hex(X)来生成BLOB或TEXT UUID例如:lower(hex(randomblob(16)))

With similar index perfomance, a significant trade-off becomes whether a human readable string is preferred to the smaller binary data size.

利用类似的索引性能,显着的权衡取决于人类可读字符串是否优于较小的二进制数据大小。

#1


30  

SQLite itself does not support GUID as internal type.

SQLite本身不支持GUID作为内部类型。

Except that, it does! (sort of). Remember, in SQLite any string can be used as type name, and that includes GUID or UUID (read more about SQLite datatypes).

除此之外,确实如此! (有点)。请记住,在SQLite中,任何字符串都可以用作类型名称,并且包括GUID或UUID(了解有关SQLite数据类型的更多信息)。

According to those rules, GUID type has affinity NONE, which is the same as for BLOB fields. With this in mind, you can create column of GUID type, and use following rules to access it:

根据这些规则,GUID类型具有亲和力NONE,这与BLOB字段相同。考虑到这一点,您可以创建GUID类型的列,并使用以下规则来访问它:

  • Store it as string like X'01020304050607080910111213141516' (X notation is used to represent 16 byte BLOB value). To insert, use:

    将其存储为字符串,如X'01020304050607080910111213141516'(X表示法用于表示16字节的BLOB值)。要插入,请使用:

    INSERT INTO mytable (uuid)
    VALUES (X'01020304050607080910111213141516');
    
  • Read it as 16-byte BLOB. quote(uuid) can be used to format output using X notation:

    将其读作16字节BLOB。 quote(uuid)可用于使用X表示法格式化输出:

    SELECT quote(uuid)
    FROM mytable
    

Such column can be also used as primary key. Unfortunately, there is no AUTOINCREMENT functionality like it exists for integer primary keys - you will have to handle it yourself. You can use something as simple as randomblob(16) for that, but it is not quite UUID as defined by standard.

这种列也可以用作主键。不幸的是,没有像整数主键那样存在的AUTOINCREMENT功能 - 你必须自己处理它。你可以使用像randomblob(16)那样简单的东西,但它不是标准定义的UUID。

Confusingly, you can also store text representation of UUID in the same field (SQLite won't stop you from doing that), but it will take at least 2x more space: BLOB is 16 bytes, UUID as text is at least 32 bytes.

令人困惑的是,您还可以将UUID的文本表示存储在同一个字段中(SQLite不会阻止您这样做),但它将占用至少2倍的空间:BLOB是16个字节,UUID作为文本至少是32个字节。

#2


9  

sqlite3 does not have a native UUID 128-bit format, per se.

sqlite3本身没有本机UUID 128位格式。

However, GUIDs can be used as keys in SQLite as either a TEXT or a binary BLOB representation.

但是,GUID可以在SQLite中用作TEXT或二进制BLOB表示的键。

Based on the performance numbers posted in answer to a similar question, both binary and string UUIDs can be efficient in SQLite for Create and Query when indexed.

基于回答类似问题时发布的性能数字,二进制和字符串UUID在索引时可以在SQLite中用于创建和查询。

see table in: https://*.com/a/11337522/3103448

请参阅表:https://*.com/a/11337522/3103448

SQLite can genarate either BLOB or TEXT UUIDs with randomblob(16) and hex(X) For example: lower(hex(randomblob(16)))

SQLite可以使用randomblob(16)和hex(X)来生成BLOB或TEXT UUID例如:lower(hex(randomblob(16)))

With similar index perfomance, a significant trade-off becomes whether a human readable string is preferred to the smaller binary data size.

利用类似的索引性能,显着的权衡取决于人类可读字符串是否优于较小的二进制数据大小。