Re: INSERT INTO with FOREIGN KEY ???



I can't figure out how to manage INSERT INTO in a table having FOREIGN KEY
constraint.

Well if you have FK constraint, you must make sure items exist _FIRST_ on
the FK table before inserting your table.

We have FKs on a SQL Server database.
When inserting data into tables, we have to make sure that they are
populated in the right order so that FKs are not triggered.

The same is true for DELETE but it is now in reverse order.

And if you have a recordset where you are doing INSERTs and DELETEs then you
are in trouble as
You need 2 recordsets, an empty one for INSERTs and a different one for
DELETEs (or use Command/Connection Execute). But basically you want to do

DELETE table
DELETE FK table
'UPDATES here
INSERT FK table
INSERT table

in that order so that FK constraints are alsways satisfied. Triggers are
worth looking at if you want cascaded INSERTs, DELETEs

Stephen Howe


.



Relevant Pages

  • Re: Updating a Table
    ... You know what, I misread your post. ... constraint. ... If you are using table3 threre should be no problem. ... inserting the correct data into the table itself (inserting into the other ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: identity ranges on republished restore of db
    ... You only need to update the constraint IF non replication processes will be ... inserting the rows, i.e. if you or your app want to insert the rows on the ... First off I would reseed your table to DBCC CHECKIDENT ('ApplProduct', ...
    (microsoft.public.sqlserver.replication)
  • Re: Confussing error .. help needed
    ... But apparently you have a unique constraint on the ... License and AccountID columns of your table and for some reason you are ... You will have to figure out why you are inserting a new record with ...
    (microsoft.public.dotnet.languages.vb)