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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Use bit in SQL?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-07, 14:59
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
Use bit in SQL?

Hello,

I have a Table with some char(1) columns. Allowed values are y,n and NULL. I consider to change to bit. My professor told me not to do, because of some kind of incompatibility. Did'nt really get what he meant. Is that true? Are there any issues about this?

Thank you!
Reply With Quote
  #2 (permalink)  
Old 03-31-07, 16:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
one incompatibility is that it is not portable

some database systems do not support a BIT datatype, but they all support CHAR

in other words, if you build a database using BIT, and then you wish to move it to some other system, you may have to change BIT anyway

CHAR(1) and TINYINT are the two datatypes most often used

another benefit of CHAR(1) and TINYINT is flexibility and this is illustrated by the following scenario

imagine we have a BIT column called MaritalStatus, with values 0 and 1 meaning Single and Married

actually, we wouldn't, because that would be misusing the BIT datatype, which is really supposed to be used like a switch -- on/off, yes/no, 0/1

so we would have to name our BIT column IsMarried

now, would we also have another column called IsSingle? probably not, but you can imagine the programming code either way...
Code:
  IF IsMarried <> 0 ...
  IF IsSingle IS NOT TRUE ...
  IF NOT IsMarried ...
  IF IsSingle < 1 ...
  IF IsMarried = 1 ...
and then one day, we are told to change our application to incorporate divorced as a valid marital status

if we had started out with TINYINT values of 0 and 1, we could simply add 2

would any of the code need to be changed? yes, if when we wrote the code we pretended that there would never ever forever never be more than two values
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-31-07, 17:00
silas silas is offline
Registered User
 
Join Date: Mar 2007
Posts: 97
Thanks for the example. Do you think it makes sense to convert from y/n to 1/0 ?
Reply With Quote
  #4 (permalink)  
Old 03-31-07, 17:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by silas
Thanks for the example. Do you think it makes sense to convert from y/n to 1/0 ?
no, i would just leave your CHAR(1) the way it is

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-31-07, 18:43
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
TINYINT is not standardized SQL either. So that is also not portable.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 03-31-07, 18:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you are absolutely right

so what do you use for this, SMALLINT?

or CHAR(1)?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 04-01-07, 12:15
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'm in favor of CHAR(1) for many reasons, but the problem is that most people use mneumonic values like "Y" for yes and "N" for no, and those values are very locale/language specific. You also run into problems where you have mneumonic conflicts, where you'd have SINGLE, MARRIED, DIVORCED, STUPID (remarried) where the obvious answer causes a mneumonic clash (the S for single and the S for stupid conflict).

I have a very strong tendancy to use a foreign key to a lookup table for lookups like this... That allows me to validate the incoming data (so that the code can only insert values in the lookup table), and to extend the set of valid values by simply adding a new row to the lookup table. If you choose to get fancy about things, you can carry the lookup to a language/lookup table to allow you to store multiple language descriptions (via a one to many relationship) for your lookup values.

-PatP
Reply With Quote
  #8 (permalink)  
Old 04-01-07, 15:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Pat Phelan
...where the obvious answer causes a mneumonic clash (the S for single and the S for stupid conflict).
it's even worse in my case: STUPID SQUARED (married twice, divorced twice)

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 04-01-07, 16:40
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by r937
so what do you use for this, SMALLINT?
Yes, SMALLINT is in SQL:2003.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 04-01-07, 16:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
fascinating, thanks

do you happen to have copies of the standard(s)? i personally don't, and i would enjoy knowing someone who does

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 04-02-07, 08:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yes, I do. I just can't pass them along for copyright reasons.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #12 (permalink)  
Old 04-02-07, 08:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks

i wasn't asking for copies -- i just wanted to know whom to contact when i have a particular question about the standard (which is not often)

for example, which of the following is valid ...

select *, 23 from t

select t.*, 23 from t

select 23, t.* from t
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 04-03-07, 10:50
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
1 is invalid. An <asterisk> must either be qualified (as in 2 and 3), or it must be the only expression in the <select list>. 2 and 3 are fine. (Subclause 7.23, <query expression> in SQL:2003)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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