Page 1 of 1

MySQL Foreign Key syntax

Posted: Tue Sep 21, 2004 1:21 pm
by Sergeant Thorne
I'm making a table and I want this column to be a Foreign Key:

Code: Select all

...,
column_name TINYINT UNSIGNED NOT NULL,
...
I haven't been able to get it to create the table (it gives me a "syntax" error at this line). I've already cosulted the MySQL readme and a book called "Managing and Using MySQL", but I can't seem to figure it out. I'm just using the MySQL console. Could someone be so kind as to tell me how it needs to be specified?

Posted: Tue Sep 21, 2004 2:54 pm
by DCrazy
Did you try searching the MySQL documentation first?

Here's the example from that page:

Code: Select all

CREATE TABLE parent(id INT NOT NULL,
                    PRIMARY KEY (id)
) TYPE=INNODB;
CREATE TABLE child(id INT, parent_id INT,
                   INDEX par_ind (parent_id),
                   FOREIGN KEY (parent_id) REFERENCES parent(id)
                     ON DELETE CASCADE
) TYPE=INNODB;

Posted: Wed Sep 22, 2004 8:49 am
by Sergeant Thorne
Thanks, DCrazy. I guess I'm going to have to learn more about Keys before I can really make use of it. For now I'm using a different method.