wood burning stoves 2.0*
The moose likes JDBC and the fly likes How to achieve One to One mapping between two tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to achieve One to One mapping between two tables " Watch "How to achieve One to One mapping between two tables " New topic
Author

How to achieve One to One mapping between two tables

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi

I have taken two tables Car and Registration I want to have One to One mapping between these two tables such that each car can have only One Registration .


so i had made Unique On the fORIGN KEY . Is this way correct to achive One to One on the mapping .

create table Car (
CarName Varchar2(20) primary key,
CarModel Varchar2(20)
)


create table Registration (
RegNumber Varchar2(20) primary key,
CarName Varchar2(20),
FOREIGN KEY (CarName)
REFERENCES Car(CarName)
UNIQUE(car_name))


)

Please help .


Save India From Corruption - Anna Hazare.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Yes. Although carName isn't the ideal key because it could change.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
cow clarke
Greenhorn

Joined: Nov 12, 2009
Posts: 15
I would believe that it depends on the database system product you are using, which is not job of java program.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
As a compliment to Jeanne's post. 'carName' doesn't even sound unique. If you just came up with this to show your idea, then its okay arguably. Otherwise, introducing a surrogate key, if there are no candidates found, is a good idea. Folks prefer surrogate keys over natural, but again it depends upon case to case. Surrogate key is the only solution, in case you have no natural candidates.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to achieve One to One mapping between two tables
 
Similar Threads
Hbm files generated for One to One Mapping
Hibernate One to One Example
Hibenate Mapping
problem with on to one mapping in hibernate
how to related two tables