Showing posts with label cola. Show all posts
Showing posts with label cola. Show all posts

Thursday, March 8, 2012

check constraint

Hi ,
Could a check constraint check for another column i.e u
need to ensure that either ColA OR ColB must NOT be null
when entering the information into the table ?
or a trigger is a better solution ?
thks & rdgs
You can use a table-level CHECK constraint to enforce this rule. For
example:
CREATE TABLE MyTable
(
ColA int NULL,
ColB int NULL,
ColC int NULL,
CONSTRAINT CK_ColC
CHECK (ColA IS NOT NULL OR ColB IS NOT NULL)
)
Hope this helps.
Dan Guzman
SQL Server MVP
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs
|||Sure, you could do it with a check constraint... either of the following
syntax:
CREATE TABLE #foo(A INT,
B INT,
CHECK (A IS NOT NULL OR B IS NOT NULL))
...or...
CREATE TABLE #foo(A INT,
B INT)
ALTER TABLE #foo
ADD CONSTRAINT A_B_NotNULL CHECK (A IS NOT NULL OR B IS NOT NULL)
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs
|||The reason a check constraint is the better choice (even though you could do
the same thing in a trigger), is that the Check constraint works faster..
In general, if you can get something done in a check constraint, do it
there... Only when check constraints can't do the job do we go into
triggers.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs

check constraint

Hi ,
Could a check constraint check for another column i.e u
need to ensure that either ColA OR ColB must NOT be null
when entering the information into the table ?
or a trigger is a better solution ?
thks & rdgsYou can use a table-level CHECK constraint to enforce this rule. For
example:
CREATE TABLE MyTable
(
ColA int NULL,
ColB int NULL,
ColC int NULL,
CONSTRAINT CK_ColC
CHECK (ColA IS NOT NULL OR ColB IS NOT NULL)
)
--
Hope this helps.
Dan Guzman
SQL Server MVP
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs|||Sure, you could do it with a check constraint... either of the following
syntax:
CREATE TABLE #foo(A INT,
B INT,
CHECK (A IS NOT NULL OR B IS NOT NULL))
...or...
CREATE TABLE #foo(A INT,
B INT)
ALTER TABLE #foo
ADD CONSTRAINT A_B_NotNULL CHECK (A IS NOT NULL OR B IS NOT NULL)
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs|||>--Original Message--
>Hi ,
> Could a check constraint check for another column i.e u
>need to ensure that either ColA OR ColB must NOT be null
>when entering the information into the table ?
> or a trigger is a better solution ?
>thks & rdgs
>.
>|||The reason a check constraint is the better choice (even though you could do
the same thing in a trigger), is that the Check constraint works faster..
In general, if you can get something done in a check constraint, do it
there... Only when check constraints can't do the job do we go into
triggers.
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs

check constraint

Hi ,
Could a check constraint check for another column i.e u
need to ensure that either ColA OR ColB must NOT be null
when entering the information into the table ?
or a trigger is a better solution ?
thks & rdgsYou can use a table-level CHECK constraint to enforce this rule. For
example:
CREATE TABLE MyTable
(
ColA int NULL,
ColB int NULL,
ColC int NULL,
CONSTRAINT CK_ColC
CHECK (ColA IS NOT NULL OR ColB IS NOT NULL)
)
Hope this helps.
Dan Guzman
SQL Server MVP
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs|||Sure, you could do it with a check constraint... either of the following
syntax:
CREATE TABLE #foo(A INT,
B INT,
CHECK (A IS NOT NULL OR B IS NOT NULL))
...or...
CREATE TABLE #foo(A INT,
B INT)
ALTER TABLE #foo
ADD CONSTRAINT A_B_NotNULL CHECK (A IS NOT NULL OR B IS NOT NULL)
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs|||The reason a check constraint is the better choice (even though you could do
the same thing in a trigger), is that the Check constraint works faster..
In general, if you can get something done in a check constraint, do it
there... Only when check constraints can't do the job do we go into
triggers.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"maxzsim" <anonymous@.discussions.microsoft.com> wrote in message
news:bdd801c48977$13af8500$a401280a@.phx.gbl...
> Hi ,
> Could a check constraint check for another column i.e u
> need to ensure that either ColA OR ColB must NOT be null
> when entering the information into the table ?
> or a trigger is a better solution ?
> thks & rdgs