ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Amr Alhawary , Technical Lead , ITWORX Education
تاريخ النشر: 2014/07/03
Atif Tanveer
من قبل Atif Tanveer , Decision Support Specialist , Hamilton Health Science

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

Olyad Fekade
من قبل 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
من قبل Mohammad Shalabi , Solution Architect, Android, and IOS Architect , ALM New Way

have same key as primary key in both tables.

Gayasuddin Mohammed
من قبل 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
من قبل omar albluwe , Database Developer , iHorizons

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

Amr Alhawary
من قبل 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
من قبل 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)

المزيد من الأسئلة المماثلة