Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004

    Question Unanswered: forcing numbers in increments of .5

    I have the need to record quantities that would never be more or less than a half. Normal data would be an integer value, but in some cases I may have to record a .5 value. Is there a way I can force mySql to accept only values evenly divided by .5? I'll do this checking in my application too, but I want to ensure the data in the table is always correct.


  2. #2
    Join Date
    Apr 2002
    Toronto, Canada
    store the actual value multipled by 2 in an integer column, and create a view on the table in which the column is multiplied by 0.5 to give the result

    vwalah! | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    In front of the computer
    Provided Answers: 54
    If some future version of MySQL supported CHECK CONSTRAINTS, then you could use one of them and store the data as you'd actually use it. Be wary, because MySQL will happily parse check constraints, but it does not currently enforce them.

    For all present versions of MySQL, I think that r937's suggestion is as good as you can get.


  4. #4
    Join Date
    Mar 2007
    Actually there's no reason why you can't write a trigger that does this test for you. Thus omitting the new record if it's bad (or logging it into an errors table).

Posting Permissions

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