May 29, 2014

CASCADE in SQL Server (Foreign & Primary Key Constraints)

We have several tables in an SQL database referencing each other with foreign key constraints. If we need to delete a row in one of these tables as well as all other rows that reference it, then we have two options. The first option is to execute an explicit DELETE statement for each table that contains referencing rows and then finish by deleting the referenced row (this order is important if we don’t want to violate any foreign key constraints). The other option is to declare our foreign keys as ON DELETE CASCADE. This clause tells the database to automatically delete the referencing row if the row it references is deleted.

Select * from dbo.ProductDetails
Select * from dbo.Products

Suspect Query for MS SQL 2005

How To Repair A Suspect Database In MSSQL 2005:

Issue
You have a database in MSSQL that is tagged as (suspect) and you are unable to connect to the database.

Possible Causes
The database could have become corrupted.
There is not enough space available for the SQL Server to recover the database during startup.
The database cannot be opened due to inaccessible files or insufficient memory or disk space.
The database files are being held by operating system, third party backup software etc.
There was an unexpected SQL Server Shutdown, power failure or a hardware failure.