Go Premium for a chance to win a PS4. Enter to Win

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

Check Constraints

The documentation for MS SQL7 recommends using check constraints rather than rules.

1. Can I impose a check constraint on a user-defined data type?  If so how - add the syntax to this statement to force either 'Y','N', or '' as acceptable values:

   EXEC sp_addtype yes_no, char(1), 'NULL'

2. In a table definition can a field-level check constraint refer to a field in a general lookup table (this is not a foreign key candidate)?  If so please provide specific syntax.

3. Opinion only - why would a check constraint be preferable to a rule?
0
scarlett
Asked:
scarlett
  • 3
  • 3
1 Solution
 
formulaCommented:
Here's your answer:
1)create your user defined datatype:
     EXEC sp_addtype yes_no, 'char(1)', NULL
  use it in a table and add contraint (this is table level):
    create table test( YNcolumn yes_no)
    alter table test add constraint YN check  
      (YNcolumn in ('Y','N'))
  OR
  Add constraint at table create time(This is column level):
    create table test(
    YNcolumn yes_no constraint YN check (YNcolumn in ('Y','N')))

2)A check constraint cannot compare to values in another table.  You should use a trigger to do this.

3) Rules are transact SQL and not ANSI supported. Check constraints are ANSI SQL, which would allow for more portability to other ANSI supported platforms.


0
 
scarlettAuthor Commented:
My interest in question #1 is to globally associate the values with the user-defined data type so that I don't have to declare them each time I place the field in another table.  Is this possible?  If so, what is the syntax?
0
 
formulaCommented:
Just wanted to respond and say there is no way to store the values of the datatype within the datatype definition itself.  It is the rule or constraint that associates the values.  You only have to create a rule once, but you still have to bind the rule to a column each time you create it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
scarlettAuthor Commented:
Interesting that you can bind a default to a user-defined data type but not the ability to check for acceptable values.

I'd like to give you credit for your answer and close this question, formula - could you send me an "answer" instead of a "comment".
0
 
formulaCommented:
I must take credit in leading you partially astray. Here are the steps to do what you want:

 1) EXEC sp_addtype yes_no, "char(1)", 'NULL'
 2) create rule rule_yn as @yn in ("Y","N")
 3) exec sp_bindrule rule_yn, yes_no

After this sequence, you can use the datatype "yes_no" in any table create and the rule will be applied.  You cannot use a constraint except in the context of a table create, but you can use a rule or default as above.  I suppose your original question about constraints lead me astray.  But if I redeem myself with this answer, it will be worth it.  I've actually tested and proven this on my system.


0
 
scarlettAuthor Commented:
Thank you.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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