English 中文(简体)
如果万国邮联提到“关键”的话,则其行为方式与万国不同。
原标题:MySQL REPLACE behaves differently than UPDATE if the PRIMARY KEY is referenced by a FOREIGN KEY

我有两个表格:

CREATE TABLE `category` (
  `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` mediumint(8) unsigned NOT NULL,
  `name` varchar(20) CHARACTER SET ascii NOT NULL,
  `description` varchar(100) DEFAULT NULL,
  `repeat_interval` tinyint(3) unsigned NOT NULL DEFAULT  0 ,
  `color` mediumint(8) unsigned NOT NULL,
  PRIMARY KEY (`category_id`),
  KEY `id` (`user_id`),
  CONSTRAINT `category_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON  DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `event` (
  `event_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(10) unsigned NOT NULL,
  `name` varchar(20) CHARACTER SET ascii NOT NULL,
  `description` varchar(100) DEFAULT NULL,
  `repeat_interval` tinyint(3) unsigned NOT NULL DEFAULT  0 ,
  `color` mediumint(8) unsigned NOT NULL,
  `priority` tinyint(3) unsigned NOT NULL DEFAULT  0 ,
  `start` timestamp NOT NULL DEFAULT  0000-00-00 00:00:00 ,
  `end` timestamp NOT NULL DEFAULT  0000-00-00 00:00:00 ,
  `done` tinyint(1) NOT NULL DEFAULT  0 ,
  PRIMARY KEY (`event_id`),
  KEY `category_id` (`category_id`),
  CONSTRAINT `event_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category`     (`category_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

If I make a REPLACE in the category table (one row) then all the entries in the event table referencing the modified row in the category table will be discarded.

But if I UPDATE a row in the category table then the entries in the event table are left untouched.

为什么这一行为,为什么在IREPLACE时,所有条目都提到该栏被丢弃?

I tried with both ON UPDATE CASCADE and the default, same behaviour.

谷歌可以帮助我。

最佳回答

页: 1

From Mysql Docs:

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.

http://dev.mysql.com/doc/refman/5.0/en/replace.html“rel=“nofollow noreferer”>。 http://dev.mysql.com/doc/refman/5.0/en/replace.html。

为了解决这个问题,你可能希望使用<代码>。 http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html。

问题回答

替代行为是删除和重新插入。 当你努力更新主要钥匙时,这一点是有益的。

i 假定关键关系,你将删除一个栏目,然后在表格中插入一个字句,但没有在外表上插入。

You probably have ON DELETE CASCADE. REPLACE deletes the record from category table, which then deletes all event records assigned to that category.

我需要操作一次性升级文字,因此我的工作方向是:

  1. Drop the foreign key constraints
  2. Run my "REPLACE INTO" statements
  3. Re-add the foreign key constraints

这将不作为守则中的一种永久解决办法,但它确实是一个快速的黑板。





相关问题
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 ...

热门标签