Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the best way to implement one-one relationship ?

user-image
Question added by Amr Alhawary , Technical Lead , ITWORX Education
Date Posted: 2014/07/03
Atif Tanveer
by Atif Tanveer , Decision Support Specialist , Hamilton Health Science

Secondary table must have only one record aginst each Primery Key in main table.

Olyad Fekade
by Olyad Fekade , Oracle Database Administrator , Dashen Bank

Dear Amr Alhawary, 

If you want a one to one (1-1) relationship then the Foreign key should itself be a Primary Key or unuiqe index in the child table that guarantees that there may be at most one row in the child table with that value.

 

Implementation

 

Create Table ParentTable

    (

    PrimaryKeyCol ... not null Primary Key

    , ...

    )

 

Create Table ChildTable

    (

    , ForeignKeyCol ... [not] null [Primary Key, Unique]

    , ...

    , Constraint FK_ChildTable_ParentTable

        Foreign Key ( ForeignKeyCol )

        References ParentTable( PrimaryKeyCol )

    )

 

I think this will help

 

with regards,

Mohammad Shalabi
by Mohammad Shalabi , Solution Architect, Android, and IOS Architect , ALM New Way

have same key as primary key in both tables.

Gayasuddin Mohammed
by Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

where is the requirement of creating two master-child tables for one-one relationship in rdbms?, can we simply not create one table having primary key will suffice, right? and further purpose of shortening the tables, we can creates views, right? i think this may be the best solution can think of instead of having two tables.

 correct me please if i'm wrong or understood the question wrongly. thanks

omar albluwe
by omar albluwe , Database Developer , iHorizons

 using Foreign key , you should add a column in one table that reference the other table 

Amr Alhawary
by Amr Alhawary , Technical Lead , ITWORX Education

yes you're totally right but i'm asking about how to do that ? the best way to do it in database

Ibrahim Haymour
by Ibrahim Haymour , Technical Support Dept. Director , Computer Applications & Technical Serveices (CATS)

if you have a tableA with as Primary Key "PKA" and the same key exists in tableB as a primary key "PKB" a one to one relation can be established between the two tables and selecting all feilds from both using the follwing query

select a.*,b.* from tableA a,tableB b where a.PKA = a.PKB

or you can inner join like this

select a.*,b.* from tableA as a inner join tableB as b on a.PKA=b.PKB

please note that both the keys must be primary in both tables or at least the field of these keys must have a unique index (constraint)

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.