473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Any possibility of creating a MUTUALLY dependent CHECK

Hi,

DDL:
-- create table #task (taskID int identity(1,1) primary key, taskName
varchar(25) unique, taskCompleteDate dateTime, taskComplete bit
default(0));
/*
Business Rules:
a) if taskCompleteDate is NULL (like default) then, taskComplete may
not be set to 1 (attempt to update it to 1 would fail);
b) else automatically set taskComplete = 1
*/
I was thinking using CHECK constraint (mutual constraint if possible),
along the following line:
CHECK (if taskCompleteDate is null set taskComplete=0 else set
taskComplete=1)

Hmm, your thought?
Thanks.
Jul 20 '05 #1
5 2802
Why not just eliminate the transitive dependency? The taskcomplete column is
redundant unless you wanted to allow a case where taskcomplete = 1 but the
date is NULL (you know the task is complete but the date is unknown). Since
your business rule excludes that case I would just put the taskcomplete
column in a view:

CREATE SomeView AS
SELECT ... taskcompletedate, CASE WHEN taskcompletedate IS NULL
THEN 1 ELSE 0 END, ...

Also, why so many nullable columns? Aparently you don't have a natural
primary key (should it be Taskname?).

In answer to your question, yes you can create a mutually dependent
constraint, but the purpose of a constraint is to validate data not to
change it:

.... taskcompletedate DATETIME, taskcomplete BIT NOT NULL DEFAULT 0, CHECK
((taskcompletedate IS NULL AND taskcomplete=0) OR (taskcompletedate IS NOT
NULL AND taskcomplete=1))...

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
qw********@boxfrog.com (Doug Baroter) wrote in message news:<fc**************************@posting.google. com>...
Hi,

DDL:
-- create table #task (taskID int identity(1,1) primary key, taskName
varchar(25) unique, taskCompleteDate dateTime, taskComplete bit
default(0));
/*
Business Rules:
a) if taskCompleteDate is NULL (like default) then, taskComplete may
not be set to 1 (attempt to update it to 1 would fail);
b) else automatically set taskComplete = 1
*/
I was thinking using CHECK constraint (mutual constraint if possible),
along the following line:
CHECK (if taskCompleteDate is null set taskComplete=0 else set
taskComplete=1)

Hmm, your thought?
Thanks.

You could implement the CHECK constraint like this:

create table #task (
taskID int identity(1,1) primary key,
taskName varchar(25) unique,
taskCompleteDate dateTime,
taskComplete bit default(0),
constraint CHK_taskComplete check ((taskCompleteDate is null and
taskComplete = 0) or (taskCompleteDate is not null and taskComplete =
1))
);

Or use a computed column:

create table #task (
taskID int identity(1,1) primary key,
taskName varchar(25) unique,
taskCompleteDate dateTime,
taskComplete as case when taskCompleteDate is not null then 1 else 0
end
);

The taskComplete flag seems to be redundant, given that you can use
taskCompleteDate for the same purpose, but presumably there's a reason
for this in your environment.

Simon
Jul 20 '05 #3
Thanks. I was losing the link between "check" and "contraint" :)

sq*@hayes.ch (Simon Hayes) wrote in message news:<60**************************@posting.google. com>...
qw********@boxfrog.com (Doug Baroter) wrote in message news:<fc**************************@posting.google. com>...
Hi,

DDL:
-- create table #task (taskID int identity(1,1) primary key, taskName
varchar(25) unique, taskCompleteDate dateTime, taskComplete bit
default(0));
/*
Business Rules:
a) if taskCompleteDate is NULL (like default) then, taskComplete may
not be set to 1 (attempt to update it to 1 would fail);
b) else automatically set taskComplete = 1
*/
I was thinking using CHECK constraint (mutual constraint if possible),
along the following line:
CHECK (if taskCompleteDate is null set taskComplete=0 else set
taskComplete=1)

Hmm, your thought?
Thanks.

You could implement the CHECK constraint like this:

