Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thats helpful .Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you scott very much .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic