Link to home
Start Free TrialLog in
Avatar of Mark
MarkFlag for Ireland

asked on

SQL Select Query not working

Hi,

I'm trying to add a Constraint to my student table in my MySQL database ensuring a student’s age is 17 or older but it isn't working.
Can you please help me?

ALTER TABLE student
MODIFY AGE CONSTRAINT check check_age CHECK (AGE >= 17);
Capture.JPG
Avatar of Carlo-Giuliani
Carlo-Giuliani
Flag of Canada image

You need to precede this statement with a "USE db_name " statement.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

I'm altering the table in order to add the constraint!! What's the soluation?
I think GaryC123 just told you, makel2 ! Take a second look at his code - it's an ADD, not a MODIFY that he's using.

hth

Mike
MySQL doesn't support CHECK constraints yet.  You need to use triggers to emulate the functionality.  I answered a similar question at https://www.experts-exchange.com/questions/27714612/MySQL-trigger-double-restriction.html, but it looks like there are now more elegant solutions, especially for MySQL 5.5.x, though still using triggers.  Kinda busy right now but do take a look at the alternative solutions posted at http://stackoverflow.com/questions/24/throw-an-error-in-a-mysql-trigger
Avatar of Mark

ASKER

Thanks Mike/GaryC123 half asleep last night!
Please note that MySQL will accept the syntax for adding CHECK constraints, but it won't actually enforce it.