create table #task (
taskID int identity(1,1) primary key,
taskName varchar(25) unique,
taskCompleteDate dateTime,
taskComplete bit default(0),
constraint CHK_taskComplete check ((taskCompleteDate is null and
taskComplete = 0) or (taskCompleteDate is not null and taskComplete =
1))
);

Or use a computed column:

create table #task (
taskID int identity(1,1) primary key,
taskName varchar(25) unique,
taskCompleteDate dateTime,
taskComplete as case when taskCompleteDate is not null then 1 else 0
end
);

The taskComplete flag seems to be redundant, given that you can use
taskCompleteDate for the same purpose, but presumably there's a reason
for this in your environment.

Simon

Jul 20 '05 #4
"why so many nullable columns?" you're right.
It's not an actual table, just to get a point across for the question. Thanks.

"David Portas" <RE****************************@acm.org> wrote in message news:<Gf********************@giganews.com>...
Why not just eliminate the transitive dependency? The taskcomplete column is
redundant unless you wanted to allow a case where taskcomplete = 1 but the
date is NULL (you know the task is complete but the date is unknown). Since
your business rule excludes that case I would just put the taskcomplete
column in a view:

CREATE SomeView AS
SELECT ... taskcompletedate, CASE WHEN taskcompletedate IS NULL
THEN 1 ELSE 0 END, ...

Also, why so many nullable columns? Aparently you don't have a natural
primary key (should it be Taskname?).

In answer to your question, yes you can create a mutually dependent
constraint, but the purpose of a constraint is to validate data not to
change it:

... taskcompletedate DATETIME, taskcomplete BIT NOT NULL DEFAULT 0, CHECK
((taskcompletedate IS NULL AND taskcomplete=0) OR (taskcompletedate IS NOT
NULL AND taskcomplete=1))...

Jul 20 '05 #5
Oops, I did a slightly different implementation on an existing table,
so, I created a check constraint via EM, the contraint reads:
([JobEndDate] is null and [JobComplete] = 0 OR [JobEndDate] is not
null and [JobComplete] = 1)

/*
consider job in place of task, thanks;
I thought with the check constraint, if JobEndDate is updated with a
value then JobComplete column would be automatically updated with
value of 1, didn't seem to work this way
*/

Attempt to update JobEndDate failed due to the constraint, err msg:
UPDATE statement conflicted with TABLE CHECK constraint
'CK_JobDateStatus'

Appreciated.
You could implement the CHECK constraint like this:

create table #task (
taskID int identity(1,1) primary key,
taskName varchar(25) unique,
taskCompleteDate dateTime,
taskComplete bit default(0),
constraint CHK_taskComplete check ((taskCompleteDate is null and
taskComplete = 0) or (taskCompleteDate is not null and taskComplete =
1))
);

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: wooks | last post by:
I have defined a schema with an xsd:choice element for 2 mutually exclusive fields. When both are present I get an error which is good, but what is not so good is the error message which says...
4
by: pbj | last post by:
my application was created in vb.net, vs 2003. the setup project was also created in vs 2003. one of my application reviewers reported this: The .Net link that is included in the setup is not the...
10
by: Brad Kartchner | last post by:
In a project I am working on, I have found myself with two classes that reference each other. For example: firstclass.h: { class FirstClass { function (SecondClass* Pointer); } }
14
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
3
by: vipin | last post by:
Hi, I have created c# code using wsdl.exe from a wsdl file. But I want to use the functionality from c/c++ code. Is it possible to convert the c# code to COM classes or something similar which...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
2
by: mscertified | last post by:
I have 2 drop-downs on my web form. I need to make the contents of the second one dependent on the choice from the first. How? I tried removing the existing entries from the second listbox and...
8
by: arun | last post by:
Hi Can any one suggest me how to access the mutually exclusive check boxes in a table of 7 rows by 14 columns. Only one can be selected in each row. Other than checking each one with if...
2
by: arun | last post by:
Hi Can any one suggest me how to access the mutually exclusive check box extender in a table of 7 rows by 14 columns. Only one can be selected in each row. Other than checking each one with if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.