关于选择数据类型

2022-11-14数据库问题
2

本文介绍了关于选择数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果在我的表中的一列中,我希望值为 Yes、No 或 Optional,那么我需要使用什么数据类型?

If in one of my columns in my Table I want the values as either Yes, No or Optional then what data type do I need to use?

推荐答案

BIT:

  • 占用 1 个字节,但在 SQL Server 中最多可以将 8 个 BIT 字段合并为一个 BYTE.
  • 存储两个值之一:1(表示真)和 0(表示假),因此该列需要可以为空,以便 NULL 作为您的第三个值传递
    • 占用 1 个字节
    • ASCII 不区分大小写为 26 个字符,区分大小写为 52 个字符
    • 占用 1 个字节
    • 值从 0 到 255

    所有选项占用相同的空间量,使得 JOIN/etc 的性能相当.

    All of the options take the same amount of space, making performance equivalent for JOINs/etc.

    BIT 如果可能的值有任何变化的机会,则不是最明智的选择.CHAR(1) 是立即可读的 IE: Y, N, O.TINYINT 是您想要通过外键关联的表中的主键的不错选择,并且将描述性文本存储在另一列中.

    BIT is not the wisest choice if there's any chance of the possible values changing. CHAR(1) is immediately readable IE: Y, N, O. TINYINT is a good choice for the primary key in a table you want to relate via foreign key, and store the descriptive text in another column.

    CHAR(1) 将是我的选择,否则 TINYINT.
    使用 CHAR(1),自然主键是单个字符的可能性很小.如果您有 2 个以上以相同字符开头的单词,则假设基于前导字符的自然键会失败,并且如果标签需要更改,则会导致悲伤,因为键也应该更改并永久保留(除非您很懒惰& 喜欢解释为什么代码不遵循与其他代码相同的方案).CHAR(1) 还提供了 TINYINT 所做的大约五分之一的可能性(假设上限为 52 个区分大小写的值)——人工/代理键与描述更改隔离.

    CHAR(1) would be my choice if not using a foreign key relationship, TINYINT otherwise.
    With CHAR(1), having a natural primary key that is a single character is very unlikely. Assuming a natural key based on the leading character fails if you have 2+ words that start with the same character, and causes grief if the label needs to change because the key should also change and be perpetuated (unless you're lazy & like explaining why a code doesn't follow the same scheme as the others). CHAR(1) also provides roughly a fifth of the possibilities (assuming the upper end, 52 case sensitive values) that TINYINT does -- the artificial/surrogate key insulates from description changes.

    这篇关于关于选择数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12