显示标签为“constraints”的博文。显示所有博文
显示标签为“constraints”的博文。显示所有博文

2012年3月19日星期一

fk constraints question

any way to turn off all foreign keys in a database and then turn them
back on after a few update statements are run?for each table you'd have to
alter table mytable nocheck constraint all
This will turn off check and FK constraints on the table... you could
combine this with
sp_msforeachtable ( if I remember the name correctly )
or generate the script ie
select 'alter table ' + name + ' nocheck constraint all' from
sysobjects where type = 'u' and id > 100
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
"ch" <ch@.dontemailme.com> wrote in message
news:416BC63E.F3CE4029@.dontemailme.com...
> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?|||You can use ALTER TABLE on a table that has foreign key
constraints to disable the constraint, e.g.
ALTER TABLE table_name NOCHECK CONSTRAINT constraint_name
You can find more information and an example in books online
under ALTER TABLE.
-Sue
On Tue, 12 Oct 2004 06:55:42 -0500, ch <ch@.dontemailme.com>
wrote:
>any way to turn off all foreign keys in a database and then turn them
>back on after a few update statements are run?|||Hi,
SP_MSFOREACHTABLE 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
And later...
SP_MSFOREACHTABLE 'ALTER TABLE ? CHECK CONSTRAINT ALL'
Hermilson Tinoco.
"ch" wrote:
> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?
>

fk constraints question

any way to turn off all foreign keys in a database and then turn them
back on after a few update statements are run?
for each table you'd have to
alter table mytable nocheck constraint all
This will turn off check and FK constraints on the table... you could
combine this with
sp_msforeachtable ( if I remember the name correctly )
or generate the script ie
select 'alter table ' + name + ' nocheck constraint all' from
sysobjects where type = 'u' and id > 100
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
"ch" <ch@.dontemailme.com> wrote in message
news:416BC63E.F3CE4029@.dontemailme.com...
> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?
|||You can use ALTER TABLE on a table that has foreign key
constraints to disable the constraint, e.g.
ALTER TABLE table_name NOCHECK CONSTRAINT constraint_name
You can find more information and an example in books online
under ALTER TABLE.
-Sue
On Tue, 12 Oct 2004 06:55:42 -0500, ch <ch@.dontemailme.com>
wrote:

>any way to turn off all foreign keys in a database and then turn them
>back on after a few update statements are run?
|||Hi,
SP_MSFOREACHTABLE 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
And later...
SP_MSFOREACHTABLE 'ALTER TABLE ? CHECK CONSTRAINT ALL'
Hermilson Tinoco.
"ch" wrote:

> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?
>

fk constraints question

any way to turn off all foreign keys in a database and then turn them
back on after a few update statements are run?for each table you'd have to
alter table mytable nocheck constraint all
This will turn off check and FK constraints on the table... you could
combine this with
sp_msforeachtable ( if I remember the name correctly )
or generate the script ie
select 'alter table ' + name + ' nocheck constraint all' from
sysobjects where type = 'u' and id > 100
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
"ch" <ch@.dontemailme.com> wrote in message
news:416BC63E.F3CE4029@.dontemailme.com...
> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?|||You can use ALTER TABLE on a table that has foreign key
constraints to disable the constraint, e.g.
ALTER TABLE table_name NOCHECK CONSTRAINT constraint_name
You can find more information and an example in books online
under ALTER TABLE.
-Sue
On Tue, 12 Oct 2004 06:55:42 -0500, ch <ch@.dontemailme.com>
wrote:

>any way to turn off all foreign keys in a database and then turn them
>back on after a few update statements are run?|||Hi,
SP_MSFOREACHTABLE 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
And later...
SP_MSFOREACHTABLE 'ALTER TABLE ? CHECK CONSTRAINT ALL'
Hermilson Tinoco.
"ch" wrote:

> any way to turn off all foreign keys in a database and then turn them
> back on after a few update statements are run?
>

FK Checks

Suppose you have two (or more) tables with foreign key constraints. My
question is thus:

Is it better to check if the fk exists before you try to perform the
insert or let SQL do it for you?

On one hand, if you check yourself and the key does not exist you can
gracefully handle it (maybe exit out of method with error). If you let
SQL do it, the server will throw an error which cannot be suppressed.

On the performance side, you doing the check will incur a slight (VERY
slight) hit since SQL will ALSO check anyways.Jason (JayCallas@.hotmail.com) writes:
> Suppose you have two (or more) tables with foreign key constraints. My
> question is thus:
> Is it better to check if the fk exists before you try to perform the
> insert or let SQL do it for you?

Depends on business requirements. Basically, Fkeys is the database's
mean of protection against bad data. If the user interface does not
perform any checks itself, and relies on the database, you may avoid
integrity violations, but the users may not get adequate error message.

The way I see database constraints constitutes an inner defense line.
The user interface should help the user, and have its own defense line.
Error messages from the database exposed to the user, should be considered
a bug.

But validation in a GUI, can be quite different from the validation in
SQL Server. Normally you don't let the user to type in the FK as free-
text, but you let him choose from a drop-down box or from a search screen.
Once you know, the user has selected data this way, you can assume that
you are safe. (Although, someone may delete the row before the users
saves.)

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland Sommarskog <sommar@.algonet.se> wrote in message news:<Xns944C1D0487E5Yazorman@.127.0.0.1>...
> Depends on business requirements. Basically, Fkeys is the database's
> mean of protection against bad data. If the user interface does not
> perform any checks itself, and relies on the database, you may avoid
> integrity violations, but the users may not get adequate error message.
> The way I see database constraints constitutes an inner defense line.
> The user interface should help the user, and have its own defense line.
> Error messages from the database exposed to the user, should be considered
> a bug.
> But validation in a GUI, can be quite different from the validation in
> SQL Server. Normally you don't let the user to type in the FK as free-
> text, but you let him choose from a drop-down box or from a search screen.
> Once you know, the user has selected data this way, you can assume that
> you are safe. (Although, someone may delete the row before the users
> saves.)

I should have been more explicit in my question. I was referring to
performing checks within stored procedures.

Assuming Table2 has a FK relationship with Table1.

IF EXISTS (SELECT * FROM Table1 WHERE Table1.Field1 = @.field1)
INSERT INTO Table2 (@.field1, @.field2, field3)

as opposed to

INSERT INTO Table2 (@.field1, @.field2, field3)
IF @.@.Error RAISERROR('Unable to insert into Table2 because of missing
constraint', 15, 1)|||Jason (JayCallas@.hotmail.com) writes:
> I should have been more explicit in my question. I was referring to
> performing checks within stored procedures.
> Assuming Table2 has a FK relationship with Table1.
> IF EXISTS (SELECT * FROM Table1 WHERE Table1.Field1 = @.field1)
> INSERT INTO Table2 (@.field1, @.field2, field3)
> as opposed to
> INSERT INTO Table2 (@.field1, @.field2, field3)
> IF @.@.Error RAISERROR('Unable to insert into Table2 because of missing
> constraint', 15, 1)

Again it depends. Say that your procedure is to be called from a GUI. In
this case you can presume that the GUI gets the information from the
databaes, and a foreign-key violation could only occur if the GUI is
incorrect. On the other hand, say that you are accepting data from an
external source over which you have no control. In this case, it is better
to check explicitly, so that you can log incorrect data appropriately.
Recall that when a constraint blows up, you cannot in T-SQL determine
which constraint that fired.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp