简体   繁体   English

插入触发器之前的mysql错误代码1364:字段没有默认值和错误代码1442。无法更新存储的函数/触发器中的表

[英]mysql before insert trigger Error Code 1364: Field does not have a default value and Error Code 1442. Can't update table in stored function/trigger

I have a hierarchical table (left/right values to determine the hierarchy) in my database and I would like to add a "before insert" trigger. 我的数据库中有一个层次结构表(用左/右值确定层次结构),我想添加一个“插入前”触发器。 One of the problems with large hierarchical tables of this sort is that it can be hard to figure out what has to change when you insert a new item into the hierarchy. 这种大型层次结构表的问题之一是,当您将新项目插入层次结构时,可能很难弄清必须更改的内容。 So I wanted to automate this in my trigger. 因此,我想在触发器中使其自动化。

So, my table looks like this... 所以我的桌子是这样的...

    name varchar(25) not null,
    leftValue int not null,
    rightValue int not null

Some data... 一些数据...

    'DEFAULT',           1, 10
    'FirstChild',        2, 3
    'SecondChild',       4, 7
    'SecondChildsChild', 5, 6
    'ThirdChild',        8, 9

Now I want to add a 'FirstChildsChild'. 现在,我想添加一个“ FirstChildsChild”。 This will affect every row except default. 这将影响除默认值以外的每一行。 I would like the user to only have to do something simple like this... 我希望用户只需要做这样简单的事情...

    SET parent = 3;  -- The left value of FirstChild
    insert into myTable (name, leftValue) VALUES ('FirstChildsChild', parent)

My new table (after properly executing the trigger that I am having trouble with) should look like this... 我的新表(在正确执行我遇到麻烦的触发器之后)应如下所示:

    'DEFAULT',           1, 12
    'FirstChild',        2, 5
    'FirstChildsChild',  3, 4
    'SecondChild',       6, 9
    'SecondChildsChild', 7, 8
    'ThirdChild',        10, 11

So I wrote the following trigger... 所以我写了以下触发器...

    CREATE TRIGGER `myTrigger` BEFORE INSERT ON `myTable` FOR EACH ROW
    begin
        SET new.rightValue = new.leftValue + 1;

        UPDATE MissionFactors SET
            leftValue = leftValue + 2
        WHERE
            leftValue >= new.leftValue;

        UPDATE MissionFactors SET
            rightValue = rightValue + 2
        WHERE 
            rightValue >= new.leftValue;
    END

FINALLY my questions: 1. Since the trigger is a BEFORE insert, why won't it let me leave the right value undefined in my insert statement? 最后,我的问题是:1.由于触发器是在插入之前进行的,为什么不让我在插入语句中保留未定义的正确值? The trigger fills in the value. 触发器将填写该值。

    Error Code: 1364. Field 'rightValue' doesn't have a default value.
  1. If I do pass in a right value... 如果我传递正确的值...

     Error Code: 1442. Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

I could write a stored procedure to do all of this. 我可以编写一个存储过程来完成所有这些工作。 But I thought that this type of thing was one of the reasons why triggers were created. 但是我认为这种事情是创建触发器的原因之一。

I encountered the same problem. 我遇到了同样的问题。

You have to put the column nullable if you don't want it to appear in your insert's column list even if a trigger sets a default value. 如果您不希望列出现在插入的列列表中,即使触发器设置了默认值,也必须将列设置为可空。

For your second problem, you can't update a table while it's open for inserting. 对于第二个问题,在打开要插入的表时无法更新它。 In Oracle there is a similar problem called 'mutating table'. 在Oracle中,有一个类似的问题称为“变异表”。 If you want to do this you have to use several triggers including an after statement trigger that update the table. 如果要执行此操作,则必须使用多个触发器,包括更新表的after语句触发器。

暂无
暂无

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

相关问题 错误代码:1442。无法更新存储函数/触发器中的表“inventario”,因为它已被语句使用 - Error Code: 1442. Can't update table 'inventario' in stored function/trigger because it is already used by statement 错误代码:1442。无法更新存储的函数/触发器中的表“学生” - Error Code: 1442. Can't update table 'students' in stored function/trigger 错误代码:1442。无法更新存储函数/触发器中的表“客户”,因为它已被调用此存储 function 的语句使用 - Error Code: 1442. Can't update table 'customer' in stored function/trigger because it is already used by statement which invoked this stored function 错误代码:1442。无法更新存储触发器中的表“操作”,因为它已被调用此存储触发器的语句使用 - Error Code: 1442. Can't update table 'operation' in stored trigger because it is already used by statement which invoked this stored trigger 错误代码:1442。无法更新触发器中的表“A”,因为它已被调用此触发器的语句使用 - Error Code: 1442. Can't update table 'A' in trigger because it is already used by statement which invoked this trigger 错误1442:无法更新存储的函数/触发器中的表 - ERROR 1442: Can't update table in stored function/trigger Mysql #1442 - 无法更新存储函数/触发器中的表“库存” - Mysql #1442 - Can't update table 'stock' in stored function/trigger MySQL条件插入导致错误代码1364:“字段没有默认值” - MySQL conditional insert causing Error Code 1364: “Field doesn't have a default value” 如何解决错误 1442:无法更新 mysql 中存储功能/触发器中的表 'tb_name'? - How to solve error 1442 : Can`t update table 'tb_name' in stored fuction/trigger in mysql? 错误1442:无法更新存储函数/触发器中的表,因为调用该存储函数/触发器的语句已使用该表 - ERROR 1442: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM