If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Whats the best datatype to use for boolean data?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-08, 09:01
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Whats the best datatype to use for boolean data?

I need a column for status that takes only one of two values, 0 or 1. I also need a way to flip the status (similar to binary bit compliment) from 0 to 1 or 1 to 0. Can I do this in MySql? Some thing like Update _tbl set flag=!flag?

Thanks

Grav
Reply With Quote
  #2 (permalink)  
Old 10-20-08, 09:06
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
look on the manual at the bit data type

And as for complimenting the boolean; I think the answer in MySQL is to use the bang (!) symbol
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-20-08, 09:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
use TINYINT

reason? it is the easiest datatype to upgrade to 3 values (e.g. 0,1,2) with the least impact on application code

to flip the status, use
Code:
UPDATE daTable
   SET status = CASE WHEN status = 1 THEN 0 ELSE 1 END
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-20-08, 09:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Argueably, bit allows for 3 values, the third being the absence of a value, but a fair point nonetheless.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 10-20-08, 11:05
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
I tried the bang (!) on tinyint I dont think it works. I ll try it on bit datatypes.

Thanks everyone for your help.
Reply With Quote
  #6 (permalink)  
Old 10-20-08, 11:32
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Failing that, try the tilde (~)

Let us know how you get on
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 10-20-08, 14:11
gravity gravity is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
The ! doesnt work, but ~ seems to work but is behaving funny. It flipped a b'0' to b'1' but could not flip a 1 back. Must be something I am doing wrong. Anyways I opted to use tinyint(1)

with "UPDATE daTable
SET status = CASE WHEN status = 1 THEN 0 ELSE 1 END" for flipping bits as r937 suggested.

Thanks everyone
Reply With Quote
  #8 (permalink)  
Old 10-20-08, 15:09
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by georgev
Argueably, bit allows for 3 values, the third being the absence of a value, but a fair point nonetheless.
I always thought the absence of a value is represented with NULL in an RDBMS...
Reply With Quote
  #9 (permalink)  
Old 10-20-08, 16:11
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
That's what I was hinting at shammat, but I see your finicky point
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On