473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Foreign Keys Don't Work?

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>

Apr 18 '06 #1
5 1688
of course I reference "struct.node_id" -- just a small typo while
renaming the table for this post

Apr 18 '06 #2
clops wrote:
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,

.. . .

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.
Apr 18 '06 #3
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" <bi**@karwin.com> kirjoitti
viestissä:e2********@enews2.newsguy.com...
clops wrote:
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,

. . .

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.

Apr 19 '06 #4
thanks!!

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

regards,

ak

Apr 20 '06 #5
thanks!!

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

regards,

ak

Apr 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
26
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
3
by: Thomas LeBlanc | last post by:
I am able to enter a null in a foreign key field in one table(table2) without the foreign key relationship (table1.t1_id) validating the value. Why is this? Below are 2 table's scripts: ...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
6
by: Victor Spång Arthursson | last post by:
New to foreign keys and have some questions… The first is, i have a language table with a primary key on the fields lang and relid: relid | lang | text -------+------+------------- 11111...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
2
by: kal stevens | last post by:
I have been trying to write a database schema in mysql, and I cant figure this out. Here is a database schema DROP DATABASE IF EXISTS d; CREATE DATABASE d;
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.