首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL触发器执行顺序

MySQL触发器执行顺序
EN

Stack Overflow用户
提问于 2015-06-19 04:18:07
回答 1查看 1.1K关注 0票数 0

我有一个既有After Insert触发器又有After Update触发器的表。如果我在一个命令中同时插入记录和更新记录,那么触发器触发的顺序是什么?

EN

回答 1

Stack Overflow用户

发布于 2015-06-22 22:22:54

在构建了一个测试项目之后,我能够得到答案。触发器按照行在表中的顺序触发。因此,如果我有一个带有After Insert、After Update和After Delete触发器的表,它们将按照触发它们的行的顺序触发。这在MySQL 5.6中进行了测试。

这是您要的样品

代码语言:javascript
复制
Public Function UpdateTriggerrecords(ByVal dt As DataTable) As Boolean
    Try
        Return PerformUpdate(dt, SETUPTriggerrecords_UPDATE(), SETUPTriggerrecords_INSERT(), SETUPTriggerrecords_DELETE(), False)
    Catch ex As Exception
        Throw ex
    End Try
End Function

Private Function SETUPTriggerrecords_UPDATE() As MySqlCommand
    Dim cmd As New MySqlCommand("UPDATE tbltriggerrecords SET strDescription=@strDescription, intTestingValue=@intTestingValue " & _
                                "WHERE  idTriggerRecords=@idTriggerRecords")
    CreateTriggerrecordsParameters(cmd)
    CreateTriggerrecordsIDParameters(cmd)
    Return cmd
End Function

Private Function SETUPTriggerrecords_INSERT() As MySqlCommand
    Dim cmd As New MySqlCommand("INSERT INTO tbltriggerrecords(strDescription,intTestingValue) " & _
                                            "VALUES(@strDescription,@intTestingValue)")

    CreateTriggerrecordsParameters(cmd)
         Return cmd
End Function

Private Function SETUPTriggerrecords_DELETE() As MySqlCommand
    Dim cmd As New MySqlCommand("DELETE FROM tbltriggerrecords WHERE idTriggerRecords=@idTriggerRecords")
    CreateTriggerrecordsIDParameters(cmd)
    Return cmd
End Function

Private Sub CreateTriggerrecordsIDParameters(ByVal cmd As MySqlCommand)
    cmd.Parameters.Add("@idTriggerRecords", MySqlDbType.Int32).SourceColumn = "idTriggerRecords"
End Sub

Private Sub CreateTriggerrecordsParameters(ByVal cmd As MySqlCommand)
    cmd.Parameters.Add("@strDescription", MySqlDbType.VarChar).SourceColumn = "strDescription"
    cmd.Parameters.Add("@intTestingValue", MySqlDbType.Int32).SourceColumn = "intTestingValue"
End Sub

Private Function PerformUpdate(ByVal dt As DataTable, ByVal cmdUpdate As MySqlCommand, ByVal cmdInsert As MySqlCommand, ByVal cmdDelete As MySqlCommand, ByVal capturePrimaryKeyValue As Boolean) As Boolean 
    If connMySQL Is Nothing Then
        connMySQL = New MySqlConnection(connstrMySQL)
    End If
    Using connMySQL
        If connMySQL.State <> ConnectionState.Open Then
            connMySQL.ConnectionString = connstrMySQL
            connMySQL.Open()
        End If
        trans = connMySQL.BeginTransaction
        Dim da As New MySqlDataAdapter("", connMySQL)
        cmdUpdate.Connection = connMySQL
        cmdUpdate.Transaction = trans
        cmdInsert.Connection = connMySQL
        cmdInsert.Transaction = trans
        If cmdDelete IsNot Nothing Then
            cmdDelete.Connection = connMySQL
            cmdDelete.Transaction = trans
        End If
        Try
            If capturePrimaryKeyValue = True Then
            AddHandler da.RowUpdated, New MySqlRowUpdatedEventHandler(AddressOf OnRowUpdatedMySQL)
            End If
            da.UpdateCommand = cmdUpdate
            da.InsertCommand = cmdInsert
            If cmdDelete IsNot Nothing Then
                da.DeleteCommand = cmdDelete
            End If
            da.Update(dt)
            If capturePrimaryKeyValue = True Then
                RemoveHandler da.RowUpdated, AddressOf OnRowUpdatedMySQL
            End If
            PerformUpdate = True
            trans.Commit()
        Catch ex As Exception
            trans.Rollback()
            PerformUpdate = False
        Finally
            If connMySQL.State <> ConnectionState.Closed Then
                connMySQL.Close()
            End If
        End Try
    End Using
End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30925046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档