aspose file tools*
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
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to achieve One to One mapping between two tables