Varchar(max)列不允许是SQL Server中的主键

时间:2020-12-15 10:36:53

Varchar(max) column not allowed to be a Primary Key in SQL Server.

Varchar(max)列不允许是SQL Server中的主键。

What is the max length of a varchar type that can be made a primary key.

可以作为主键的varchar类型的最大长度是多少。

This might be a bad practice, but came across the situation.

这可能是一种不好的做法,但遇到了这种情况。

3 个解决方案

#1


16  

900 bytes is the max length for a primary key, this is a bad idea in my opinion, why don't you use a surrogate key instead?

900字节是主键的最大长度,在我看来这是一个坏主意,为什么不使用代理键呢?

#2


5  

This question has already been answered in this thread; the short answer is that an index field cannot exceed 900 bytes. That said, it's generally a bad idea to use a long VARCHAR field as an index for myriad reasons such as efficiency and maintainability.

这个问题已经在这个问题中回答了;简短的回答是索引字段不能超过900个字节。也就是说,使用长VARCHAR字段作为索引通常是一个坏主意,因为有很多原因,如效率和可维护性。

#3


3  

The primary key is used in all other indexes to identify rows. Having a potentially multi-megabyte value as a key makes very little sense, hence the limit in general on total bytes in any key.

主键在所有其他索引中用于标识行。具有潜在的多兆字节值作为密钥几乎没有意义,因此通常限制任何密钥中的总字节数。

#1


16  

900 bytes is the max length for a primary key, this is a bad idea in my opinion, why don't you use a surrogate key instead?

900字节是主键的最大长度,在我看来这是一个坏主意,为什么不使用代理键呢?

#2


5  

This question has already been answered in this thread; the short answer is that an index field cannot exceed 900 bytes. That said, it's generally a bad idea to use a long VARCHAR field as an index for myriad reasons such as efficiency and maintainability.

这个问题已经在这个问题中回答了;简短的回答是索引字段不能超过900个字节。也就是说,使用长VARCHAR字段作为索引通常是一个坏主意,因为有很多原因,如效率和可维护性。

#3


3  

The primary key is used in all other indexes to identify rows. Having a potentially multi-megabyte value as a key makes very little sense, hence the limit in general on total bytes in any key.

主键在所有其他索引中用于标识行。具有潜在的多兆字节值作为密钥几乎没有意义,因此通常限制任何密钥中的总字节数。