What is referential integrity in SQL Server ?

 Posted by Bhakti on 11/15/2009 | Category: Sql Server Interview questions | Views: 14330
Answer:

To avoid logically corruption of data we need to maintain relationships with most of the databases. Building and maintaining logical relationships between tables is essential part to work with relational databases and when such relationships exist, we say that the data has referential integrity. With such database one table is referenced table and the other is the referencing table and values with both the tables (referencing table and referenced table) must match.
We can enforce such referential integrity through foreign key constraints.

For an example,…
CREATE TABLE Table_Referenced 

(
id INT
CONSTRAINT pk_id PRIMARY KEY(id)--Create Primary Key here
);
GO

CREATE TABLE Table_Referencing
(
RID Int
CONSTRAINT fk_RID FOREIGN KEY(RID)REFERENCES Table_Referenced(id)--Creates foreign key names 'RID' which is referring primary key of Table_Referenced.id
);
GO


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response