Multiple Foreign Keys -
07-08-2003
, 10:59 AM
Hi,
How are foreign keys defined in SQL? From my notes and text there are
two ways, one of which can be used when you have multiple foreign keys,
the other cannot. It's looks like the following is supposed to be the
right one, but this doesn't work in MySQL*. I suppose it could be a
vendor thing... but I'm not so sure having read over the examples and
notes in the manual (all these different implementations and their
friggin' variations on SQL! )
Code:
CREATE TABLE Likes
(
drinker VARCHAR(20),
beer VARCHAR(20),
PRIMARY KEY (drinker,beer),
FOREIGN KEY drinker
REFERENCES Drinkers(name)
);
I tried extending it with
Code:
CREATE TABLE Likes
(
drinker VARCHAR(20),
beer VARCHAR(20),
PRIMARY KEY (drinker,beer),
FOREIGN KEY drinker, beer
REFERENCES Drinkers(name), Beers(name)
);
and ...
Code:
CREATE TABLE Likes
(
drinker VARCHAR(20),
beer VARCHAR(20),
PRIMARY KEY (drinker,beer),
FOREIGN KEY drinker
REFERENCES Drinkers(name),
FOREIGN KEY Beers
REFERENCES Beer(name)
);
but MySQL doesn't want to play. What's the correct syntax or one of the
above fine and it is a MySQL peculiarity? My text doesn't show an
example of multiple foreign keys at all.
Cheers,
Danx
*I'm ignoring the fact MySQL doesn't pay any attention to foreign keys
in general, it's not important. The purpose is to learn SQL. |