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

    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.

    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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!
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    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.

    -PatP

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    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
  •