MySQL Foreign Key syntax

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4640
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

MySQL Foreign Key syntax

Post 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?
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post 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;
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4640
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Post 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.
Post Reply