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
CONSTRAINT pk_id PRIMARY KEY(id)--Create Primary Key here
CREATE TABLE Table_Referencing
CONSTRAINT fk_RID FOREIGN KEY(RID)REFERENCES Table_Referenced(id)--Creates foreign key names 'RID' which is referring primary key of Table_Referenced.id
Asked In: Many Interviews |