是否有理由不在 SQL 中将布尔值存储为位数据类型? [英] Are there reasons for not storing boolean values in SQL as bit data types?
问题描述
是否有理由不在 SQL 中将布尔值存储为没有 NULL 的位数据类型?我看到它们通常存储为没有约束的整数,以将值限制为 0 和 1,并作为字符串存储,如 T/F、真/假、是/否等,同样没有约束.将它们存储为位而不用担心额外的约束不是更好吗?我在这里错过了什么?
Are there reasons for not storing boolean values in SQL as bit data types without NULL? I see them often stored as integers without constraints to limit values to 0 and 1, and as strings with things like T/F, True/False, yes/no, etc., again without constraints. Isn't it better to store them as bits and not have to worry about additional constraints? What am I missing here?
推荐答案
我总是坚持使用最小的数据类型来存储它.
I'd always stick with the smallest data type I can to store this.
- SQLServer:BIT
- Oracle:NUMBER(1)(或 PL/SQL 中的 BOOLEAN)
- MySQL:TINYINT(iirc BOOLEAN 自动映射到此)
Oracle 的 BOOLEAN 只是 PL/SQL,不是表定义.更新了答案以反映这一点.
Oracle's BOOLEAN is PL/SQL only, not table definition. Updated answer to reflect this.
这篇关于是否有理由不在 SQL 中将布尔值存储为位数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!