Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Database Design Solutions ISBN: 978-0-470-38549-4
This is the forum to discuss the Wrox book Beginning Database Design Solutions by Rod Stephens; ISBN: 9780470385494
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Database Design Solutions ISBN: 978-0-470-38549-4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 2nd, 2012, 01:28 PM
Authorized User
Points: 143, Level: 2
Points: 143, Level: 2 Points: 143, Level: 2 Points: 143, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 32
Thanks: 9
Thanked 0 Times in 0 Posts
Default CHECK constraint

I have a problem with CONSTRAINTs
This is what I am using:

mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 1.1.8 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.16 |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86 |
| version_compile_os | Win32 |
+-------------------------+------------------------------+

I tried to add a CHECK constraint for the gender. I tried both these and without success. I saw a lot of people report it as a bug, but not sure if it's a but or I do something wrong.

Code:
CHECK(gender IN ('M', 'F'));
CHECK (gender LIKE '[MF]');
I tried

Quote:
ALTER TABLE users
ADD CONSTRAINT CHECK (gender LIKE '[MF]')
as well as

Code:
CONSTRAINT ck_gender CHECK (gender in ( 'M', 'F' ) )
when define the table.

Is my syntax wrong? Thank you for your time.

Last edited by masterlayouts; May 2nd, 2012 at 01:31 PM.
  #2 (permalink)  
Old May 2nd, 2012, 04:10 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,153, Level: 23
Points: 3,153, Level: 23 Points: 3,153, Level: 23 Points: 3,153, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 643
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Sorry but both of those look valid to me. But I've seen some posts that say MySQL doesn't support CHECK constraints. See this thread:

http://forums.mysql.com/read.php?136...474#msg-152474

Be sure to read the replies. They say you may be able to use a trigger to perform the job of the CHECK constraint.

In this case you could also build a lookup table and use it as a foreign key to validate the field. That seems like serious overkill but it should work.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
masterlayouts (May 3rd, 2012)
 


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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference Constraint? jn148 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 December 29th, 2008 09:06 AM
Can I have a constraint like this.....? callagga SQL Language 7 August 13th, 2008 01:08 AM
Error adding a check constraint SQLScott SQL Server 2005 2 October 3rd, 2007 05:58 AM
CHECK Constraint Error Kep SQL Server 2000 5 April 11th, 2006 08:46 PM
constraint for 1-to-n relation j_yan MySQL 0 December 10th, 2004 10:03 PM



All times are GMT -4. The time now is 06:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.