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 > forcing numbers in increments of .5

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-08, 08:14
sammydafish sammydafish is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Question 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
Reply With Quote
  #2 (permalink)  
Old 01-25-08, 11:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-25-08, 13:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
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
Reply With Quote
  #4 (permalink)  
Old 01-29-08, 10:21
aschk aschk is offline
Registered User
 
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).
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