Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Storing True/False

    What is the best way to store True/False in MySQL?

    Should I use TinyInt and store {0, 1} ?

    Should I store {True, False} ?

    Should I store {TRUE, FALSE} ?


    In general, I think it is better when the values you store in a database are "self-identifying".

    For example, it is better to store Category = 'Shoes" versus Category = '479' because the former is obvious to anyone dealing with your data or database, whereas the latter requires inside knowledge.

    What do you think about my original question?


    Debbie

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 207 Times in 204 Posts
    TinyInt 0/1 especially since True/TRUE and False/FALSE equate to 1 or 0 anyway.
    Dave .... HostMonster for all of your hosting needs

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    TinyInt 0/1 especially since True/TRUE and False/FALSE equate to 1 or 0 anyway.
    They equate in MySQL or PHP or both?

    If I stored '1' in the "paid_member" column, then could I write my query like this...

    PHP Code:
    q1 "SELECT username
    FROM user
    WHERE paid_member = TRUE"


    Debbie

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Using a numerical data type is best for boolean values. I always define the field as a BOOL which in an aliases for tinyint(1) and I'm pretty sure that it only accepts 0 or 1 in the assignment, but I'd need to retest that to be absolutely sure.

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 207 Times in 204 Posts
    Quote Originally Posted by doubledee View Post
    They equate in MySQL or PHP or both?
    In PHP.
    Dave .... HostMonster for all of your hosting needs

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    In general, I think it is better when the values you store in a database are "self-identifying".
    In computing, 0 & 1 are considered to be "self-identifying". They are also much more efficient in storage and conditionals.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •