以mysql數(shù)據(jù)庫(kù)為例,判斷類型應(yīng)該用tinyint類型。
永順網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),永順網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為永順上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的永順做網(wǎng)站的公司定做!
解釋:
mysql是不支持布爾類型的,當(dāng)把一個(gè)數(shù)據(jù)設(shè)置成布爾類型的時(shí)候,數(shù)據(jù)庫(kù)會(huì)自動(dòng)轉(zhuǎn)換成tinyint(1)的數(shù)據(jù)類型,其實(shí)這個(gè)就是變相的布爾。
工具:mysql
5.6
步驟:
1、創(chuàng)建表:
create table test
(id int,
col varchar(10),
if_true tinyint(1))2、插入數(shù)據(jù):
insert into test values (1,'真',1);
insert into test values (2,'假',0);3、插入后結(jié)果:
總結(jié):默認(rèn)值也就是1,0兩種,分別對(duì)應(yīng)了布爾類型的true和false。
其實(shí)mysql是用tinyint(1)來(lái)代替bool類型的true
用tinyint(0)來(lái)代替bool類型的false
mysql是不支持bool類型的,所以,當(dāng)把一個(gè)數(shù)據(jù)設(shè)置成bool類型的時(shí)候,數(shù)據(jù)庫(kù)會(huì)自動(dòng)轉(zhuǎn)換成tinyint(1)的數(shù)據(jù)類型,其實(shí)這個(gè)就是變相的bool。 默認(rèn)值也就是1,0兩種,分別對(duì)應(yīng)了bool的true和false
數(shù)據(jù)有一個(gè)字段,用的是 tinyint 長(zhǎng)度是1 默認(rèn)值為0 ,
當(dāng)用vs2013中的 EF5來(lái)生成 實(shí)體模型之后,看到這個(gè)列被標(biāo)識(shí)為 bool 類型
Mysql官方參考文檔關(guān)于布爾類型的說(shuō)明:
BOOL, BOOLEAN
These types are synonyms(同義詞) for TINYINT(1). A value of zero is considered(認(rèn)為是) false. Nonzero(不為0) values are considered true
下面是一個(gè)老外的文章,MySQL :: MySQL Connector/Net for EF binds TINYINT(1) incorrectly 他用的是EF4.3版本,我用的是EF5版本
I believe I've discovered a bug in the way the MySQL Connector for .NET maps fields of type TINYINT(1) within Entity Framework.
The database I'm working with has a number of fields used to store "enumeration values" - basically an integer that represents a specific .NET enumeration value. ie/ Open = 1, Closed = 2
Since the enumerations contain a small number of possible values (2-5), the majority of these fields are declared as the MySQL datatype TINYINT(1). In other words, we want an integer with a minimal amount of storage space and a maximum of "one character".
When we use Entity Framework 4.3 to map these TINYINT(1) fields to an "int" data type, the integer value *always* comes back as "1", regardless of the underlying storage value. The integer values 2, 3, 4, etc all get converted to 1.
If I convert the entity property's type to "string", it receives a value of "True".
It appears as though the MySQL Connector for .NET is hardcoded to treat TINYINT(1) as a boolean, regardless of the data type it's eventually bound to. TINYINT(1) should only be converted to a boolean when it's bound to a "bool" property, and nothing else. It appears as though it's trying to simulate the behavior of the "BIT" field in SQL Server, which is a completely different thing (it's not an integer, while TINYINT is).
Is this a known issue? Should I file a bug report?
翻譯過(guò)來(lái)如下
他說(shuō),在EF4.3中,他用一個(gè)字段 Tinyint(1) 想去存一個(gè) int類型的值,不管是存 1還是2,3,4 他都是變成了1
如果他在Ef的實(shí)體中把類型改為string,到了數(shù)據(jù)庫(kù)卻變成了 “True”
這個(gè)問(wèn)題好像是發(fā)生在,通過(guò) MySql Connector For .Net這個(gè)組件來(lái)鏈接Mysql的時(shí)候發(fā)生的 .
表打開(kāi)之后看到的是數(shù)據(jù)列表,沒(méi)法看字段的類型。要查看表中每個(gè)字段的類型,選擇表之后,上面有個(gè)“設(shè)計(jì)表”,點(diǎn)擊之后就看到所有字段的類型了。
布爾值。
MySQL數(shù)據(jù)庫(kù)存儲(chǔ)布爾值,在Java編程中,我們經(jīng)常會(huì)遇到將boolean值寫(xiě)入到mysql數(shù)據(jù)庫(kù)的情況,但是,mysql數(shù)據(jù)庫(kù)是沒(méi)有boolean類型的,它的boolean值是用數(shù)字0和1來(lái)表示的。
mysql是不支持bool類型的,所以,當(dāng)把一個(gè)數(shù)據(jù)設(shè)置成bool類型的時(shí)候,數(shù)據(jù)庫(kù)會(huì)自動(dòng)轉(zhuǎn)換成tinyint的數(shù)據(jù)類型,其實(shí)這個(gè)就是變相的bool。