Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

MYSQL Constraint Check

I've been trying to get the CHECK working in MYSQL and from what I am reading it's not yet been implemented.

How would I go about getting this to work:

ALTER TABLE contracts
ADD CONSTRAINT contract_check CHECK (value >500 )
0
directxBOB
Asked:
directxBOB
  • 2
1 Solution
 
Roger BaklundCommented:
As you have allready read, it is not implemented, alas, you can not get it to work.
0
 
directxBOBAuthor Commented:
is there any form of work around? Perhaps a trigger mechanism.
0
 
Roger BaklundCommented:
Yes, you could implement triggers to handle this. You need BEFORE INSERT and BEFORE UPDATE triggers. Something like this:
CREATE TRIGGER contracts_ins_trigger BEFORE INSERT on contracts
  FOR EACH ROW set new.value=if(new.value<500,500,new.value); 
 
CREATE TRIGGER contracts_upd_trigger BEFORE UPDATE on contracts
  FOR EACH ROW set new.value=if(new.value<500,500,new.value); 

Open in new window

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now