存储0,1,空值的最佳数据类型

时间:2021-12-11 16:57:36

I have three values: 0, 1, NULL. Now I want to know, what data type is fine for that column?

我有三个值:0,1,NULL。现在我想知道,该列可以使用哪种数据类型?

But the way, NULL is default of that column (in the database) and I achieve 0 and 1 form parameter (get method) of URL. Something like this:

但是方式,NULL是该列的默认值(在数据库中),我实现了URL的0和1表单参数(get方法)。像这样的东西:

www.example.com/?q=param=0

And then

接着

$var = isset($_GET['param']) ? $_GET['param'] : null;

And then

接着

INSERT INTO table(col) values ($var);

2 个解决方案

#1


8  

TINYINT(1) UNSIGNED NULL

tinyint's are perfect for booleans

tinyint非常适合布尔人

#2


11  

I would use Bit-Value Type - BIT

我会使用比特值类型 - BIT

BIT(1) NULL DEFAULT NULL

BIT(1) needs 1 Byte of storage, wich is the same as TINYINT(1) does. The difference is that BIT(1) only accepts the values 0 and 1 (or b'0' and b'1') while TINYINT(1) UNSIGNED accepts values from 0 up to 255. The length defined in the brackets for TINYINT does not take any affect to values that can be stored. It is only an information for clients, how to display the values (e.g. if you use ZEROFILL).

BIT(1)需要1个字节的存储空间,与TINYINT(1)相同。不同之处在于BIT(1)只接受值0和1(或b'0'和b'1'),而TINYINT(1)UNSIGNED接受0到255之间的值.TINYINT括号中定义的长度确实如此不会对可以存储的值产生任何影响。它只是客户的信息,如何显示值(例如,如果您使用ZEROFILL)。

#1


8  

TINYINT(1) UNSIGNED NULL

tinyint's are perfect for booleans

tinyint非常适合布尔人

#2


11  

I would use Bit-Value Type - BIT

我会使用比特值类型 - BIT

BIT(1) NULL DEFAULT NULL

BIT(1) needs 1 Byte of storage, wich is the same as TINYINT(1) does. The difference is that BIT(1) only accepts the values 0 and 1 (or b'0' and b'1') while TINYINT(1) UNSIGNED accepts values from 0 up to 255. The length defined in the brackets for TINYINT does not take any affect to values that can be stored. It is only an information for clients, how to display the values (e.g. if you use ZEROFILL).

BIT(1)需要1个字节的存储空间,与TINYINT(1)相同。不同之处在于BIT(1)只接受值0和1(或b'0'和b'1'),而TINYINT(1)UNSIGNED接受0到255之间的值.TINYINT括号中定义的长度确实如此不会对可以存储的值产生任何影响。它只是客户的信息,如何显示值(例如,如果您使用ZEROFILL)。