Pages

Sunday, November 6, 2011

This is one the typical
SQL interview
questions and also the favorable question of the interviewers, which
has been asked in most of the .NET interviews.

Referential Integrity: - Referential Integrity is a DataBase
concept that ensures the relationship between tables remainsconsistent, where one table has a foreign
key reference to the other table which is declared as
primary key.

In simply words when a relation is maintained between two table’s
using primary key and foreign keyreference is called as
Referential Integrity.

Let’s see a simple demonstration to understand the concept of Referential
Integrity.

In order see it practically you just need to follow the following steps.

In the above table of CustomerDetails you can see that I have declared
CustID as foreign key references to the Customer(CustId)
table.

Step2: - Now, let’s Insert some data to both the table’s.

Query: - Inserting into Customer Table.

insert into dbo.Customer(CustID,CustomerName)values(1,'Kalim')
insert into dbo.Customer(CustID,CustomerName)values(2,'Wasim')
insert into dbo.Customer(CustID,CustomerName)values(3,'Salim')
insert into dbo.Customer(CustID,CustomerName)values(4,'Nadeem')

Note: - When there is Referential Integrity between two table’s then
you cannot delete record from the respective table.

Step3: - Let’s see a example to prove the above mentioned note.

So, let’s try to delete record from the Customer table and see
what is the output.

Query: -

delete from Customer where CustID = 1

As soon as you click on execute you will output result like
below diagram.

In the above output result diagram you can clearly see that the compiler
does not allow deleting record from the table. Which means that,
when there is relation maintains between two table’s using Referential
Integrity you cannot delete records from the respective tables.

See the following video on the differences between unique key and primary key as
follows: -

Subscription Package for fundamentals and Interview preparation

which consist of .NET, C#, ASP.NET, SQL Server, WCF, Design Patterns, SilverLight, LINQ, SharePoint, Azure videos read details of subscription Also want to inquire more then call us on +91-22-66752917/mail us on questpond@questpond.com. Simply click to .