database 我应该如何在 Oracle 中存储 GUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/153815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How should I store a GUID in Oracle?
提问by Shaun Bowe
I am coming from the SQL server world where we had uniqueidentifier. Is there an equivalent in oracle? This column will be frequently queried so performance is the key.
我来自我们拥有唯一标识符的 SQL 服务器世界。oracle中是否有等价物?此列将被频繁查询,因此性能是关键。
I am generating the GUID in .Net and will be passing it to Oracle. For a couple reasons it cannot be generated by oracle so I cannot use sequence.
我在 .Net 中生成 GUID 并将其传递给 Oracle。由于几个原因,它不能由 oracle 生成,所以我不能使用序列。
回答by Turnkey
回答by Erik Anderson
As others have stated, there is a performance hit using GUIDs compared to numeric sequences. That said, there is a function named "SYS_GUID()" available since Oracle 8i that provides the raw equivalent:
正如其他人所说,与数字序列相比,使用 GUID 会降低性能。也就是说,自 Oracle 8i 以来,有一个名为“ SYS_GUID()”的函数可用,它提供了原始等效项:
SQL> SELECT SYS_GUID() FROM DUAL;
SYS_GUID()
--------------------------------
248AACE7F7DE424E8B9E1F31A9F101D5
A function could be created to return a formatted GUID:
可以创建一个函数来返回格式化的 GUID:
CREATE OR REPLACE FUNCTION GET_FORMATTED_GUID RETURN VARCHAR2 IS guid VARCHAR2(38) ;
BEGIN
SELECT SYS_GUID() INTO guid FROM DUAL ;
guid :=
'{' || SUBSTR(guid, 1, 8) ||
'-' || SUBSTR(guid, 9, 4) ||
'-' || SUBSTR(guid, 13, 4) ||
'-' || SUBSTR(guid, 17, 4) ||
'-' || SUBSTR(guid, 21) || '}' ;
RETURN guid ;
END GET_FORMATTED_GUID ;
/
Thus returning an interchangeable string:
因此返回一个可互换的字符串:
SQL> SELECT GET_FORMATTED_GUID() FROM DUAL ;
GET_FORMATTED_GUID()
--------------------------------------
{15417950-9197-4ADD-BD49-BA043F262180}
A note of caution should be made that some Oracle platforms return similar but still unique values of GUIDs as notedby Steven Feuerstein.
需要注意的是,如Steven Feuerstein 所述,某些 Oracle 平台会返回类似但仍然唯一的 GUID 值。
回答by hamishmcn
If I understand the question properly, you want to generate a unique id when you insert a row in the db.
You could use a sequenceto do this. link here
Once you have created your sequence you can use it like this:
如果我正确理解了这个问题,您希望在数据库中插入一行时生成一个唯一的 id。
您可以使用序列来执行此操作。 此处链接
创建序列后,您可以像这样使用它:
INSERT INTO mytable (col1, col2) VALUES (myseq.NEXTVAL, 'some other data');
回答by stolsvik
RAW(16) is apparently the preferred equivalent for the uniqueidentifier MS SQL type.
RAW(16) 显然是唯一标识符 MS SQL 类型的首选等效项。
回答by Osama Al-Maadeed
GUIDs are not as used in Oracle as in MSSQL, we tend to have a NUMBER field (not null & primary key) , a sequence, and a trigger on insert to populate it (for every table).
GUID 在 Oracle 中不像在 MSSQL 中那样使用,我们倾向于有一个 NUMBER 字段(不是空和主键)、一个序列和一个插入时的触发器来填充它(对于每个表)。
回答by MusiGenesis
There is no uniqueidentifier in Oracle.
Oracle 中没有唯一标识符。
You can implement one yourself by using RAW (kind of a pain) or CHAR. Performance on queries that JOIN on a CHAR field will suffer (maybe as much as 40%) in comparison with using an integer.
您可以通过使用 RAW(有点痛苦)或 CHAR 自己实现。与使用整数相比,在 CHAR 字段上 JOIN 的查询的性能会受到影响(可能高达 40%)。
If you're doing distributed/replicated databases, the performance hit is worth it. Otherwise, just use an integer.
如果您正在使用分布式/复制数据库,那么性能损失是值得的。否则,只需使用整数。
回答by dacracot
The general practice using Oracle is to create an artificial key. This is a column defined as a number. It is populated via a sequence. It is indexed/constrained via a primary key definition.
使用 Oracle 的一般做法是创建一个人工密钥。这是一个定义为数字的列。它通过序列填充。它通过主键定义进行索引/约束。