字符串的 SQL 数据类型

发布于 2024-12-23 06:23:11 字数 524 浏览 0 评论 0原文

我有类似的数据结构 -

class House
{
    int id;
    string street;
    string city;
    string review;
    string status;
}
  1. streetcity 是常规字符串,并且应始终少于 32 个字符。我认为他们应该有一个 nchar(32) 的 SQL 数据类型?或者它们应该是 varchar?

  2. review 是用户可以输入的可选字符串。即使他们确实给出了,也可能有很大差异,从 4-5 个单词到 2000 多个字符。那么我应该使用什么数据类型来存储它?

  3. 状态是一个标志,其值为“新”、“旧”、“未启动”。无论它具有什么值,都将显示在桌面应用程序的数据网格中。我应该将此字段存储为字符串,还是具有不同位的字节作为标志?

I have data structure something along the line of this -

class House
{
    int id;
    string street;
    string city;
    string review;
    string status;
}
  1. street and city are regular strings and should always be less than 32 characters. I take it they should have an SQL data type of nchar(32)? Or should they be varchar?

  2. review is an optional string that users may input. And if they do give it, it can vary hugely, from 4-5 words up to 2000+ characters. So what data type should I use to store this?

  3. status is a flag that can have values of 'New', 'Old', 'UnStarted'. Whatever value it has will be displayed in a datagrid in a desktop application. Should I be storing this field as a string, or a byte with different bits acting as flags?

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

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

发布评论

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

评论(1

甜中书 2024-12-30 06:23:11

如果您正在设计美国表,我建议使用 VARCHAR(32) 作为地址和城市。如果您正在设计一个国际字段,请增大两个字段并切换为 NVARCHAR(72)。 NVARCHAR 存储占用较多空间,但允许存储非 ASCII 字符...

CHAR(32) 保留 32 字节数据,无论字段保存 1 个字符还是 32 个字符。此外,某些客户端编程语言不会自动修剪多余的空格(这是正确的,但可能不是预期的)。 NCHAR(32) 保留 64 个字节,因为每个字符都用 2 个字节表示

对于审核,我同意 lanks、TEXTVARCHAR(max)< /strong> -(MS SQL 特定) 最好。评论长度可以超过2000字吗?如果 2000 是绝对限制,那么我会选择 VARCHAR(2000)。如果您可以进行任意长度的审核,我只会使用文本字段。请记住,如果用户输入的评论超过 2000 个字符,并且您尝试插入它,数据库将引发错误,因此您的应用程序需要限制字符数或在错误发生时进行处理。

状态应该是数据库允许的最小整数。您可以创建第二个表来提供代码的文本描述。

I would suggest using VARCHAR(32) for address and city if you are designing a United States table. If you are designing an international one, make both fields larger and switch to NVARCHAR(72) for example. NVARCHAR storage takes up more space, but allows non-ASCII characters to be stored....

CHAR(32) reserves 32 bytes of data, regardless of whether the field holds 1 character or 32 characters. In addition, some client programming languages will not trim the extra spaces automatically (which is proper, but might not be expected). NCHAR(32) reserves 64 bytes, since every character is represent in 2 bytes

For the review, I agree with lanks, a TEXT or VARCHAR(max) -(MS SQL specific) would be best. Can the review be longer than 2000 characters? If 2000 is the absolute limit, then I would go with VARCHAR(2000). I would only go with a TEXT field if you can have any length review. Keep in mind, if a user enters a review more than 2000 characters, the database will raise an error if you try to insert it, so your application needs to either restrict the number of characters or handle the error when it occurs.

Status should be the smallest integer your database allows. You can create a second table to provide text descriptions for the codes.

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