在设计数据库时,存储多个true/false值的首选方法是什么? [英] When designing databases, what is the preferred way to store multiple true / false values?
问题描述
如标题中所述,在设计数据库时,处理具有多列的表的首选方法是,这些表仅将true/false值存储为一个或一个值(例如"Y/N:或"0" /1)?同样,在不同的数据库(例如Oracle和SQL Server)之间是否会出现一些问题,可能会影响列的处理方式?
As stated in the title, when designing databases, what is the preferred way to handle tables that have multiple columns that are just storing true / false values as just a single either or value (e.g. "Y/N: or "0/1")? Likewise, are there some issues that might arise between different databases (e.g. Oracle and SQL Server) that might affect how the columns are handled?
推荐答案
在SQL Server
中,存在BIT
数据类型.您可以在那里存储0或1,比较这些值,但不能运行MIN
或MAX
.
In SQL Server
, there is BIT
datatype. You can store 0 or 1 there, compare the values but not run MIN
or MAX
.
在Oracle
中,您只需使用NUMBER
或CHAR(1)
.
在MySQL
和PostgreSQL
中,任何数据类型都可以隐式转换为BOOLEAN
.
In MySQL
and PostgreSQL
any datatype is implicitly convertible to BOOLEAN
.
两个系统都支持BOOLEAN
数据类型,您可以在WHERE
或ON
子句中直接使用BOOLEAN
数据类型:
Both systems support BOOLEAN
datatype which you can use as is, without the operators, in the WHERE
or ON
clauses:
SELECT *
FROM mytable
WHERE col1
,这在SQL Server
和Oracle
中是不可能的(您需要在其中具有某种种类或谓词).
, which is impossible in SQL Server
and Oracle
(you need to have some kind or a predicate there).
在MySQL
中,BOOLEAN
是TINYINT(1)
的同义词.
在PostgreSQL
中(在存储方面),但从逻辑上讲,它不能隐式转换为任何其他类型.
In PostgreSQL
too (in terms of storage), but logically, it's not implicitly convertible to any other type.
这篇关于在设计数据库时,存储多个true/false值的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!