且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据类型在SQL Server中应该是2的幂的大小吗?

更新时间:2023-01-01 09:39:28

长度为50是SQL Server默认为50作为长度是一个问题的大多数数据类型的长度。



如上所述,字段的长度应该适合于存储在那里的数据,而不是因为SQL Server中单个记录的长度有限制(约8000字节)。



此外,字段的长度可以被视为您的文档的一部分。我不知道有多少次,我遇到了懒惰的程序员谁声称他们不需要文档,因为代码是自我记录,然后他们不打扰做的东西,使代码自我记录。 p>

What are good sizes for data types in SQL Server? When defining columns, i see data types with sizes of 50 as one of the default sizes(eg: nvarchar(50), binary(50)). What is the significance of 50? I'm tempted to use sizes of powers of 2, is that better or just useless?

Update 1 Alright thanks for your input guys. I just wanted to know the best way of defining the size of a datatype for a column.

The reason so many fields have a length of 50 is that SQL Server defaults to 50 as the length for most data types where length is an issue.

As has been said, the length of a field should be appropriate to the data that is being stored there, not least because there is a limit to the length of single record in SQL Server (it's ~8000 bytes). It is possible to blow past that limit.

Also, the length of your fields can be considered part of your documentation. I don't know how many times I've met lazy programmers who claim that they don't need to document because the code is self documenting and then they don't bother doing the things that would make the code self documenting.