用于存储带前导零的数字的适当数据类型是什么?

发布于 2024-08-08 09:41:50 字数 126 浏览 2 评论 0原文

在 Access 2003 中,我需要显示这样的数字,同时保留前导零:

  • 080000
  • 090000
  • 070000

我应该使用什么数据类型?

In Access 2003 I need to display numbers like this while keeping the leading zeroes:

  • 080000
  • 090000
  • 070000

What data type should I use for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾听心声的旋律 2024-08-15 09:41:50

使用字符串(或文本、varchar,或您的特定 RDBMS 使用的任何字符串变体)并用您需要的任何字符(“0”)填充它。

Use a string (or text, or varchar, or whatever string variant your particular RDBMS uses) and pad it with whatever character you want ("0") that you need.

硪扪都還晓 2024-08-15 09:41:50

关键问题:

前导零是有意义的数据,还是只是格式化?

例如,07086 是我的邮政编码,前导零是有意义的,因此美国邮政编码必须存储为文本。

值“1”、“01”、“001”和“0001”是否被视为唯一的合法值,或者是否被视为重复值?

如果前导零在您的表中没有意义,并且仅用于格式化,则将数据存储为数字,并根据显示需要使用前导零进行格式化。

您可以使用 Format() 函数进行格式化,如本示例查询所示:

SELECT Format(number_field, "000000") AS number_with_leading_zeroes
FROM YourTable;

此外,我所知道的所有数据库引擎中的数字存储和索引都比文本存储和索引更有效,因此对于大型数据集(数百条数千条记录及更多),性能对数值数据使用文本数据类型的阻力可能相当大。

最后,如果您需要对数据进行计算,您希望将它们存储为数字。

关键是从数据的使用方式开始,并相应地选择数据类型。人们应该只在演示时(在表格和报告中)担心格式。

外观永远不应该驱动表字段中数据类型的选择。

Key question:

Are the leading zeros meaningful data, or just formatting?

For instance, 07086 is my zip code, and the leading zero is meaningful, so US zip codes have to be stored as text.

Are the values '1', '01', '001' and '0001' considered to be unique, legal values or are they considered to be duplicates?

If the leading zero is not meaningful in your table, and is just there for formatting, then store the data as a number and format with leading zeros as needed for display purposes.

You can use the Format() function to do your formatting, as in this example query:

SELECT Format(number_field, "000000") AS number_with_leading_zeroes
FROM YourTable;

Also, number storage and indexing in all database engines I know of are more efficient than text storage and indexing, so with large data sets (100s of thousands of records and more), the performance drag of using text data type for numeric data can be quite large.

Last of all, if you need to do calculations on the data, you want them to be stored as numbers.

The key is to start from how the data is going to be used and choose your data type accordingly. One should worry about formatting only at presentation time (in forms and reports).

Appearance should never drive the choice of data types in the fields in your table.

只有一腔孤勇 2024-08-15 09:41:50

如果您的真实数据与示例类似并且具有固定的位数,只需将数据存储在数字字段中,并使用 Access 表设计中列的格式/输入掩码属性将其显示为填充零。

除非您有可变数量的前导零,否则没有理由存储它们,而且这通常是一个坏主意。不必要地使用文本类型会损害性能,更容易引入异常数据,并使查询数据库变得更加困难。

If your real data looks like your examples and has a fixed number of digits, just store the data in a numeric field and use the format/input mask attributes of the column in Access table design display them with the padded zeros.

Unless you have a variable number of leading zeros there is no reason to store them and it is generally a bad idea. unecessarily using a text type can hurt performance, make it easier to introduce anomalous data, and make it harder to query the database.

野心澎湃 2024-08-15 09:41:50

采用 Unicode 压缩的固定宽度字符,并带有 CHECK 约束,以确保正好六个数字字符,例如 ANSI -92 查询模式语法:

CREATE TABLE IDs 
(
 ID CHAR(6) WITH COMPRESSION NOT NULL
    CONSTRAINT uq__IDs UNIQUE, 
 CONSTRAINT ID__must_be_ten_numeric_chars
    CHECK (ID ALIKE '[0-9][0-9][0-9][0-9][0-9][0-9]')
);

Fixed width character with Unicode compression with a CHECK constraint to ensure exactly six numeric characters e.g. ANSI-92 Query Mode syntax:

CREATE TABLE IDs 
(
 ID CHAR(6) WITH COMPRESSION NOT NULL
    CONSTRAINT uq__IDs UNIQUE, 
 CONSTRAINT ID__must_be_ten_numeric_chars
    CHECK (ID ALIKE '[0-9][0-9][0-9][0-9][0-9][0-9]')
);
萌吟 2024-08-15 09:41:50

您是否需要将它们保留为表中的数字(即您确实认为您需要在查询中进行聚合 - 例如 SUM 等)?

如果没有,那么文本/字符串数据类型就足够了。

如果您这样做,那么您可能需要 2 个字段。

  1. 存储数字 [即 80000] 并
  2. 存储一些有关如何显示该值的元数据
    • 可能是某种掩码或格式模式[例如“000000”]。

然后,您可以使用上面的模式字符串来格式化数字的显示

  • 如果您使用的是 .NET 语言,则可以使用 System.String.Format() 或 System.Object.ToString()

  • 如果您使用的是 Access 表单/报告,则 Access 在其 UI 控件中使用非常相似的字符串格式模式.

Do you need to retain them as numbers within the table (i.e. do think you will need to do aggregations within queries - such as SUM etc)?

If not then a text/string datatype will suffice.

If you DO then perhaps you need 2 fields.

  1. to store the number [i.e. 80000] and
  2. to store some meta-data about how the value needs to be displayed
    • perhaps some sort of mask or formatting pattern [e.g. '000000'].

You can then use the above pattern string to format the display of the number

  • if you're using a .NET language you can use System.String.Format() or System.Object.ToString()

  • if you're using Access forms/reports then Access uses very similar string formatting patterns in it's UI controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文