• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to create a tables having One to One relation ship to each other

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am using Oracle as Database .


when googling for One to One relationship in Database tables .I found an example for it .

“An Employee can have one and only one Company.” .


I have created these two tables Employee and Company

Create table Employee(Name varchar(20))

Create table Company(Name varchar(20))


Can anybody please tell me how can i keep a one to one relation among these two tables .

Please help
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to either define your foreign key columns on both sides of the relationship if it is bidirectional. If the relationship is not bidirectional you will need to add a key column to the child table only and add a unique constraint to that column.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats helpful .Thank you
 
Marshal
Posts: 73019
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not one-to-one. That's one (company) to many (employees).
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That's not one-to-one. That's one (company) to many (employees).



From other side it will be one to one and one to many as you said

one to many (Cpmpany----- Employee)

One to One (Employee -----Company)


correct me if i am wrong
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are mistaken. A one-to-one relationship implies a situation where A --> B and B --> A. Such an example might include social security numbers and e-mail addresses since having one tends to give you the other. In general, one-to-one relationships across tables aren't that common in a database.

What's more common is what your describing, which is one-to-many relationship. An employee has one company and the company has many employees. You should refrain from referring to this as a one-to-one relationship in any way. The employee may determine the company (as in your example) but the company does not determine the employee, therefore its not one-to-one.
 
Campbell Ritchie
Marshal
Posts: 73019
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You beat me to it, Scott. Probably better than I would have put it
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you scott very much .
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic