/    Sign up×
Community /Pin to ProfileBookmark

Use of Boolean – more efficient ?

In a db is using a boolean field more efficient that using a char field with one chracter as an
indicator flag ?

This is especially relevent when I only have two options “yes or no”.

eg. if I use a single char field callad flag1, I can put a “y” in it and test it
with a WHERE eg

$sql = “SELECT * FROM stuff WHERE flag1 =”y”

IF I used a boolean field – lets say flag2, and set it to true ( how do I do that ?)
How do I test it in the WHERE statement ?

$sql = “SELECT * FROM stuff WHERE flag2 ???

Anyway – if I do this is it somehow better or more efficient ?

Thanks – you can guess that I haven’t used bool. values much !

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 31.2005 — uses less space but can be confusing with the pressence of true, I much prefare enums.
Copy linkTweet thisAlerts:
@NogDogJan 31.2005 — In MySQL:
<i>
</i>mysql&gt; SELECT TRUE, True, true, FALSE, False, false;
--&gt; 1, 1, 1, 0, 0, 0

So, to check if a boolean field is set to true, any of the following would work:
<i>
</i>SELECT * FROM my_table WHERE boolean_column = TRUE;
SELECT * FROM my_table WHERE boolean_column = true;
SELECT * FROM my_table WHERE boolean_column = 1;

If the purpose of a column is to record a boolean value/condition, then I am an advocate of using a boolean type: it makes your data design more self-documenting.
Copy linkTweet thisAlerts:
@DaiWelshJan 31.2005 — I agree with NogDog (usually do) that if it is fundamentally boolean data it should be in a boolean field.

As far as performance goes, I am not sure about MySQL but I seem to remember that MSSQL treats bit flags fundamentally differently, using a bit map rather than the usual search methods, therefore I would indeed expect it to perform better.

Even if I misremembered that, common sense says that it is possible to search binary fields more efficiently than character fields so I would be surprised if a well written db like mysql did not do so.

HTH,

Dai
Copy linkTweet thisAlerts:
@ShrineDesignsJan 31.2005 — i set the field type to INT(1) DEFAULT '0' it makes test true or false a little easier becase true == 1 and false == 0

to my knowledge MySQL does not have a field type of BOOLEAN
Copy linkTweet thisAlerts:
@NogDogJan 31.2005 — [i]Originally posted by ShrineDesigns [/i]

[B]i set the field type to INT(1) DEFAULT '0' it makes test true or false a little easier becase true == 1 and false == 0



to my knowledge MySQL does not have a field type of BOOLEAN [/B]
[/QUOTE]

Well, it sort of has a boolean type. From http://dev.mysql.com/doc/mysql/en/numeric-type-overview.html .

BIT , BOOL , BOOLEAN

These are synonyms for TINYINT(1). The BOOLEAN synonym was added in MySQL 4.1.0. A value of zero is considered false. Non-zero values are considered true.

In the future, full boolean type handling will be introduced in accordance with standard SQL.[/quote]
Copy linkTweet thisAlerts:
@ShrineDesignsJan 31.2005 — BIT , BOOL , BOOLEAN

These are synonyms for TINYINT(1). The BOOLEAN synonym was added in MySQL 4.1.0. A value of zero is considered false. Non-zero values are considered true.

In the future, full boolean type handling will be introduced in accordance with standard SQL.[/quote]
hmm... mysql returns strings values, are they saying that future MySQL engines COULD return non-string values, like: integers, float/double floating integers, objects?!? that would be quite interesting
×

Success!

Help @DaveinLondon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...