我们可以使用的GUID在SQLite数据库主键 [英] Can we use Guid as a Primary Key in Sqlite Database

查看:2010
本文介绍了我们可以使用的GUID在SQLite数据库主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,可以使用GUID在SQLite数据库主密钥?如果可能的话它的数据类型可以用吗?

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

推荐答案

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

SQLite itself does not support GUID as internal type.

除,但它确实! (有点)。请记住,在SQLite的任何字符串可以作为类型名,这包括的GUID UUID (阅读更多关于< A HREF =htt​​p://www.sqlite.org/datatype3.html> SQLite的数据类型)。

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).

根据这些规则,的GUID 类型有亲和力 NONE ,这是相同 BLOB 字段。考虑到这一点,你可以按照规则来访问它创建的GUID 的类型,并使用柱:

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:

  • 存储它作为字符串如 X'01020304050607080910111213141516(X符号是用来重新present 16字节 BLOB 值)。 要插入,使用:

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

INSERT INTO mytable (uuid)
VALUES (X'01020304050607080910111213141516');

  • 它读成16字节 BLOB 报价(UUID)可用于用X标记来格式化输出:

  • Read it as 16-byte BLOB. quote(uuid) can be used to format output using X notation:

    SELECT quote(uuid)
    FROM mytable
    

  • 这种柱,也可以使用作为主键。不幸的是,有没有自动增量功能,例如它存在的整数主键 - 你将不得不自己处理。你可以使用一些简单的 randomblob(16)对于这一点,但它是不是很 UUID 定义<一个HREF =htt​​ps://en.wikipedia.org/wiki/Uuid>用标准。

    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.

    令人困惑的是,还可以存储文本重新$ P $ UUID在同一领域psentation(SQLite的不会那么做阻止你),但它至少需要2倍以上的空间:BLOB是16个字节,UUID作为文本是至少32字节。

    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.

    这篇关于我们可以使用的GUID在SQLite数据库主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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