English 中文(简体)
我sql中的外国关键参考资料问题
原标题:issue with foreign key reference in mysql

我有一个称为邮递的表格,有一个外国关键栏被删除。 用户用pk表列出参考资料。 当我创建新的邮件时,我想把这个领域划为无效,因为邮件被删除,然而,当我确实遇到外国关键制约因素失败错误时。

我完全理解这一错误,但我如何绕过这一错误?

表:-

CREATE TABLE `mail` (
  `pk` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `from` int(11) unsigned NOT NULL,
  `to` int(11) unsigned NOT NULL,
  `subject` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `dt` datetime NOT NULL,
  `read` enum( 0 , 1 ) NOT NULL DEFAULT  0 ,
  `deletedby` int(11) unsigned DEFAULT  0 ,
  PRIMARY KEY (`pk`),
  KEY `from_mail__users_pk` (`from`),
  KEY `to_mail__users_pk` (`to`),
  KEY `deletedby_mail__users_pk` (`deletedby`),
  CONSTRAINT `deletedby_mail__users_pk` FOREIGN KEY (`deletedby`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `from_mail__users_pk` FOREIGN KEY (`from`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `to_mail__users_pk` FOREIGN KEY (`to`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

错误信息:

 ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`ajfit`.`mail`, CONSTRAINT `deletedby_mail__users_pk` FOREIGN KEY (`deletedby`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
 INSERT INTO `ajfit`.`mail` (`from`, `to`, `subject`, `message`, `read`) VALUES (31, 30,  test ,  test ,  0 )

成就

纽约总部

最佳回答

由于您没有为<代码>删除<>/代码>提供价值,因此使用的<编码>DEFAULT/code>值0。 但<编码>用户没有(推定)记录。 以0的<代码>pk为表格,因此外国关键制约因素(正确无误)失败。

如你说过的那样,你希望<代码>删除<>><>条/代码>有<条码>DEFAULT 值NUL,以便在提供替代价值之前,不再对外国关键制约因素进行检查:

ALTER TABLE mail MODIFY deletedby int(11) unsigned DEFAULT NULL;
问题回答

用户上自栏的数值。 页: 1

由于外国关键制约因素接受国家扫盲十年的数值,一种办法是将国家扫盲十年的数值逐栏删除,同时在<条码>上插入新行。

用户表中是否附有0码的条目? 因为我想到的是,桌子试图找不到零头。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签