GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-May-2003, 16:31
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Angry

InnoDB FOREIGN KEY error!


I was trying my hand at InnoDB tables and am having problems with FOREIGN KEYS!
PHP Code:

<?php
$sqls[] = 'CREATE TABLE templatesets (
  tplset_code VARCHAR(4) NOT NULL PRIMARY KEY,
  tplset_name VARCHAR(30) NOT NULL
) TYPE=InnoDB';

$sqls[] = 'CREATE TABLE templatex (
  tpl_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tpl_name VARCHAR(100) NOT NULL,
  tpl_html TEXT NOT NULL,
  set_code VARCHAR(4) NOT NULL,
  KEY tpl_name( tpl_name ),
  KEY set_code( set_code ),
  FOREIGN KEY( set_code ) REFERENCES templatesets( tplset_code ) ON DELETE CASCADE
) TYPE=InnoDB';
?>


I am getting the error no. 1005 i.e. errno 150, that means a foreign key definition would be incorrectly formed.

What could be wrong?
  #2  
Old 16-May-2003, 17:18
Allowee's Avatar
Allowee Allowee is offline
Regular Member
 
Join Date: May 2003
Location: The Netherlands
Posts: 339
Allowee has a spectacular aura about
you forgot to set something in the table 'templatesets'

quote from MySQL.com
Quote:
Both tables have to be InnoDB type and there must be an index where the foreign key and the referenced key are listed as the FIRST columns. InnoDB does not auto-create indexes on foreign keys or referenced keys: you have to create them explicitly.
__________________
Pastebin
PHP Documentation Site
Allowee's Blog http://allowee.net
  #3  
Old 16-May-2003, 17:29
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello Allowee,

Thank you for the reply but I don't think I got what you were trying to explain.

A sample correction would be nice
  #4  
Old 17-May-2003, 04:01
Allowee's Avatar
Allowee Allowee is offline
Regular Member
 
Join Date: May 2003
Location: The Netherlands
Posts: 339
Allowee has a spectacular aura about

Re: InnoDB FOREIGN KEY error!


PHP Code:

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 SET NULL
) TYPE=INNODB; 


I got this from MySQL.com

so your code will be something like.

PHP Code:

<?php
$sqls[] = 'CREATE TABLE templatesets (
  tplset_code VARCHAR(4) NOT NULL PRIMARY KEY,
  tplset_name VARCHAR(30) NOT NULL
) TYPE=InnoDB';

$sqls[] = 'CREATE TABLE templatex (
  tpl_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tpl_name VARCHAR(100) NOT NULL,
  tpl_html TEXT NOT NULL,
  set_code VARCHAR(4) NOT NULL,
  KEY tpl_name( tpl_name ),
  KEY set_code( set_code ),
  [b]INDEX tpls_code ( tplset_code ),[/b]
  FOREIGN KEY( set_code ) REFERENCES templatesets( tplset_code ) ON DELETE CASCADE
) TYPE=InnoDB';
?>

I'm not sure if this will work.....

at least you forgot an INDEX.
thats why i put in the quote from MySQL.com
__________________
Pastebin
PHP Documentation Site
Allowee's Blog http://allowee.net
  #5  
Old 17-May-2003, 05:45
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

What is the difference between KEY and INDEX in MySQL?


KEY is a synonym for INDEX.

Yeah, I didn't know that till about a month or so ago, so it's common to be NOT aware of this little detail.

Here's some odd news:

Today, just before getting online, I started my MySQL server and absent-mindedly brought this very page (containing the sql commands above) up and it WORKED!

SO, either my MySQL server last night was screwy or there is a bug with phpmyadmin (since my first attempt trying to create the tables, was through phpmyadmin). I remember I was RELOADING MySQL all the time last night, after each change - yet nothing!

The one thing I didn't do was to STOP and START MySQL. Perhaps that would have solved the problem immediately... I will try to see if I can re-create the problem so I can know for sure what caused it.
 
 

Recent GIDBlogReview: Gel laptop cooling pad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in ukrspp21 MySQL / PHP Forum 33 02-Sep-2009 20:42
Implicit Declaration error mike3340 C++ Forum 1 16-Dec-2003 09:12
error during program rjd72285 C++ Forum 0 11-Nov-2003 19:49
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 21:10
CD burner, focus or tracking error ShingoDrrazz Computer Hardware Forum 1 09-Aug-2003 17:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:41.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.