Hi there,

here a simple sample (basicall taken from the documents) with 3 tables.

2 points to watch out for:

1) first declare the primary keys 
        here (p1.id and p2.id) 
2) index the foreign key column 
        here (child.id1 and child.id2)


mysql> CREATE TABLE p1(id INT PRIMARY KEY)TYPE=INNODB;
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE p2(id INT PRIMARY KEY)TYPE=INNODB;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE child(
    -> id1 INT,
    -> id2 INT,
    -> INDEX(id1),
    -> INDEX(id2),
    -> FOREIGN KEY (id1) REFERENCES p1(id),
    -> FOREIGN KEY (id2) REFERENCES p2(id))TYPE=InnoDB;
Query OK, 0 rows affected (0.01 sec)


In you case you haven't indexed IDTr

        Create table y (
         IDTr INT NOT NULL,
INDEX (IDTr)
        constraint FOREIGN KEY  IDTr REFERENCES x(ID)
         )Type=Innodb;


That should work. Best regards

Nils Valentin
Tokyo/Japan

2003年 8月 12日 火曜日 04:31、b b さんは書きました:
>  I am using MYSQL 4. I understand that it allows for
> foreign keys. Could someone show me an example of how
> to declare a foriegn key. I tried a combination of
> statements but I always got a syntax error.
>
>  Here is what I am trying for example ...
>
>  Create table x (
>  ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
>  whatever int
>
> )Type=Innodb;
>
>
>  Create table y (
>  IDTr INT NOT NULL,
>  constraint FOREIGN KEY  IDTr REFERENCES x(ID)
> )Type=Innodb;
>
>  How would I create a foreign key linking IDTr to
> x(ID)?
>
> Cheers.
>
>  Cheers.
>
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site design software
> http://sitebuilder.yahoo.com

-- 
---
Valentin Nils
Internet Technology

 E-Mail: [EMAIL PROTECTED]
 URL: http://www.knowd.co.jp
 Personal URL: http://www.knowd.co.jp/staff/nils


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to