Answered Questions

  • What is the main use of FOREIGN KEY?

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: MANAS KUMAR ROUT ( USA ) (TCS) (Eli LILLY ) - PFIZ

    • Jul 6th, 2007


    Hey Ramesh,

    I understand your interviewer's enthusiasm to know about the Foreign Key.? See foreign key definitions says its states the relationship of one attribute(Column) of one table to the corresponding column on the other table.

    So one os a parent and other is child. PARENT is that table which holds the primary key and CHILD is table which holds the referential key. Hence every entry in the PRIMARY table is unique where as same entry might be repeating in the child table. BUT that same has to be present in the PARENT table (Primary KEY).

    Now Going by definition Foreign key can be of two types :
    ========================================
    1.? ON DELETE SET NULL
    2.? ON DELETE SET CASCADE

    DETAILS:
    =======
    On delete set null :
    =======
    When a foreign key is created by on delete set null definition then when you delete one row from the primary?column (of parent table ) , then the corresponding entry in the foreign key table (Child table ) have the value "NULL" for that particular column.


    ON Delete Set Cascade:
    ================
    When the foreign key is created by this definition then when you delete the primary column (any one row -unique data) , then the Child table forcefully deletes all the rows in the child table having same value for that particular column.


    I hope u got the crux of the Foreign key
    Thanks
    Manas Kumar Rout
    SAS Consultant
    Oracle Certified Associate
    Unix Consultant

    shweta dhumal

    • Sep 8th, 2011

    foreign key is primary key of other table. for example: 'STUDENT' is one table which stores information about students and 'SID' is primary key in that table and 'MARKS' is another table which stor...

    shweta dhumal

    • Sep 8th, 2011

    The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted. For example, say we have two tab...