Check for violation of referential integrity rules before deleting

Before deleting a row from a table, is there an easy way to check if it violates the referential integrity rule? I would like to do this from an application for creating a C # form (.Net 3.5) using SQL Server 2005.

+3
source share
2 answers

There are several possible options that come to mind:

  • Configure cascading deletes in the database so that deletion always succeeds.
  • Before deleting, check the related records with SELECT. This requires the application to know the limitations.
  • A good domain model (business classes) should allow the application to keep abreast of the relevant records.
  • O/R, NHibernate.
  • SMO (Microsoft.SqlServer.Smo) , . , .
+1

:

try
{
  begin transaction
  delete row
}
catch SQLException
{
  if SQL error indicates referential integrity violation
    throw ReferentialIntegrityViolationException
}
finally
{
  rollback transaction
}

(, , )

+1

Source: https://habr.com/ru/post/1709920/


All Articles