Triggers and Stored Procedures

https://blog.csdn.net/jesse621/article/details/9452049

Flip-flops, simple, stored procedures, and clear

use

 

 


 

Trigger action:
main role is to trigger a complex which enables referential integrity and data can not be guaranteed by the primary and foreign keys consistency. It is possible for the relevant database tables modified cascade, CHECK constraints force than the more complex data integrity, and custom action message, maintenance state before and after the non-normalized data and comparison data modification. CHECK constraints different trigger may refer to other columns in the table. Using triggers to implement complex referential integrity under the following conditions; mandatory integrity between the data. When you create a multi-line trigger, when the insert, update, delete multiple rows of data, you must write a trigger handle multiple rows of data. Perform cascading update or cascade delete such action. Cascade modify database of all related tables. Revocation or rollback violate referential integrity of the operation, to prevent illegal to modify the data.
The difference between the trigger and stored procedures:
         The main difference is that triggers and stored procedures run mode trigger. The user must call the stored procedure, or triggers an application to display and perform, and when the trigger is the occurrence of a particular time, or performed automatically activated, regardless of the connection with the user in a database, or an application. When a row is inserted, updated or deleted when the trigger was executed, but also depends on how the trigger is created, use an update trigger UPDATE occurs when using an insert trigger occurs when the INSERT, DELETE occur when using a delete trigger.


 

Knowledge about the database stored procedures and triggers, this is a very technical details, as long as the general will quickly master its use. After so many years, I usually design the database, they also will be more or less use of stored procedures and triggers, the reason is very simple: good performance, Ye Hao business to achieve. But when doing the last item, because the business is complex, the number of stored procedures and triggers are reached as many hundreds, this is a very frightening thing, especially in the wrong time debugging and maintenance, I do not think these things will be good. Now I sort of experience from the point of view about the views of stored procedures and triggers.

       1, the trigger is a special stored procedure.

         This sentence will often appear in textbooks, which indicates that the two are very much tied, my general understanding is a hidden trigger is a stored procedure, because it requires no argument, no call display, often in without your knowledge I have done a lot of action. From this perspective, because it is hidden, potentially increasing the complexity of the system, non-DBA personnel database will be difficult to understand, because it does not perform simply do not feel its presence. Again, when it comes to complex logic of nested triggers it is unavoidable, if we involve several stored procedures, coupled with the transaction and so on, it is prone to deadlock, and then when debugging will often go from one of the triggers another, constantly retroactive to cascade, it is easy to make large head. In fact, from the performance, and the flip-flop did not improve performance much, just from the code, it may be easy to achieve business at the time of coding, so my point is: abandon the trigger! The basic function of the trigger can be used to implement stored procedures.

       2, the stored procedure has many advantages, can often use

  •     You can encapsulate data logic and business rules so that users can access data and objects intended for use only by developers and database administrators.
  •     All parameters stored procedure to verify user input may be used to prevent SQL injection attacks. If you use dynamic SQL, be sure to command parameters of, and must not include the parameter values ​​directly in the query string.
  •     It may prohibit ad hoc query and data modification. This will prevent users from accidentally or maliciously damage data or execute the query, to avoid degrading the performance of the server or network.
  •     Error codes may be processed in the process, the directly transmitted without error to the client application. This prevents returns an error message, it may help to avoid detection attack. Recorded in the server error and processes.
  •     The stored procedure can only be written once, many applications can be accessed.
  •     The client application does not need to know anything about the underlying data structure. As long as the changes do not affect the data type parameter list or return to, you can change the stored procedure code, without having to make changes in the client application.
  •     Stored procedures by combining a plurality of operations to process a call to reduce network traffic.
  •     Security is good - you can access without having to execute a stored procedure has permission to directly manipulate the underlying tables
  •     Reducing network traffic - stored procedure may comprise multiple SQL statements, but as long as a statement to execute the stored procedure, thereby reducing the client application to the server call number and length
  •     Fast execution - the stored procedure syntax checking and compilation, compiled version stored in the first execution in the cache, called again for
  •     Ensure consistency - If you only modify the data through stored procedures, you can eliminate the problems caused by accidental modification to reduce operator error and programmers - because the less information transfer, making it easier to perform complex tasks, SQL less prone to error


        3, consider fatal portability, the stored procedure
        if a system used to use stored procedures, business logic that the system is too dependent on the database, which would give the system an additional increase in the business logic layer of the database, if the development when using sql server, and later found that the data is too large, the need to improve the performance of transplant oracle or mysql, which would be very troublesome, equivalent to rewrite the stored procedure, which can not be tolerated. We usually do the project, it is often a function of the client implementation was difficult, it is easy to implement on the server side, so a lot of people will choose to do on the server side, but for the future to stay hidden. When demand analysis of the project, be sure to consider performance issues, how long it is likely to upgrade, if the amount of data is small, sql server for decades with no problem, it can be a multi-use stored procedures; but the amount of data is likely to gradually add up to ten million or even more, we have to consider the portability of the system, and this time try not to use stored procedures, all the code control.
        4, stored procedure code reusability is poor.
        This object-oriented thinking entirely useless in a stored procedure, two very similar functionality also requires two stored procedures, because they are independent of each other, you can call each other, but can not inherit operations such as object-oriented, which means increase the amount of code.

        To sum up: in general small systems (simple logic), the stored procedures and triggers can be multi-purpose, after all, ms designs, you can largely improve performance; in a complex system, we recommend without triggers, stored procedures less .

Guess you like

Origin www.cnblogs.com/Dfrank/p/11823962.html