Unix Technical Forum

Preventing Negative values in table.

This is a discussion on Preventing Negative values in table. within the MySQL forums, part of the Database Server Software category; --> Lets say I have a table: create table test( ID int unsigned default 0); If someone was to update ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:29 AM
nopam_pcartier@atlashosting.com
 
Posts: n/a
Default Preventing Negative values in table.

Lets say I have a table:

create table test(
ID int unsigned default 0);



If someone was to update the table with the following statment:
UPDATE test SET ID = ID - 1

And the ID in the field was 0, this would cause a large numeric value
because it's an unsigned int,
is there a way to prevent this from happening, in the form of some trigger
or so.

My goal is not to have these values in the table, if the result is going to
cause invaild data, I want to somehow
prevent this.
Thanks
-Paul C

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:29 AM
Michael Fesser
 
Posts: n/a
Default Re: Preventing Negative values in table.

..oO(nopam_pcartier@atlashosting.com)

>Lets say I have a table:
>
>create table test(
> ID int unsigned default 0);
>
>
>
>If someone was to update the table with the following statment:
>UPDATE test SET ID = ID - 1
>
>And the ID in the field was 0, this would cause a large numeric value
>because it's an unsigned int,
>is there a way to prevent this from happening, in the form of some trigger
>or so.


Your application should catch that, not the DB. Don't let your users
submit such values.

Micha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:29 AM
douggunnoe@gmail.com
 
Posts: n/a
Default Re: Preventing Negative values in table.

On Oct 31, 1:38 pm, nopam_pcart...@atlashosting.com wrote:
> Lets say I have a table:
>
> create table test(
> ID int unsigned default 0);
>
> If someone was to update the table with the following statment:
> UPDATE test SET ID = ID - 1
>
> And the ID in the field was 0, this would cause a large numeric value
> because it's an unsigned int,
> is there a way to prevent this from happening, in the form of some trigger
> or so.
>
> My goal is not to have these values in the table, if the result is going to
> cause invaild data, I want to somehow
> prevent this.
> Thanks
> -Paul C


Michael Fesser is correct.

However, I am wondering what would happen if you set ID to NULL
instead of 0. If someone then tried this, UPDATE test SET ID = ID - 1,
would that not generate an invalid data type error from the DB?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:29 AM
nopam_pcartier@atlashosting.com
 
Posts: n/a
Default Re: Preventing Negative values in table.

I created a trigger, and that seemed to work, here it is below:

CREATE TRIGGER t_UpdateMemberStats BEFORE UPDATE ON member_stats
FOR EACH ROW BEGIN
IF NEW.news_comments < 0 THEN SET NEW.news_comments = 0; END IF;
IF NEW.forum_comments < 0 THEN SET NEW.forum_comments = 0; END IF;
END;

This seem to prevent negative values

-Paul

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:29 AM
ZeldorBlat
 
Posts: n/a
Default Re: Preventing Negative values in table.

On Oct 31, 8:22 pm, Michael Fesser <neti...@gmx.de> wrote:
>
> Your application should catch that, not the DB. Don't let your users
> submit such values.
>


Indeed it should, but I would never rely on the UI alone to enforce
data integrity. Unfortunately I don't think MySQL has check
constraints. Triggers are probably the next best thing.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:29 AM
nopam_pcartier@atlashosting.com
 
Posts: n/a
Default Re: Preventing Negative values in table.

Trigger was the way to go, works perfect!

Thanks for the input.

CREATE TRIGGER t_UpdateMemberStats BEFORE UPDATE ON members.member_stats
FOR EACH ROW BEGIN
IF NEW.news_comments < 0 THEN SET NEW.news_comments = 0; END IF;
IF NEW.photo_comments < 0 THEN SET NEW.photo_comments = 0; END IF;
END;

-Paul

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:05 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com