SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating a Check Constraint


Creating a Check Constraint

Author
Message
daniel-703741
daniel-703741
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 12
Hello. I am obviously very new to SQL Server 2005 Express. I am an Access developer and I am giving a try to SQL Server Express. Maybe my question will be answered faster this way 8)

I am going through a book (... In 24 Hours series) and every time I try to complete a simple exercise about creating a (seems like a) Check Constraint I get an error. Here's what I am doing:

The field is State: Char (2) and we want to apply the constraint where only the following values are accepted: CA, UT, AZ WY, and a few others.

My steps:
I click the selected to the left of the State field and click Manage Check Constraints.
In the Check Constraint dialog I click Add and the constraint CK_Table_1* appears. The name is simply CK_Table_1.
I name the constraint ckState (like in the book)
In the Expression I type @State In('CA', 'UT', 'AZ', 'WY') and as soon as I hit Enter a message appears that there is an error in the constraint and ask me if I want to Edit it.

I am at loss here because this matches the screen shot in the book.

Can anyone help?

I hope I provided enough information.
Michael Earl-395764
Michael Earl-395764
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21699 Visits: 23078
Remove the @ symbol

State In('CA', 'UT', 'AZ', 'WY')

"State" is referring to your field name.
daniel-703741
daniel-703741
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 12
Thank you so much Michael,

The book does specify the @ but it is incorrect! Thank you again. I will remember.

Daniel
zgembo
zgembo
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 235
Remove @ from your statement( @State In('CA', 'UT', 'AZ', 'WY'))
Michael Earl-395764
Michael Earl-395764
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21699 Visits: 23078
don't believe everything you read.
marina.x.rotua
marina.x.rotua
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 22
hello, I'm a beginner in SQL Server 2005, i'm trying to create check constraint, and when i'm trying to create it, an error message was appear.
my steps are:
1) The field is ZIP using nchar(5),
2) in the Expression I type: zip LIKE '[0-9][0-9][0-9][0-9][0-9]'
3) When I click Save button, there was an error message and unable to save the changes

kindly help...
GilaMonster
GilaMonster
SSC Guru
SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)

Group: General Forum Members
Points: 369989 Visits: 46950
Please post new questions in a new thread. Thanks.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


kaza_rohan
kaza_rohan
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 104
Check:
Check limits the values that are accepted by one or more columns.
The Below code accepts only values A or k or R or S, it will not accept other than this.
USE Rohan
GO
CREATE TABLE CHEECK (ID INT, SHORT VARCHAR(2) CHECK (SHORT LIKE '[A,K,R,S]'))


Thanks,
Rohan k
UsefulSite:
CheckConstraint
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search