Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to achieve One to One mapping between two tables

 
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 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 .
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Although carName isn't the ideal key because it could change.
 
cow clarke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic