foreign key constraints or / and unique key ?

This is a discussion on foreign key constraints or / and unique key ? within the MySQL Database forums, part of the Database Forums category; I have the following database structure : my_user ( id, name ); my_country (id, name ); my_travel ( id_user , id_country , length_of_stay ); id_user AND id_country are ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-22-2006
Ah Bon
 
Posts: n/a
Default foreign key constraints or / and unique key ?

I have the following database structure :

my_user ( id, name );
my_country (id, name );
my_travel ( id_user , id_country , length_of_stay );

id_user AND id_country are both external keys. They references my_user
and my_country.

In mysql, innoDB engine, is it better to :

1/ add 2 foreign key constraints to my_travel.id_user and
my_travevl.id_country

or

2/ add only one unique key on (id_user, id_country)

The basic fact is : when I add foreign key constraints AND unique key,
mysql tells me that my_travel fields should'nt be part of an unique and
foregn key in the same time.

Any ideas ?
Reply With Quote
  #2 (permalink)  
Old 03-22-2006
Jerry Stuckle
 
Posts: n/a
Default Re: foreign key constraints or / and unique key ?

Ah Bon wrote:
> I have the following database structure :
>
> my_user ( id, name );
> my_country (id, name );
> my_travel ( id_user , id_country , length_of_stay );
>
> id_user AND id_country are both external keys. They references my_user
> and my_country.
>
> In mysql, innoDB engine, is it better to :
>
> 1/ add 2 foreign key constraints to my_travel.id_user and
> my_travevl.id_country
>
> or
>
> 2/ add only one unique key on (id_user, id_country)
>
> The basic fact is : when I add foreign key constraints AND unique key,
> mysql tells me that my_travel fields should'nt be part of an unique and
> foregn key in the same time.
>
> Any ideas ?


I wouldn't think you would want a unique key on id_user and id_country.
For instance - what happens if a user makes multiple trips to the
same country? Wouldn't you want to be able to list all the trips?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 03-22-2006
Martijn Tonies
 
Posts: n/a
Default Re: foreign key constraints or / and unique key ?


> I have the following database structure :
>
> my_user ( id, name );
> my_country (id, name );
> my_travel ( id_user , id_country , length_of_stay );
>
> id_user AND id_country are both external keys. They references my_user
> and my_country.
>
> In mysql, innoDB engine, is it better to :
>
> 1/ add 2 foreign key constraints to my_travel.id_user and
> my_travevl.id_country
>
> or
>
> 2/ add only one unique key on (id_user, id_country)


These are different functional wise.

So there's no "better" case, you have to take what fits your
design and requirements.


> The basic fact is : when I add foreign key constraints AND unique key,
> mysql tells me that my_travel fields should'nt be part of an unique and
> foregn key in the same time.



--
Martijn Tonies
Database Workbench - development tool for MySQL, and more!
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com


Reply With Quote
  #4 (permalink)  
Old 03-22-2006
Ah Bon
 
Posts: n/a
Default Re: foreign key constraints or / and unique key ?

Jerry Stuckle a écrit :
> Ah Bon wrote:
>> I have the following database structure :
>>
>> my_user ( id, name );
>> my_country (id, name );
>> my_travel ( id_user , id_country , length_of_stay );
>>
>> id_user AND id_country are both external keys. They references my_user
>> and my_country.
>>
>> In mysql, innoDB engine, is it better to :
>>
>> 1/ add 2 foreign key constraints to my_travel.id_user and
>> my_travevl.id_country
>>
>> or
>>
>> 2/ add only one unique key on (id_user, id_country)
>>
>> The basic fact is : when I add foreign key constraints AND unique key,
>> mysql tells me that my_travel fields should'nt be part of an unique
>> and foregn key in the same time.
>>
>> Any ideas ?

>
> I wouldn't think you would want a unique key on id_user and id_country.
> For instance - what happens if a user makes multiple trips to the same
> country? Wouldn't you want to be able to list all the trips?
>
>

In this case, I would have added another column :

my_travel ( id_user , id_country , date_departure, length_of_stay );

But your post made me think about the whole problem. I now consider that
Unique is the right key to use in such design, so I'll use it.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:12 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0