Gert-Jan Strik
3/16/2007 4:29:00 PM
Basically, a CHECK constraint can only reference the columns of that
row. Since no other rows can be accessed, no aggregates are possible.
For the same reason you cannot use a CHECK constraint to limit the
number of rows in a table. For that you would need an ASSERTION, which
SQL Server does not support.
You are correct that constraints cannot be deferred in SQL Server. The
same is true for triggers.
As mentioned by others, you could use a trigger to enforce a maximum
number of rows in a table. I don't see a trigger or constraint based
solution for you second requirement though.
HTH,
Gert-Jan
qwerty2_reverse_iterator wrote:
>
> 1) Are aggregate functions allowed in CHECK constraints?
> Basically, I would like to use an aggregate function, like COUNT, with a
> CHECK constraint for a table. But when I try to do it via MS-SQL Mngmnt
> Studio Express, I get an error. Basically I right click on a table in the
> design view, choose "Check Constraints" option, and add the following:
>
> COUNT(column_name) < 10
>
> in the "Expression" text box. But I get an error in validation.
>
> 2) I have two tables in my database, Table1 and Table2. After the end of
> some series of transactions (ie, the series of transactions is to be viewed
> as an atomic transaction itself), I want to verify that the number of rows in
> Table1 equals the number of rows in Table2. Is there a way I can do this via
> constraints? (At first I thought of using the initially deferred construct
> but it seems TSQL doesn't support yet.)
>
> Thank you in advance.