If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Is there any difference between these two foreign key query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-05-10, 17:36
cpthk cpthk is offline
Registered User
 
Join Date: Aug 2010
Posts: 7
Is there any difference between these two foreign key query?

Is there any difference between these two foreign key query?

Code:
CONSTRAINT test FOREIGN KEY (abc, xyz)
      REFERENCES test2 (abc, xyz),
vs.
Code:
CONSTRAINT test1 FOREIGN KEY abc
      REFERENCES test2 abc,
CONSTRAINT test2 FOREIGN KEY xyz
      REFERENCES test2 xyz
Reply With Quote
  #2 (permalink)  
Old 08-05-10, 17:37
cpthk cpthk is offline
Registered User
 
Join Date: Aug 2010
Posts: 7
Is there any difference between these two foreign key query?

Is there any difference between these two foreign key query?

Code:
CONSTRAINT test FOREIGN KEY (abc, xyz)
      REFERENCES test2 (abc, xyz),
vs.
Code:
CONSTRAINT test1 FOREIGN KEY abc
      REFERENCES test2 abc,
CONSTRAINT test2 FOREIGN KEY xyz
      REFERENCES test2 xyz
Reply With Quote
  #3 (permalink)  
Old 08-05-10, 19:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by cpthk View Post
Is there any difference between these two foreign key query?
yes, sir, absolutely there is a big difference

please do a SHOW CREATE TABLE for test2 and i'll explain why

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 08-05-10, 19:31
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
If the primary key of test2 is defined as (abc, xyz) then you cannot create foreign keys that point to only one of the columns.

So the second statement will not work.
Reply With Quote
  #5 (permalink)  
Old 08-05-10, 20:58
cpthk cpthk is offline
Registered User
 
Join Date: Aug 2010
Posts: 7
sorry, I do not have that in my actual database. I am just asking because I saw other's query.
Reply With Quote
  #6 (permalink)  
Old 08-05-10, 21:01
cpthk cpthk is offline
Registered User
 
Join Date: Aug 2010
Posts: 7
so you mean the first code will check if that test2 table has a row that has both abc and xyz as values?
and second code will check separately that test2 table a row that has abc as value, and test2 table has a row that xyz as values.

In short, first code, both values has to exist in the same row, second code can be separated?
Reply With Quote
  #7 (permalink)  
Old 08-05-10, 22:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by cpthk View Post
sorry, I do not have that in my actual database. I am just asking because I saw other's query.
okay, i understand

after you actually work with foreign keys for a while, you will understand why those two are different

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 08-05-10, 22:39
cpthk cpthk is offline
Registered User
 
Join Date: Aug 2010
Posts: 7
the first code will check if that test2 table has a row that has both abc and xyz as values?
and second code will check separately that test2 table a row that has abc as value, and test2 table has a row that xyz as values.

In short, first code, both values has to exist in the same row, second code can be separated? Is this correct?
Reply With Quote
  #9 (permalink)  
Old 08-05-10, 22:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by cpthk View Post
so you mean the first code will check if that test2 table has a row that has both abc and xyz as values?
and second code will check separately that test2 table a row that has abc as value, and test2 table has a row that xyz as values.

In short, first code, both values has to exist in the same row, second code can be separated?
that is correct

i find it very useful to think of the implications about test2 that the FKs give us, as opposed to the process-oriented checking of test1 values existing in test2

every FK must reference a unique key (wherekey is not an index, but rather a defining characteristic, which is uniqueness)

a table might have several candidate keys, and one of them is (usually) selected to be the primary key

so now let's review the last example ...

CONSTRAINT test1 FOREIGN KEY abc REFERENCES test2 abc,
CONSTRAINT test2 FOREIGN KEY xyz REFERENCES test2 xyz

we know that abc will be unique in test2, so let's say that there are N values

but we also know that xyz will be unique in test2, so therefore there will also be N values of xyz

conclusion: the design of test1 implied by these two FKs simultaneously (note the comma!) is that in order to insert a row into test1, you have to supply both key values simultaneously, when in fact giving only one of them is all that is required to determine which row of test2 the row being inserted in test1 belongs to (where you can replace "belongs to" with appropriate terminology for your particular application)

whether it is desirable for an application to have specify redundant key values is a separate question

in the first example ...

CONSTRAINT test FOREIGN KEY (abc, xyz) REFERENCES test2 (abc, xyz)

if we assumpe this is a different test2 table from the above, then here we see that test2 might have N*M rows

however, if this is the same test2 table as above, we already know that there are only N rows in test2, and of course, by deduction, every combination of abc and xyz must be unique, so this FK is redundant

holler if you didn't understand any of this

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 08-05-10 at 22:48.
Reply With Quote
  #10 (permalink)  
Old 08-06-10, 03:51
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Threads merged.
One question, one thread please.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On