Book a Demo

Author Topic: Constraints  (Read 3751 times)

mariec

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Constraints
« on: February 15, 2005, 01:16:59 am »
Hi,

How can you create column constraints when designing a database model and get this constraint to be created when generating the DDL file?

For example creating a CHECK constraint where the column value must be between 0 and 20.

thanks,
« Last Edit: February 15, 2005, 01:41:30 am by mariec »

David ODriscoll

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Constraints
« Reply #1 on: February 19, 2005, 01:36:49 am »
Marie

I needed to do the same this afternoon.  After some experimentation, I found that you need to do the following:

1.  Create a new operation on the target table
2.  Set the stereotype to "check"
3.  Select the column affected (eg Status)
4.  Put the condition in the "Notes" field on the General tab.  For example, "IN (1,2,3)"

This yields DDL of:
ALTER TABLE <table>
ADD CONSTRAINT CK_<table>_<column> CHECK (Status IN (1, 2, 3))

I had trouble using the BETWEEN clause, but maybe this will help you track down the solution.  If so, please let me know.

Regards
David O'Driscoll
Brisbane, Australia


Regards
David O'Driscoll
Brisbane, Australia

mariec

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Constraints
« Reply #2 on: February 20, 2005, 11:03:25 pm »
Thanks! It works fine. I followed your exact explaination and I got this:

ALTER TABLE <table>
ADD CONSTRAINT <constraint_name> CHECK (<column> BETWEEN 0 AND 20);

Just didn't think the "notes" field was used for this type of operation.

Thanks again, this saves me from doing constraints manually!

sorayanasar

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Constraints
« Reply #3 on: April 25, 2005, 11:56:34 am »
Hi everybody!

I've done what you said about the check constraints and it worked fine! But now I have to make the same thing but with multiple fields. I mean, one constraint that checks something like this:

(FIELD_A IS NULL AND FIELD_B IS NOT NULL AND FIELD_C IS NOT NULL) OR
(FIELD_A IS NOT NULL AND FIELD_B IS NULL AND FIELD_C IS NULL)

In fact, I've already done it in this way:
1) create the constraint
2) setting the above clause in the notes but without setting any field in the columns tab.

Is there another way to do that?
Thanks!!!
Soraya

mariec

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Constraints
« Reply #4 on: April 25, 2005, 11:16:18 pm »
Hi,

you can follow the same steps as decribed above, but skip the "column selection" part. Just put the check condition in the notes part and include the column names in there (exactly like your example).

It should give you something like this:

ALTER TABLE TestTable
ADD CONSTRAINT CK_Test CHECK ( (FIELD_A IS NULL AND FIELD_B IS NOT NULL AND FIELD_C IS NOT NULL) OR (FIELD_A IS NOT NULL AND FIELD_B IS NULL AND FIELD_C IS NULL))
;