Connecting Tech Pros Worldwide Forums | Help | Site Map

Foreign Keys Don't Work?

clops
Guest
 
Posts: n/a
#1: Apr 18 '06
Dear Collegues,

can anyone explain me WHY the following set of instructions does not
spit any errors (MySQL 5.017) -- the second insert should not have
worked as it references a non-existing record!

mysql> CREATE TABLE struct(
-> node_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> parent_node_id INT UNSIGNED REFERENCES
ep_catalog_structure.node_id ON DELETE CASCADE ON UPDATE CASCADE,
->
-> # Labels
-> name VARCHAR(255) NOT NULL,
->
-> PRIMARY KEY(node_id)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into struct set name = 'Hello World';
Query OK, 1 row affected (0.00 sec)

mysql> insert into struct set name = 'Hello World Child',
parent_node_id = 1;
Query OK, 1 row affected (0.03 sec)

mysql> insert into struct set name = 'Hello World Child',
parent_node_id = 6;
Query OK, 1 row affected (0.00 sec)

mysql> select * from struct;
+---------+----------------+-------------------+
| node_id | parent_node_id | name |
+---------+----------------+-------------------+
| 1 | NULL | Hello World |
| 2 | 1 | Hello World Child |
| 3 | 6 | Hello World Child |
+---------+----------------+-------------------+
3 rows in set (0.00 sec)

mysql>


clops
Guest
 
Posts: n/a
#2: Apr 18 '06

re: Foreign Keys Don't Work?


of course I reference "struct.node_id" -- just a small typo while
renaming the table for this post

Bill Karwin
Guest
 
Posts: n/a
#3: Apr 18 '06

re: Foreign Keys Don't Work?


clops wrote:[color=blue]
> mysql> CREATE TABLE struct(
> -> node_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
> -> parent_node_id INT UNSIGNED REFERENCES
> struct.node_id ON DELETE CASCADE ON UPDATE CASCADE,[/color]
.. . .

Normally the syntax is "REFERENCES tablename(columnname)", not
"tablename.columnname".

See http://dev.mysql.com/doc/refman/5.0/...ate-table.html

I'm not sure how MySQL is interepreting this CREATE TABLE, or why it
didn't give you an error. But it may not be enforcing what you think
it's enforcing.

Regards,
Bill K.
Heikki Tuuri
Guest
 
Posts: n/a
#4: Apr 19 '06

re: Foreign Keys Don't Work?


Hi!

It is this bug:

http://bugs.mysql.com/bug.php?id=13301

MySQL still does not give a warning for syntax that does not work.

Best regards,

Heikki

Oracle Corp./Innobase Oy
InnoDB - transactions, row level locking, and foreign keys for MySQL

InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up MyISAM
tables
http://www.innodb.com/order.php

"Bill Karwin" <bill@karwin.com> kirjoitti
viestissä:e23bov0gv1@enews2.newsguy.com...[color=blue]
> clops wrote:[color=green]
>> mysql> CREATE TABLE struct(
>> -> node_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
>> -> parent_node_id INT UNSIGNED REFERENCES
>> struct.node_id ON DELETE CASCADE ON UPDATE CASCADE,[/color]
> . . .
>
> Normally the syntax is "REFERENCES tablename(columnname)", not
> "tablename.columnname".
>
> See http://dev.mysql.com/doc/refman/5.0/...ate-table.html
>
> I'm not sure how MySQL is interepreting this CREATE TABLE, or why it
> didn't give you an error. But it may not be enforcing what you think it's
> enforcing.
>
> Regards,
> Bill K.[/color]


clops
Guest
 
Posts: n/a
#5: Apr 20 '06

re: Foreign Keys Don't Work?


thanks!!

I got it working with the normal sytax "FOREIGN KEY (col) REFERENCES
blablabla"

regards,

ak

clops
Guest
 
Posts: n/a
#6: Apr 20 '06

re: Foreign Keys Don't Work?


thanks!!

I got it working with the normal sytax "FOREIGN KEY (col) REFERENCES
blablabla"

regards,

ak

Closed Thread