PDA

View Full Version : Validating foreign keys in MySQL


amars1983
10-16-2009, 11:09 PM
I'm a mysql noob, trying to port a script from oracle. I'm validating foreign keys like so in oracle:


SELECT m.member_id, c.member_id
FROM member m, contact c
WHERE m.member_id = c.member_id;


To port to mysql do I just add a JOIN in my from statement like so


SELECT m.member_id, c.member_id
FROM member m JOIN contact c
WHERE m.member_id = c.member_id;


or, do I have to include an ON statement with the foreign key constrain names. Any help would be greatly appreciated. Thanks in advance

Old Pedant
10-16-2009, 11:59 PM
First of all, there was no need to change the Oracle query.

It would work as is in MySQL.

Secondly, you can't mix up an EXPLICIT JOIN with an IMPLICIT JOIN.

These two queries are 100% IDENTICAL IN SEMANTICS (even though the syntax is different:

-- implicit join
SELECT m.member_id, c.member_id
FROM member m, contact c
WHERE m.member_id = c.member_id;

versus

-- explicit join
SELECT m.member_id, c.member_id
FROM member m INNER JOIN contact c
ON m.member_id = c.member_id;


The names "explicit" and "implicit" joins are my own terms. "New style" and "old style" are also accurate.

Anyway, ANSI SQL supports both, and MySQL supports both.

Now, if you were coding an OUTER JOIN, then MySQL properly supports only the "new style" explicit join.