带有varchar(255)PRIMARY KEY的表会不会有任何伤害? [英] Will a table with varchar(255) PRIMARY KEY hurt anything?

查看:87
本文介绍了带有varchar(255)PRIMARY KEY的表会不会有任何伤害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不是一个好主意,但我想再次检查一下这不会造成疯狂的后果,例如使服务器崩溃.

I know it's not a good idea, but I would like to double check that this won't do something crazy like crash the server.

CREATE TABLE [dbo].[Items](
    [Id] [nvarchar](255) NOT NULL PRIMARY KEY,
    [Value] [nvarchar](max) NOT NULL,
)

了解类似这样的键与[int]键的详细信息将很有趣,但是仅确认这不会伤害任何东西就足够了.

It would be interesting to know details about how a key like this compares to an [int] key, but just confirmation that this will not hurt anything is sufficient.

推荐答案

可以使用VARCHAR(#)作为主键吗?

Can you use VARCHAR(#) as a primary key?

是的
来自文档,它是 VARCHAR(255)对于该列,每行最多需要257(255 + 2)个字节(取决于实际数据的长度).

Yes.
From the documentation, a VARCHAR(255) will take upwards of 257 (255+2) bytes (depends on the length of the actual data), for that column -- per row.

了解类似这样的键与 INT

INT 占用4个字节,根据文档.根据您的数据,有些数字数据类型会占用较少的空间:

INT takes 4 bytes, according to documentation. Depending on your data, there are numeric data types that take less space:

  • SMALLINT :2个字节
  • TINYINT :1个字节
  • SMALLINT: 2 bytes
  • TINYINT: 1 byte

字节数越少,列中的数据访问速度越快.其中包括JOIN.此外,数据库(和备份)将更小.

The lower the number of bytes, the faster accessing data in the column will be. That includes JOINs. Additionally, the database (and backups) will be smaller.

我会非常质疑是否需要这么大的 VARCHAR 作为主键-GUID的长度还不长.

I would greatly question the need for such a large VARCHAR as the primary key -- GUIDs aren't even that long.

这篇关于带有varchar(255)PRIMARY KEY的表会不会有任何伤害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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