DELETE CASCADE in mySQL -
DELETE CASCADE in mySQL -
i have next 2 mysql tables 'child' table having delete cascade action set 'parent':
parent table
create table `userdetails` ( `userid` int(6) not null auto_increment, `forename` varchar(20) not null, `surname` varchar(30) not null, `emailaddress` varchar(150) not null, `password` varchar(200) not null, `passwordhint` varchar(20) not null, `subscriptionexpiration` date not null, `salt` varchar(200) not null, primary key (`userid`) ) engine=innodb auto_increment=2 default charset=utf8 auto_increment=2 ;
child table
create table `locations` ( `userid` int(6) not null, `locationid` int(6) not null auto_increment, `locationname` varchar(80) not null, primary key (`locationid`), key `userid` (`userid`) ) engine=innodb default charset=utf8 auto_increment=1 ; -- -- constraints dumped tables -- -- -- constraints table `locations` -- alter table `locations` add together constraint `locations_ibfk_1` foreign key (`userid`) references `userdetails` (`userid`) on delete cascade;
i'm trying run next php script:
<?php require("phpfile.php"); // gets info url parameters $userid = $_post["userid"]; $locationname = $_post["locationname"]; // opens connection mysql server $connection = mysql_connect ("hostname", $username, $password); if (!$connection) { die('not connected : ' . mysql_error()); } // set active mysql database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ('can\'t utilize db : ' . mysql_error()); } // insert new row user info $query = "insert locations (userid, locationname) values ('$userid', '$locationame')"; $result = mysql_query($query); if (!$result) { die('invalid query: ' . mysql_error()); }
and receive error:
invalid query: cannot add together or update kid row: foreign key constraint fails (`dbname/locations`, constraint `locations_ibfk_1` foreign key (`userid`) references `userdetails` (`userid`) on delete cascade)
i'm not sure why i'm getting there record in parent table. i've been working on hours , can't find answer.
i wondered whether perhaps take @ please , allow me know i'm going wrong.
many thanks
follow sequence:
first had insert info userdetails
insert `test`.`userdetails` (`userid`, `forename`, `surname`, `emailaddress`, `password`, `passwordhint`, `subscriptionexpiration`, `salt`) values (null, 'john', 'doo', 'johndoo@john.com', '1234', 'seq.', '2012-02-02', '!@#$%*ertyfghjcvbfgh%*rujk');
detail: if seek insert kid table first returns me message got. first need insert info userdetails. pay attending have foreign key, need userid (your key) insert kid table. right?
now take how had insert info kid table
insert `test`.`locations` (`userid`, `locationid`, `locationname`) values ('2', '25', 'brazil');
1 row(s) inserted.
can see userid (2) ?
then recommend:
when user register new account, create insert location table (child table), 'cause need userid in location table when user come update data. otherwise got error messages.
i tested here , works! allow me know if works you.
mysql
Comments
Post a Comment