FK constraints

FK constraints

Post by Stephen How » Fri, 22 Mar 2002 06:22:35



Using SQL Server 7.0, SP3

I thought of a way of speeding up these mass inserts that done for the app
we have built.
Basically do

Begin tran
alter table table1 nocheck constraint FK_table1_table2
insert into table1 values(...)
:
insert into table1 values(...)
alter table table1 check constraint FK_table1_table2
commit tran

The snag is when "alter table table1 check constraint FK_table1_table2" is
re-applied, all those rows that were inserted are not re-checked.

Stephen Howe

 
 
 

FK constraints

Post by Joel Aske » Fri, 22 Mar 2002 06:58:19


Stephen:

You seem to be aware of the trade-offs between referential integrity
checking and performance.
Is your question how to not have to make the choice between the two?  That
is, how can you have it both ways?


Quote:> Using SQL Server 7.0, SP3

> I thought of a way of speeding up these mass inserts that done for the app
> we have built.
> Basically do

> Begin tran
> alter table table1 nocheck constraint FK_table1_table2
> insert into table1 values(...)
> :
> insert into table1 values(...)
> alter table table1 check constraint FK_table1_table2
> commit tran

> The snag is when "alter table table1 check constraint FK_table1_table2" is
> re-applied, all those rows that were inserted are not re-checked.

> Stephen Howe


 
 
 

FK constraints

Post by Mike Joh » Fri, 22 Mar 2002 07:00:15


Look for the WITH CHECK clause on the alter to get SQL server to check the
constraints for existing data - but what will you then do with the
inevitable rows that fail constraints when trying to reapply the check. I
would only consider turning off constraints for MASSIVE loads of data that
has been pre-validated and being loaded via BCP, DTS or BULK INSERT .
Overall I would leave the constraints in place - they are your friends!

--
Mike John

MCSE,MCSD,MCDBA,MCT

KnowledgePool Ltd

Quote:> Using SQL Server 7.0, SP3

> I thought of a way of speeding up these mass inserts that done for the app
> we have built.
> Basically do

> Begin tran
> alter table table1 nocheck constraint FK_table1_table2
> insert into table1 values(...)
> :
> insert into table1 values(...)
> alter table table1 check constraint FK_table1_table2
> commit tran

> The snag is when "alter table table1 check constraint FK_table1_table2" is
> re-applied, all those rows that were inserted are not re-checked.

> Stephen Howe

 
 
 

FK constraints

Post by Stephen How » Fri, 22 Mar 2002 09:39:23



Quote:> Look for the WITH CHECK clause on the alter to get SQL server to check the
> constraints for existing data - but what will you then do with the
> inevitable rows that fail constraints when trying to reapply the check. I
> would only consider turning off constraints for MASSIVE loads of data that
> has been pre-validated and being loaded via BCP, DTS or BULK INSERT .
> Overall I would leave the constraints in place - they are your friends!

I realise this. But in this case, the data being inserted, updated, deleted
has been pre-validated in that numerous SProc's, singleton SELECTS etc have
been done. At that stage where all business rules have been checked, and
pending data to be inserted, updated, deleted is known to be good. Therefore
the FK constraints just hamper here.

Thanks

Stephen Howe

 
 
 

FK constraints

Post by Stephen How » Fri, 22 Mar 2002 09:47:26


Quote:> Stephen:

> You seem to be aware of the trade-offs between referential integrity
> checking and performance.
> Is your question how to not have to make the choice between the two?  That
> is, how can you have it both ways?

I just thought I might be a command which says "apply checking to rows
inserted,updated, deleted since the FK constraint was dropped".
I assume I would gain because the checking would be done all at once.

But in practice since I wrote this message, I realise that the Import apps
written (see other message), do all the checks necessary (they really do)
before changes are made to the database. For example before a row is added
to Table1 a lookup is done in a C++ map container for an entry on Table 2,
if not found, a lookup is done on the database and if the row is really
absent, it is scheluded to be added to table 2 before the corresponding
entry is done on table 1.

 Therefore, I can safely temporarily turn off the FK constraints while
changing the database and reapply them.

thanks

Stephen Howe

 
 
 

1. Cascading Delete with FK-Constraints?

As there is no ON DELETE CASCADE, i tried to solve cascading delete with
triggers. My tables contain foreign key constraints. While executing the
triggers, i get the message, that the foreign key constraint is
violated. I suppose, that the triggers are executed after deleting. Is
there a possible way to have foreign key constraints and cascading
deletes or must i remove the foreign key constraints?

Please answer quickly, because time is money :-)

2. REMOTE_LOGIN_PASSWORDFILE parameter

3. FK constraints between 2 dbs

4. restored a database

5. FK Constraints in DTS

6. Error 567 - Cannot Write Sorted Rows - Help Needed

7. 2 fk constraint in 1 field

8. SQL Problem

9. how to do inserts with circular fk constraints?

10. Recreate FK Constraints using Transact SQL

11. Identity Number Lost on failed Insert w/ FK Constraint in SQL7

12. Cascade delete with FK constraints

13. Triggers / FK Constraints