I have these two CREATE TABLE
statements:
CREATE TABLE GUEST (
id int(15) not null auto_increment PRIMARY KEY,
GuestName char(25) not null
);
CREATE TABLE PAYMENT (
id int(15) not null auto_increment
Foreign Key(id) references GUEST(id),
BillNr int(15) not null
);
What is the problem in the second statement? It did not create a new table.
I will suggest having a unique key for the payment table. On it's side, the foreign key should not be auto_increment as it refer to an already existing key.
There should be space between
int(15)
andnot null
The answer to your question is almost the same as the answer to this one .
You need to specify in the table containing the foreign key the name of the table containing the primary key, and the name of the primary key field (using "references").
This has some code showing how to create foreign keys by themselves, and in CREATE TABLE.
Here's one of the simpler examples from that:
Make sure you're using the InnoDB engine for either the database, or for both tables. From the MySQL Reference: