أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.
Secondary table must have only one record aginst each Primery Key in main table.
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,
have same key as primary key in both tables.
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
using Foreign key , you should add a column in one table that reference the other table
yes you're totally right but i'm asking about how to do that ? the best way to do it in database
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)