File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to create a tables having  One to One relation ship to each other Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to create a tables having  One to One relation ship to each other " Watch "How to create a tables having  One to One relation ship to each other " New topic
Author

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

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thats helpful .Thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
That's not one-to-one. That's one (company) to many (employees).
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You beat me to it, Scott. Probably better than I would have put it
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you scott very much .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a tables having One to One relation ship to each other