PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Preventing Negative values in table.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Preventing Negative values in table.

Réponse
 
LinkBack Outils de la discussion
Vieux 31/10/2007, 19h38   #1
nopam_pcartier@atlashosting.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 01/11/2007, 01h22   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 01/11/2007, 02h36   #3
douggunnoe@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?

  Réponse avec citation
Vieux 01/11/2007, 14h28   #4
nopam_pcartier@atlashosting.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 01/11/2007, 14h58   #5
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 02/11/2007, 17h39   #6
nopam_pcartier@atlashosting.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h59.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15083 seconds with 14 queries