home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / database / informix / 3019 < prev    next >
Encoding:
Internet Message Format  |  1993-01-21  |  13.8 KB

  1. Path: sparky!uunet!olivea!gossip.pyramid.com!pyramid!infmx!cheetah!billd
  2. From: billd@informix.com (William Daul)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: Informix triggers
  5. Message-ID: <billd.727603393@cheetah>
  6. Date: 21 Jan 93 08:03:13 GMT
  7. References: <1993Jan20.165128.9762@dssmktg.uucp> <1993Jan21.025302.14166@gateway.ssf-sys.DHL.COM>
  8. Sender: news@informix.com (Usenet News)
  9. Organization: Informix Software, Inc.
  10. Lines: 333
  11.  
  12.  
  13.                    TechInfo # 4035
  14.  
  15. Short Description:
  16. Informix Triggers
  17.  
  18. Long Description: 
  19. From: Informix Times magazine 
  20. =============================
  21.  
  22.  
  23. Informix Triggers a New Release
  24.  
  25. Informix now offers version 5.01 of its database servers, INFORMIX-OnLine and
  26. INFORMIX-SE, providing an additional area of functionality-triggers.      
  27.  
  28. Triggers are database mechanisms that automatically invoke or "trigger" a
  29. specified set of SQL statements or stored procedures whenever a particular event
  30. occurs within a database table.
  31.  
  32.  Over the past year or so, "triggers" has become a catch phrase in the database
  33. community, specifically as developers working with systems from other RDBMS
  34. vendors have relied on triggers primarily to enforce referential integrity
  35. rules. Since the 5.0 release of its database server products in early 1992,
  36. Informix has provided for referential integrity through ANSI-compliant
  37. declarative integrity constraints, enabling developers to take referential
  38. integrity for granted and focus on the other aspects of their database design.
  39.  
  40. With triggers, database developers can automatically and universally enforce
  41. business rules or conditions every time a table is modified. When used in this
  42. way, triggers are an important tool in the continuing struggle against developer
  43. backlogs, providing multiple advantages. By enforcing business rules across
  44. applications automatically and without additional coding, triggers help
  45. companies turn out highly consistent and functional applications faster to meet
  46. their business objectives. With triggers in their arsenals, developers can
  47. eradicate incongruities in their databases because the trigger will be
  48. consistent for any given table across all applications. Triggers also provide
  49. flexibility-if later on a developer wishes to add other conditions to a table,
  50. it need only be done once and it will be enforced from then on every time a
  51. table is modified.
  52.  
  53. Stored Procedures
  54.  
  55. Triggers are associated with a table and a triggering event. Inserts, deletes
  56. and updates are triggering events. When a triggering event occurs, the triggered
  57. action can be a series of SQL statements or a stored procedure. A stored
  58. procedure is an application procedure stored in the database. Stored procedures
  59. can include SQL statements and program statements that are used to define
  60. variables, assign and compare values, and control the flow of execution within
  61. the stored procedure. Stored procedures have unique names and in addition to
  62. being invoked by a trigger, they may also be executed by an application program
  63. with a call to the database server. 
  64.  
  65. The trigger or application program may pass parameters to the stored procedure
  66. and the stored procedure does its work and may return values to the program.
  67. Generally people end up creating entire libraries filled with stored procedures
  68. common to more than one application program. 
  69.  
  70. Understanding the value of stored procedures, Informix has provided the
  71. capability to utilize them since the 5.0 database server release. Any new
  72. triggers integrated into existing applications can be used to invoke any new or
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. existing stored procedures. Stored procedures are valuable in several ways.
  80.  
  81. 1) Consistent Application Logic
  82.  
  83. First, since stored procedures reside in the database and not in the
  84. applications, they are only written once and they may be triggered or called by
  85. any application program. The stored procedure written for one application will
  86. be consistent across all applications eliminating the possibility of error that
  87. recoding for every application would introduce while saving programmer time.
  88.  
  89. 2) Performance
  90.  
  91. Another valuable aspect of a stored procedure is that the SQL statements within
  92. it will be parsed and optimized when the stored procedure is created, making
  93. these procedures more efficient than if this had to take place at run time. Upon
  94. execution of the stored procedure, the database server only needs to make sure
  95. that the user passes the security checks and that all the database objects that
  96. are referenced by the stored procedure currently exist in the database.
  97.  
  98. The requests from stored procedures are executed within the database server
  99. process and not the application process. So the SQL statements in the stored
  100. procedure do not generate messages between the application and the database
  101. server. If the logic of the stored procedure were to reside in the application
  102. program, each SQL request would generate messages between the application and
  103. the server, creating additional message traffic. So stored procedures are
  104. especially beneficial in a networked environment because they reduce the number
  105. of messages that must travel across the network.
  106.  
  107. 3) Security
  108.  
  109. Stored procedures offer another means of implementing flexible security measures
  110. by allowing you to define limited access to your database. Stored procedures are
  111. subject to the same security checks as any other parts of the database such as
  112. tables or views. So granting a user access to a stored procedure limits that
  113. user to only those operations performed in the stored procedure. It does not
  114. grant the user the ability to access the tables or views from outside of the
  115. stored procedure. Nor do you need to grant a user of a stored procedure the
  116. ability to read or modify the database. Stored procedures allow you to raise the
  117. security from the data level to the procedure level, eliminating the possibility
  118. of a user writing their own routines to access the database.
  119.  
  120. Triggering Events     
  121.  
  122. A trigger can invoke an SQL statement or a stored procedure whenever certain
  123. triggering events occur against the database. These events include: an insert, a
  124. delete or an update. Informix allows one insert, one delete, and multiple update
  125. triggers for each table in the customer's 5.0 database. Each trigger is
  126. completely user-defined and can be attached to any table. So rather than the
  127. application calling the stored procedure, when any user attempts a modification
  128. for example, the previously-defined trigger will invoke the stored procedure. It
  129. may help to visualize this functionality by thinking of a trigger as an actual
  130. attachment to a table. Anytime someone wants to modify a table which has a
  131. trigger defined for that type of modification, it "triggers" the stored
  132. procedure.
  133.  
  134. Oftentimes the procedure the trigger initiates is designed to ensure that a
  135. logical relationship within the database has not been violated by the
  136. modification. Since the procedure is automatically initiated rather than called
  137. by the application program, no user can bypass the policy. 
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. Integrity Constraints
  146.  
  147. So why hasn't INFORMIX-OnLine had triggers before now? The notable use of
  148. triggers in the industry has been to achieve referential integrity-some database
  149. vendors provide no other means to that end. Informix chose a different approach.
  150. Instead of utilizing triggers to implement integrity constraints, Informix opted
  151. instead to fully implement the ANSI SQL89 standard for integrity constraints. By
  152. strictly following the guidelines of ANSI SQL89's integrity enhancement
  153. specification, Informix was able to fully implement integrity constraints
  154. without the use of triggers. In fact, although one could use triggers within
  155. OnLine 5.01 to implement referential integrity constraints, it would be a wasted
  156. trigger. It is far more efficient and the process is much less susceptible to
  157. error to create referential integrity constraints via a schema declaration when
  158. the database is designed as opposed to creating referential integrity later on
  159. using triggers.
  160.  
  161. The two most common types of integrity constraints in a database are entity
  162. integrity and referential integrity. Entity integrity encompasses default and
  163. permissible values as well as uniqueness. In other words, it enforces
  164. restrictions on what values may exist in a specific data column. 
  165.  
  166. Referential integrity enforces a master/detail (sometimes called a parent-child)
  167. relationship between records in a table. Referential integrity will not allow
  168. detail (or child) records to exist without a corresponding master (or parent)
  169. record. So any attempt to delete a master record would fail, due to referential
  170. integrity, if detail records existed for the same order.
  171.  
  172. Other RDBMSs use triggers or rules (trigger-like mechanisms) to enforce
  173. integrity. Not only does this violate the ANSI standard, but it unnecessarily
  174. invokes a stored procedure adding to system overhead. The database server
  175. itself, when designed using ANSI SQL89, can handle integrity constraints in a
  176. more straightforward, less taxing way. INFORMIX-OnLine does not need the
  177. developer to properly code a stored procedure to enforce integrity constraints.
  178. Informix enforces integrity automatically via the specification of integrity
  179. constraints for the database server when the database was defined. 
  180.  
  181. Why Triggers Now
  182.  
  183. If Informix implements integrity constraints using declarative integrity,
  184. meaning it is defined as a part of the table, why bother implementing triggers
  185. now? There are many other useful operations that can be accomplished using
  186. triggers including creating audit trails, implementing business rules, or
  187. automatically imputing data.
  188.  
  189. Audit Trails
  190.  
  191. If you would like to track activity within a database, triggers can be extremely
  192. helpful. You could easily set up a trigger which not only logs all updates to a
  193. table, but also stores the user's name and the time of the update. Database
  194. activity which requires a log of that activity can be implemented using triggers
  195. (see example, Creating an Audit Trail).
  196.  
  197. Business Rules
  198.  
  199. What constitutes a business rule? Perhaps an organization enforces limits and
  200. conditions upon what can and cannot be updated in a database (see example,
  201. Enforcing Business Rules). Certainly accounting, governmental, corporate, or
  202. departmental regulations could all fall into this category and be implemented
  203. with triggers. For example, in an accounting environment a business rule may
  204. exist that restricts a customer from placing an order if any invoices to that
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. customer are over 90 days past due. In this case, you could set up a trigger
  212. through which any attempt to write to the order table would first check a
  213. dunning table. 
  214.  
  215. Another example of a business rule: a former employee will not be able to file a
  216. COBRA claim beyond 90 days after termination unless a COBRA extension has been
  217. filed. In this case a trigger could be set up to search a table containing COBRA
  218. extensions filed if the 90-day limit is superseded.
  219.  
  220. Automatically Writing Back to the Database
  221.  
  222. You may use a trigger to perform calculations, and, if you want, write that new
  223. data back into the database, creating new values as you go. In other words, data
  224. which is not immediately available from the triggering statement may be derived
  225. by the stored procedure that is triggered (see example, Writing Back to the
  226. Database with Triggers). For example, if you were recording occurrences of
  227. seismic events by region and wanted to automatically summarize a number of
  228. regions into zones, you could create a trigger which adds several regions and
  229. records the result in a column called zone.
  230.  
  231. Cascading Triggers
  232.  
  233. Triggers can also cascade-the action of one trigger can set off another one.
  234. Cascades may be set up to trigger up to a maximum of 61 triggers in a series,
  235. including the initial trigger. Trigger one could set off triggers two through
  236. sixty. A system catalog table called "systriggers" is available, allowing you to
  237. query to find out what triggers exist for a particular table.
  238.  
  239. Tracing Triggers
  240.  
  241. If you find that the triggered action is not behaving as expected, you can
  242. monitor its execution by placing it inside a stored procedure and using the
  243. trace capabilities of Stored Procedure Language (SPL). You can trace the
  244. procedure statements and variables. The trace output reveals the values of
  245. procedure variables, procedure arguments, return values, and error codes helping
  246. you to decided why your results were not what you expected.
  247.  
  248. Triggers' Greatest Value
  249.  
  250. In summary, a trigger calls an SQL statement or a stored procedure, a
  251. pre-defined activity that you have stored in the database. Its greatest value
  252. lies in the fact that since you've already stored it, you don't have to compile
  253. it, you don't have to parse it,  the database server doesn't need to check its
  254. syntax, you don't have to optimize it, and you don't have to figure out how to
  255. access the data. All the database server has to do is execute it and then return
  256. back values from it. 
  257.  
  258. Release 5.01 is now available on Sun Microsystems computers. Availability on
  259. other platforms will be announced as they are released; please check
  260. InformixLink or call the office nearest you for information about your specific
  261. platform.
  262.  
  263.  - - - - -
  264.  
  265. Informix has provided for referential integrity through ANSI-compliant
  266. declarative integrity constraints, enabling developers to take referential
  267. integrity for granted and focus on the other aspects of their database design.
  268.  
  269. The notable use of triggers in the industry has been to achieve referential
  270. integrity-some database vendors provide no other means to that end. Informix
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. chose a different approach.
  278.  
  279. By strictly following ANSI SQL89's integrity enhancement specification, Informix
  280. was able to fully implement integrity constraints without the use of triggers.
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. -- 
  341. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  342.  William Daul             Advanced Support      INFORMIX SOFTWARE INC.
  343.  4100 Bohannon Dr.        (415) 926-6488 - wk
  344.  Menlo Park, CA. 94025    uunet!infmx!billd  or  billd@informix.com
  345.