.net GUID 的字符串长度是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968175/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:49:44  来源:igfitidea点击:

What is the string length of a GUID?

.netguidvarcharsql-typesstring-length

提问by Shimmy Weitzhandler

I want to create a varchar column in SQL that should contain N'guid'while guidis a generated GUID by .NET (Guid.NewGuid) - class System.Guid.

我想在 SQL 中创建一个 varchar 列,它应该包含N'guid'whileguid是由 .NET ( Guid.NewGuid)生成的 GUID - 类 System.Guid。

What is the length of the varcharI should expect from a GUID? Is it a static length?

varchar我应该从 GUID 中得到的长度是多少?是静态长度吗?

Should I use nvarchar(will GUID ever use Unicode characters)?

我应该使用nvarchar(GUID 是否会使用 Unicode 字符)?

varchar(Guid.Length)

PS. I don't want to use a SQL row guid data-type. I am just asking what is Guid.MaxLength.

附注。我不想使用 SQL 行 guid 数据类型。我只是问什么是Guid.MaxLength

回答by stevehipwell

It depends on how you format the Guid:

这取决于您如何格式化 Guid:

  • Guid.NewGuid().ToString()=> 36characters (Hyphenated)
    outputs: 12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("D")=> 36characters (Hyphenated, same as ToString())
    outputs: 12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("N")=> 32characters (Digits only)
    outputs: 12345678123412341234123456789abc

  • Guid.NewGuid().ToString("B")=> 38characters (Braces)
    outputs: {12345678-1234-1234-1234-123456789abc}

  • Guid.NewGuid().ToString("P")=> 38characters (Parentheses)
    outputs: (12345678-1234-1234-1234-123456789abc)

  • Guid.NewGuid().ToString("X")=> 68characters (Hexadecimal)
    outputs: {0x12345678,0x1234,0x1234,{0x12,0x34,0x12,0x34,0x56,0x78,0x9a,0xbc}}

  • Guid.NewGuid().ToString()=> 36 个字符(连字符)
    输出:12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("D")=> 36 个字符(带连字符,与 相同ToString()
    输出:12345678-1234-1234-1234-123456789abc

  • Guid.NewGuid().ToString("N")=> 32 个字符(仅限数字)
    输出:12345678123412341234123456789abc

  • Guid.NewGuid().ToString("B")=> 38 个字符(大括号)
    输出:{12345678-1234-1234-1234-123456789abc}

  • Guid.NewGuid().ToString("P")=> 38 个字符(括号)
    输出:(12345678-1234-1234-1234-123456789abc)

  • Guid.NewGuid().ToString("X")=> 68 个字符(十六进制)
    输出:{0x12345678,0x1234,0x1234,{0x12,0x34,0x12,0x34,0x56,0x78,0x9a,0xbc}}

回答by Eric

36, and the GUID will only use 0-9A-F (hexidecimal!).

36,并且 GUID 将仅使用 0-9A-F(十六进制!)。

12345678-1234-1234-1234-123456789012

12345678-1234-1234-1234-123456789012

That's 36 characters in any GUID--they are of constant length. You can read a bit more about the intricacies of GUIDs here.

在任何 GUID 中都是 36 个字符——它们的长度是恒定的。您可以在此处阅读更多有关 GUID 复杂性的信息

You will need two more in length if you want to store the braces.

如果您想存放牙套,则需要再长两个。

Note: 36 is the string length with the dashes in between. They are actually 16-byte numbers.

注意:36 是字符串长度,中间有破折号。它们实际上是 16 字节的数字。

回答by Marc Gravell

The correctthing to do here is to store it as uniqueidentifier- this is then fully indexable, etc. at the database. The next-best option would be a binary(16)column: standard GUIDs are exactly 16 bytes in length.

正确的在这里做的事情是存储它uniqueidentifier-这是再完全可转位等,在数据库中。次佳选项是binary(16)列:标准 GUID 的长度正好是 16 个字节。

If you must store it as a string, the length really comes down to how you choose to encode it. As hex (AKA base-16 encoding) without hyphens it would be 32 characters (two hex digits per byte), so char(32).

如果您必须将其存储为字符串,则长度实际上取决于您选择如何对其进行编码。作为没有连字符的十六进制(AKA base-16 编码),它将是 32 个字符(每字节两个十六进制数字),因此char(32).

However, you might wantto store the hyphens. If you are short on space, but your database doesn't support blobs / guids natively, you could use Base64encoding and remove the ==padding suffix; that gives you 22 characters, so char(22). There is no need to use Unicode, and no need for variable-length - so nvarchar(max)would be a bad choice, for example.

但是,您可能希望存储连字符。如果您的空间不足,但您的数据库本身不支持 blob / guid,您可以使用Base64编码并删除==填充后缀;这给你 22 个字符,所以char(22). 不需要使用 Unicode,也不需要可变长度 -nvarchar(max)例如,这将是一个糟糕的选择。

回答by Ross Light

I believe GUIDs are constrained to 16-byte lengths (or 32 bytes for an ASCII hex equivalent).

我相信 GUID 被限制为 16 字节的长度(或 32 字节的 ASCII 十六进制等价物)。

回答by cnd

GUIDs are 128bits, or

GUID 为 128 位,或

0 through ffffffffffffffffffffffffffffffff (hex) or 
0 through 340282366920938463463374607431768211455 (decimal) or 
0 through 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 (binary, base 2) or 
0 through 91"<b.PX48m!wVmVA?1y (base 95)

So yes, min 20 characters long, which is actually wasting more than 4.25 bits, so you can be just as efficient using smaller bases than 95 as well; base 85 being the smallest possible one that still fits into 20 chars:

所以是的,最少 20 个字符长,这实际上浪费了超过 4.25 位,因此您也可以使用比 95 更小的基数来提高效率;base 85 是最小的可能仍然适合 20 个字符:

0 through -r54lj%NUUO[Hi$c2ym0 (base 85, using 0-9A-Za-z!"#$%&'()*+,- chars)

:-)

:-)

回答by Qodex

22 bytes, if you do it like this:

22 字节,如果你这样做:

System.Guid guid = System.Guid.NewGuid();
byte[] guidbytes = guid.ToByteArray();
string uuid = Convert.ToBase64String(guidbytes).Trim('=');

回答by Hunter

Binary strings store raw-byte data, whilst character strings store text. Use binary data when storing hexi-decimal values such as SID, GUIDand so on. The uniqueidentifier data type contains a globally unique identifier, or GUID. This value is derived by using the NEWID() function to return a value that is unique to all objects. It's stored as a binary value but it is displayed as a character string.

二进制字符串存储原始字节数据,而字符串存储文本。存储十六进制值时使用二进制数据,例如SIDGUID等等。uniqueidentifier 数据类型包含全局唯一标识符或 GUID。该值是通过使用 NEWID() 函数返回所有对象唯一的值而得出的。它存储为二进制值,但显示为字符串。

Here is an example.

这是一个例子。

USE AdventureWorks2008R2;
GO
CREATE TABLE MyCcustomerTable
(
    user_login   varbinary(85) DEFAULT SUSER_SID()
    ,data_value   varbinary(1)
);
GO

INSERT MyCustomerTable (data_value)
    VALUES (0x4F);
GO

Applies to: SQL Server The following example creates the cust table with a uniqueidentifier data type, and uses NEWID to fill the table with a default value. In assigning the default value of NEWID(), each new and existing row has a unique value for the CustomerID column.

适用于:SQL Server 下面的示例使用唯一标识符数据类型创建 cust 表,并使用 NEWID 用默认值填充表。在分配 NEWID() 的默认值时,每个新行和现有行的 CustomerID 列都有一个唯一值。

-- Creating a table using NEWID for uniqueidentifier data type.  
CREATE TABLE cust  
(  
 CustomerID uniqueidentifier NOT NULL  
   DEFAULT newid(),  
 Company varchar(30) NOT NULL,  
 ContactName varchar(60) NOT NULL,   
 Address varchar(30) NOT NULL,   
 City varchar(30) NOT NULL,  
 StateProvince varchar(10) NULL,  
 PostalCode varchar(10) NOT NULL,   
 CountryRegion varchar(20) NOT NULL,   
 Telephone varchar(15) NOT NULL,  
 Fax varchar(15) NULL  
);  
GO  
-- Inserting 5 rows into cust table.  
INSERT cust  
(CustomerID, Company, ContactName, Address, City, StateProvince,   
 PostalCode, CountryRegion, Telephone, Fax)  
VALUES  
 (NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,  
 '90110', 'Finland', '981-443655', '981-443655')  
,(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',  
 '08737-363', 'Brasil', '(14) 555-8122', '')  
,(NEWID(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,   
 '1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')  
,(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,  
 '8010', 'Austria', '7675-3425', '7675-3426')  
,(NEWID(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,  
 'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68');  
GO