MySQL触发器执行顺序

时间:2015-06-18 20:18:07

标签: mysql .net vb.net triggers

我的桌子上有After InsertAfter Update触发器。如果我,在单个命令中插入记录和更新记录,触发器的触发顺序是什么?

1 个答案:

答案 0 :(得分:0)

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

这是请求的样本

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