MySQL Forums
Forum List  »  InnoDB

Constraint Check Workarounds
Posted by: Travis Gockel
Date: December 31, 2008 09:58AM

The way I do a constraint check is with TRIGGER (see : http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html ).

Basically, for your table, you would say:

DELIMITER $$
CREATE TRIGGER trig_gvars_gfirst BEFORE UPDATE ON gvars
FOR EACH ROW BEGIN
IF NEW.name NOT LIKE 'g%' THEN
SET NEW.name = 1 / 0;
END IF;
END $$

DELIMITER ;


Basically, the line "SET NEW.name = 1 / 0;" will occur and force an error any time the constraint is not satisfied. That seems to be the best way to cause the insertions not to happen. Quite dirty -- but it beats just getting ignored by the database engine. This also doesn't work if you're updating through FOREIGN KEY actions, such as CASCADE or UPDATE, as TRIGGERs are not called for these actions. So watch out for that.

Another solution would be to restrict insertion of data into tables you would like to constrain through user-defined functions and procedures. This requires enumerating every possible action scheme you might want to take, so it is generally not the best option.

Cheers!

Options: ReplyQuote


Subject
Views
Written By
Posted
100468
May 09, 2007 01:54AM
39068
May 28, 2007 05:05PM
31263
June 07, 2007 07:31PM
27914
June 21, 2007 10:58PM
26130
June 28, 2007 03:02AM
24212
December 06, 2007 02:01PM
16773
October 15, 2008 04:08AM
16606
November 15, 2008 01:23AM
12170
November 15, 2008 11:09AM
Constraint Check Workarounds
18498
December 31, 2008 09:58AM
12693
January 06, 2009 03:41PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.