MySQL Forums
Forum List  »  MySQL Query Browser

Foreign key
Posted by: sarmad shaf
Date: August 19, 2005 02:47PM

bro i have problem in mysql plz help me i want to create a foreign key in one table which is primary key in other table i use InnoDB for that but still some time the query passed but constraint not apply and some thing i got error or query here what i am doing please not i already set up InnoDb

creating first table

CREATE TABLE head (
head_code int(11) NOT NULL default '0',
head_name varchar(30) default NULL,
PRIMARY KEY (head_code),
UNIQUE KEY head2 (head_code),
UNIQUE KEY head3 (head_name),
KEY head1 (head_code)
) TYPE=InnoDB;

creating 2nd table
CREATE TABLE sub (
sub_code int(11) NOT NULL default '0',
sub_name varchar(30) default NULL,
head_code int(11) NOT NULL default '0',
PRIMARY KEY (sub_code),
UNIQUE KEY sub3 (sub_name),
UNIQUE KEY sub2 (sub_code),
KEY sub1 (head_code),
KEY sub11 (sub_code),
FOREIGN KEY (`head_code`) REFERENCES `agritec1.head` (`head_code`)
) TYPE=InnoDB;


here the primary key column of HEAD table "head_code" should be the foreign key in 2nd table SUB same as "head_Code"

if i pass value in sub like (1,"as"2) it accept where as in head the "2" value in head_Code column not present .
but still accepted.so no constraint apply
This is the error i received

ERROR 1005: Can't create table '.\ag\sub.frm' (errno: 150)


pzl help meee

Options: ReplyQuote


Subject
Written By
Posted
Foreign key
August 19, 2005 02:47PM
September 22, 2005 03:56AM
October 20, 2005 07:08AM
September 23, 2005 11:07AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.