diff --git a/CHANGELOG.md b/CHANGELOG.md
index e1e47cc3fb77bba21850528c07f079176edbf77b..7d3df1f72e4acfdaf4f5232374f7d4f2955e90c8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,5 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 * POV with the 'name' property, e.g. `FIND ENTITY WITH name = something`
   [caosdb-server#51](https://gitlab.com/caosdb/caosdb-server/-/issues/51)
 - Fixed several bugs when an Entity inherits from itself (#18, caosdb-server #85).
+- Bug in `updateEntity.sql` (when updating the primary name without a prior call
+  to `deleteEntityProperties`). Same thing for `deleteEntity`.
 
 ### Security ###
diff --git a/procedures/deleteEntity.sql b/procedures/deleteEntity.sql
index 0a70f2056b1f284d7c325d2ffe2d380ee92f4f6f..b96289bc58b77afea9e5ca927760de905eddbc0e 100644
--- a/procedures/deleteEntity.sql
+++ b/procedures/deleteEntity.sql
@@ -41,12 +41,18 @@ delimiter //
 CREATE PROCEDURE db_2_0.deleteEntity(in EntityID INT UNSIGNED)
 BEGIN
 
+    -- detele file properties
+    DELETE FROM files where file_id=EntityID;
 
-	DELETE FROM files where file_id=EntityID;
-	DELETE FROM data_type WHERE domain_id=0 and (entity_id=0 and property_id=EntityID) or entity_id=EntityID; 
-	DELETE FROM collection_type WHERE domain_id=0 and (entity_id=0 and property_id=EntityID) or entity_id=EntityID; 
-	DELETE FROM entities where id=EntityID;
-	DELETE FROM entity_acl WHERE NOT EXISTS (SELECT 1 FROM entities WHERE entities.acl = entity_acl.id LIMIT 1);
+    -- delete datatype stuff
+    DELETE FROM data_type WHERE domain_id=0 and (entity_id=0 and property_id=EntityID) or entity_id=EntityID; 
+    DELETE FROM collection_type WHERE domain_id=0 and (entity_id=0 and property_id=EntityID) or entity_id=EntityID; 
+
+    -- delete primary name (in case this is called without a prior call to deleteEntityProperties)
+    DELETE FROM name_data WHERE domain_id = 0 AND entity_id = EntityID AND property_id = 20;
+
+    DELETE FROM entities where id=EntityID;
+    DELETE FROM entity_acl WHERE NOT EXISTS (SELECT 1 FROM entities WHERE entities.acl = entity_acl.id LIMIT 1);
 
 END;
 //
diff --git a/procedures/updateEntity.sql b/procedures/updateEntity.sql
index cdcacffbe0964518d7caf285d1efb0c1770afb20..76c4bd25c8268896c789fbf37d847098f96476ec 100644
--- a/procedures/updateEntity.sql
+++ b/procedures/updateEntity.sql
@@ -36,10 +36,18 @@ BEGIN
     DECLARE ACLID INT UNSIGNED DEFAULT NULL;
     call entityACL(ACLID, ACL);
 
-    UPDATE entities e SET e.description = EntityDescription, e.role=EntityRole, e.acl = ACLID where e.id = EntityID;
+    UPDATE entities e
+        SET e.description = EntityDescription, e.role=EntityRole, e.acl = ACLID
+        WHERE e.id = EntityID;
 
+    -- clean up primary name, because updateEntity might be called without a
+    -- prior call to deleteEntityProperties.
+    DELETE FROM name_data
+        WHERE domain_id = 0 AND entity_id = EntityID AND property_id = 20;
     IF EntityName IS NOT NULL THEN
-        INSERT INTO name_data (domain_id, entity_id, property_id, value, status, pidx) VALUES (0, EntityID, 20, EntityName, "FIX", 0);
+        INSERT INTO name_data
+                (domain_id, entity_id, property_id, value, status, pidx)
+            VALUES (0, EntityID, 20, EntityName, "FIX", 0);
     END IF;
 
     DELETE from data_type where domain_id=0 AND entity_id=0 AND property_id=EntityID;
diff --git a/tests/test_insert_update_delete.sql b/tests/test_insert_update_delete.sql
new file mode 100644
index 0000000000000000000000000000000000000000..00b119444a38a270ae06e9b15226b4c1f991c920
--- /dev/null
+++ b/tests/test_insert_update_delete.sql
@@ -0,0 +1,77 @@
+USE _caosdb_schema_unit_tests;
+BEGIN;
+CALL tap.no_plan();
+
+-- SETUP
+
+CALL entityACL(@ACLID1, "{acl1}");
+CALL entityACL(@ACLID2, "{acl2}");
+SELECT entity_id into @TextDatatypeID FROM name_data WHERE value ="TEXT";
+
+-- TESTS
+
+-- TEST insertEntity
+SELECT tap.eq(COUNT(id), 0, "No entities")
+    FROM entities WHERE id>=100;
+CALL insertEntity("EntityName", "EntityDesc", "RECORDTYPE", "{acl1}");
+
+SELECT tap.eq(COUNT(entity_id), 1, "Entity has been inserted")
+    FROM name_data WHERE value="EntityName";
+
+SELECT entity_id INTO @EntityID FROM name_data WHERE value="EntityName";
+SELECT tap.ok(@EntityID >= 100, "EntityID greater 99");
+
+SELECT tap.eq(acl, @ACLID1, "correct acl id had been assigned")
+    FROM entities WHERE id=@EntityID;
+
+
+
+-- TEST insertEntityProperty
+
+CALL insertEntity("AProperty", "APropDesc", "PROPERTY", "{acl1}");
+SELECT entity_id INTO @PropID FROM name_data WHERE value="AProperty";
+INSERT INTO data_type (domain_id, entity_id, property_id, datatype) VALUES (0, 0, @PropID, @TextDatatypeID);
+
+SELECT COUNT(*) INTO @x FROM null_data;
+SELECT tap.eq(@x, 0, "No data in null_data table");
+CALL insertEntityProperty(0, @EntityID, @PropID, "null_data", NULL, NULL, "RECOMMENDED", NULL, NULL, NULL, NULL, 0);
+SELECT COUNT(*) INTO @x FROM null_data;
+SELECT tap.eq(@x, 1, "One row in null_data table");
+
+-- TEST updateEntity
+
+CALL updateEntity(@EntityID, "NewEntityName", "NewEntityDesc", "RECORD", NULL, NULL, "{acl2}");
+
+SELECT tap.eq(COUNT(entity_id), 0, "Old Entity name not present")
+    FROM name_data WHERE value="EntityName";
+SELECT tap.eq(COUNT(entity_id), 1, "Entity name has been updated")
+    FROM name_data WHERE value="NewEntityName";
+
+SELECT tap.eq(acl, @ACLID2, "acl has been updated")
+    FROM entities WHERE id=@EntityID;
+
+-- CALL updateEntity again an update the Name
+CALL updateEntity(@EntityID, "NewerEntityName", "NewerEntityDesc", "RECORD", NULL, NULL, "{acl2}");
+CALL updateEntity(@EntityID, "NewEntityName", "NewEntityDesc", "RECORD", NULL, NULL, "{acl2}");
+
+
+-- TEST deleteEntityProperties
+
+CALL deleteEntityProperties(@EntityID);
+SELECT COUNT(*) INTO @x FROM null_data;
+SELECT tap.eq(@x, 0, "data removed from null_data table");
+
+-- TEST deleteEntity
+
+CALL deleteEntity(@EntityID);
+CALL deleteEntity(@PropID);
+SELECT COUNT(id) INTO @x FROM entities WHERE id>100;
+SELECT tap.eq(@x, 0, "entity deleted");
+
+
+
+-- TESTS END
+
+CALL tap.finish();
+ROLLBACK;
+