简体   繁体   English

MYSQL如何在基于父表插入数据时用外键更新子表值

[英]MYSQL How to update child table value with foreign key while insert data based on parent table

I have 2 table, a parent table and child table. 我有2个表,一个父表和一个子表。 Child table has foreign key to parent table. 子表具有指向父表的外键。 I have successfully create the foreign key. 我已经成功创建了外键。 But how to auto update field 'nama' based on parent table while I only insert 'id_parent' ? 但是,当我仅插入“ id_parent”时,如何基于父表自动更新字段“ nama”? I only tried : 我只试过:

INSERT INTO child (id_parent) values (1)

but field 'name' value is still NULL 但是字段“名称”的值仍为NULL

Here my tables : 这是我的桌子:

CREATE TABLE `child` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `id_parent` int(6) DEFAULT NULL,
  `nama` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `aa` (`nama`,`id_parent`),
  CONSTRAINT `aa` FOREIGN KEY (`nama`, `id_parent`) REFERENCES `parent` (`nama`, `id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=armscii8;

-- ----------------------------
-- Table structure for parent
-- ----------------------------
CREATE TABLE `parent` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `nama` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `nama` (`nama`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=armscii8;

the parent table has data like this 父表具有这样的数据

INSERT INTO `parent` VALUES ('4', 'asa');
INSERT INTO `parent` VALUES ('1', 'batman');
INSERT INTO `parent` VALUES ('3', 'goku');
INSERT INTO `parent` VALUES ('2', 'robin');
INSERT INTO `parent` VALUES ('5', 'usu');

Check this line: 检查这一行:

KEY `aa` (`nama`,`id_parent`),
  CONSTRAINT `aa` FOREIGN KEY (`nama`, `id_parent`) REFERENCES `parent` (`nama`, `id`)

here you have created a foreign key relationship on two column ie parent ( nama , id ). 在这里,您已经在两列(即parentnamaid ))上创建了foreign key关系。 In that case you can only insert those combination values in child table that are already in parent table. 在这种情况下,您只能在父表中已经存在的子表中插入这些组合值。 Remember, here foreign key is implemented by combining two columns, not on a single column. 请记住,这里的外键是通过组合两列而不是在单个列上来实现的。

In my case I had a Location TransId column in my child table. 就我而言,我的子表中有一个Location TransId列。 I have update the location name from the parent ti child table column. 我已经从父ti子表列中更新了位置名称。 I done with the below 我做了以下

UPDATE Table_A SET Table_A.CityName = Table_B.LocationDesc 更新Table_A SET Table_A.CityName = Table_B.LocationDesc

from

<ParentTable> AS Table_A
INNER JOIN <ChildTable> AS Table_B
    ON Table_A.CityTransID = Table_B.TransID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将父表中的外键值插入子表中 - insert foreign key value from parent table into child table 如何使用插入语句在父表的子表中添加或插入外键值 - How to add or insert foreign key value in child table from parent table using insert statement 如何更新或插入具有来自父表的外键约束的子表 - How to update or insert a child table which having foreign key constrains from parent table 如何根据外键值有条件地将数据插入表中? - How to insert data into table conditionally based on foreign key value? MySQL中如何将主表的Primary Key值插入到子表的Foreign Key列? - How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL? 如何将父表的当前主键的值插入子表 - how to insert the value of current primary key of parent table into child table Mysql的。 如何将带有外键的列的空值插入子表中的行 - Mysql. How to insert row to child table with null value of column that is foreign key 如何更新引用MySQL中父表的外键字段的值? - How to update values of foreign key fields referencing to a parent table in MySQL? 如何将父表中的数据更新或插入子表? - How to update or insert data from the parent table to the child table? 如何更新父表的主键,它将在冬眠时自动更新子表的外键? - How to update primary key of parent table that will automatically update child table foreign key in hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM