# SQL query: cannot add or update a child row

**URL:** <https://forum.freecodecamp.org/t/sql-query-cannot-add-or-update-a-child-row/274789>\
**Category:** General\
**Created:** [May 1, 2019, 5:44pm UTC](https://forum.freecodecamp.org/t/sql-query-cannot-add-or-update-a-child-row/274789 "2019-05-01T17:44:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vyskoc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vyskoc/32/104656_2.png) [@Vyskoc](https://forum.freecodecamp.org/u/Vyskoc)\
**Post date:** [May 1, 2019, 5:44pm UTC](https://forum.freecodecamp.org/t/sql-query-cannot-add-or-update-a-child-row/274789/1 "2019-05-01T17:44:58Z")

</div>

Hi, I am creating a complex database using PopSQL. Here I have created a table ‘employee’, and another table ‘branch’. The table "employee’ contains a foreign key ‘branch\_id’ which is the primary key of the "branch’ table.So I tried to update the branch\_id of the first record in “employee” after inserting the required values into “branch”, which is `1` in this case

```auto
INSERT INTO employee VALUES(100, "David", "Wallace", "1967-11-17", "M", 250000, NULL, NULL);
INSERT INTO branch VALUES(1, "Corporate", 100, "2006-02-09");
INSERT INTO employee VALUES(101, "Jan", "Lavinson", "1961-05-11", "F", 110000, 100, NULL);

UPDATE employee
SET branch_id = 1
WHERE emp_id = 100;

```

But it shows that I have had a constraint error. Then I also failed to add another record that has the same branch\_id value, ( if the branch\_id is set to null, the record can be entred)  
 ![failure](https://global.discourse-cdn.com/freecodecamp/original/3X/7/e/7e5a1377b384d9b834fc88ed61470023aafcd605.png)  
So I re-added the foreign key of “branch\_id”,

```auto

ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;

```

This query has been accepted, but I still get the same error message. Can someone tell me why, thanks a lot!

---

<div class="post-metadata">

**Author:** ![Swayze](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/swayze/32/152274_2.png) [@Swayze](https://forum.freecodecamp.org/u/Swayze)\
**Post date:** [June 3, 2020, 9:25pm UTC](https://forum.freecodecamp.org/t/sql-query-cannot-add-or-update-a-child-row/274789/2 "2020-06-03T21:25:47Z")

</div>

Wow, what a dilemma. I too have that problem with that same group of fields and values. I’m in a tutorial by Mike Dane about learning MySQL via PopSQL and I assume that you (Vyskoc) were also. I’ll be waiting with you for any suggestions.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:27am UTC](https://forum.freecodecamp.org/t/sql-query-cannot-add-or-update-a-child-row/274789/3 "2021-01-16T11:27:25Z")

</div>

Closed on January 16, 2021, 11:27am UTC


