home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / visdb2tk.zip / TOOLKITD.INF (.txt) < prev    next >
OS/2 Help File  |  1995-11-21  |  264KB  |  1,035 lines

  1.  
  2. ΓòÉΓòÉΓòÉ 1. Version Notice ΓòÉΓòÉΓòÉ
  3.  
  4. Second Edition (October 1995) 
  5.  
  6. References in this publication to IBM products, or services do not imply that 
  7. IBM intends to make these available in all countries in which IBM operates. 
  8. Any references to an IBM product, program, or service is not intended to state 
  9. or imply that only IBM`s product, program, or service may be used.  Any 
  10. functionally equivalent product, program, or service that does not infringe any 
  11. of IBM`s intellectual property rights may be used instead of the IBM product, 
  12. program, or service. 
  13.  
  14. The information contained in this document has not been submitted to any formal 
  15. IBM test and is distributed AS IS.  The use of information or the 
  16. implementation of any of these techniques is a customer responsibility and 
  17. depends on the customer`s ability to evaluate and integrate them into the 
  18. customer`s operational environment.  While each item may have been reviewed by 
  19. IBM for accuracy in a specific situation, there is no guarantee that the same 
  20. or similar results will be obtained elsewhere.  Customers attempting to adapt 
  21. these techniques to their own environments do so at their own risk. 
  22.  
  23. IBM may have patents or pending patent applications covering subject matter in 
  24. this document.  The furnishing of this document does not give you any license 
  25. to these patents.  You can send licence inquiries, in writing, tot he IBM 
  26. Director of Licensing, IBM Corporation, 208 Harbor Drive, Stamford, Connecticut 
  27. 06904-2501, USA. 
  28.  
  29.  
  30. ΓòÉΓòÉΓòÉ 2. Introduction ΓòÉΓòÉΓòÉ
  31.  
  32. With Version 2 of DB2 for OS/2 and AIX, IBM has exciting, leading edge 
  33. technology, relational database products for those environments. 
  34.  
  35. Version 2 provides massive enhancements of function in many areas, some of 
  36. which will radically change the way in which database applications are 
  37. developed.  Business function which would previously have been defined in a 
  38. multitude of application programs can now be defined once in the database.  New 
  39. functions which have major impact in this area include: 
  40.  
  41. o User Defined Functions 
  42.  
  43. o User Defined Distinct Data Types 
  44.  
  45. o Triggers 
  46.  
  47. o Check constraints on tables 
  48.  
  49. The use of these new capabilities depends on the generation of SQL, some of 
  50. which can be complex to produce. 
  51.  
  52. IBM has developed a toolkit of utilities using Visualizer which will make it 
  53. very much easier to exploit these new database capabilities.  The utilities 
  54. provide a front end from Visualizer for OS/2 and Visualizer for AIX to some of 
  55. the key new features in the databases. 
  56.  
  57. All of the utilities generate SQL which may be saved to a file compatible with 
  58. the DB2 Command Line Processor.  These files can then be distributed as part of 
  59. application installation processes or the SQL included in application programs. 
  60.  
  61. This document describes a demonstration of the utilities.  Working through this 
  62. demonstration will familiarise users with the utilities in the toolkit. 
  63.  
  64. Three powerful utilities are included: 
  65.  
  66.  
  67. ΓòÉΓòÉΓòÉ 2.1. Create User Defined Distinct Type ΓòÉΓòÉΓòÉ
  68.  
  69. UDTs allow users to define distinct types which identify the business class of 
  70. the data, as well as its physical representation in the database.  Combined 
  71. with User Defined Functions, this capability provides a powerful platform for 
  72. application development.  Application developers should seriously consider the 
  73. use of UDTs in any new development, as this extra information will be exploited 
  74. by future DB2 family products. 
  75.  
  76. The definition of a new type is very simple using SQL.  However, when a new 
  77. type is defined, there are no database functions available for the type.  This 
  78. utility allows new types to be defined through a GUI interface.  Then the 
  79. functions required for the type can be selected from the built-in and 
  80. user-defined functions which are compatible with the physical representation of 
  81. the new type.  This process generates the SQL to create as many functions as 
  82. required with just a few key depressions, rather than extensive manual coding. 
  83.  
  84. The utility has the following features: 
  85.  
  86. o Create User-Defined Distinct Types in a database 
  87.  
  88. o Generate SQL to create functions for the new types 
  89.  
  90. o Allow modification of the generated SQL 
  91.  
  92. o Apply the generated SQL to the database 
  93.  
  94. o Save the generated SQL to a file compatible with the DB2 Command Line 
  95.   Processor 
  96.  
  97. o Online help 
  98.  
  99.  
  100. ΓòÉΓòÉΓòÉ 2.2. Create DB2 Table ΓòÉΓòÉΓòÉ
  101.  
  102. This utility provides a GUI allowing the user to easily define DB2 tables 
  103. exploiting both new and existing database function. 
  104.  
  105. You can extract the definition of a table from DB2 Version 2 databases. The 
  106. definition can then be applied directly to other databases, or modified before 
  107. further use.  The complete SQL definition of the table can be saved to a file 
  108. which can be directly executed by the DB2 Command Line Processor.  The utility 
  109. can be used to generate up-to-date SQL for existing database tables. 
  110.  
  111. Features of tables which are covered by the utility include: 
  112.  
  113. o Database, schema and name for the table 
  114.  
  115. o Table comments 
  116.  
  117. o Specification of tablespaces 
  118.  
  119. o Data propagator options 
  120.  
  121. o Column data type definition, including UDTs 
  122.  
  123. o Primary key 
  124.  
  125. o Table check constraints 
  126.  
  127. o Additional indexes 
  128.  
  129. o Referential constraints 
  130.  
  131. o Constraint names for all constraints 
  132.  
  133. o Comments for columns, indexes and constraints 
  134.  
  135. o Online help 
  136.  
  137. All of the above information will be extracted from the database when working 
  138. with an existing table definition, and will be included when SQL is saved from 
  139. the utility, or applied to a database. 
  140.  
  141.  
  142. ΓòÉΓòÉΓòÉ 2.3. Create Trigger ΓòÉΓòÉΓòÉ
  143.  
  144. Database triggers allow processing to be defined which will be activated on 
  145. conditions defined within the trigger.  For example, an update to a particular 
  146. column in a table can activate a trigger which will cause rows to be updated or 
  147. inserted in other tables, all within the same unit of work.  Triggers can also 
  148. generate error conditions, so they can be used for extensive data validation. 
  149.  
  150. Because of the power of the trigger mechanism, the SQL statement to define a 
  151. trigger can be long and complicated.  The Toolkit Create Trigger Utility 
  152. provides a GUI front end to allow DBAs, application developers, or auditors to 
  153. create triggers. 
  154.  
  155. Features provided are: 
  156.  
  157. o Provide 'Expression Builder' for a trigger 
  158.  
  159.   (SQL must still be entered for the trigger conditions and actions) 
  160.  
  161. o Extract existing trigger definition from any database 
  162.  
  163. o Modify existing trigger definitions 
  164.  
  165. o Model new triggers on existing trigger definitions 
  166.  
  167. o Apply triggers rapidly to multiple databases 
  168.  
  169. o Save the trigger definition as SQL which can be executed directly by the DB2 
  170.   Command Line Processor 
  171.  
  172. o Online help 
  173.  
  174.  
  175. ΓòÉΓòÉΓòÉ 3. Installation ΓòÉΓòÉΓòÉ
  176.  
  177. The Visualizer DB2 Toolkit can be installed on personal computers running the 
  178. OS/2 operating system, with Visualizer Query for OS/2 installed, or on IBM Risc 
  179. System/6000 computers running the AIX Operating System and Visualizer for AIX. 
  180.  
  181.  
  182. ΓòÉΓòÉΓòÉ 3.1. Prerequisites for OS/2 ΓòÉΓòÉΓòÉ
  183.  
  184. In order to run the toolkit, you should have the following installed on your 
  185. computer. 
  186.  
  187. o Either DB2/2 version 2, or CAE/2, with access to database servers running DB2 
  188.   Version 2 Common Server. 
  189.  
  190.   The common server is currently available on OS/2 (DB2 for OS/2 Version 
  191.   Release 1) and AIX (DB2 for AIX Version 2 Release 1). Other implementations 
  192.   will be available during 1995. 
  193.  
  194. o Visualizer Query for OS/2 Version 1 Release 2. 
  195.  
  196. You must also have any products which are prerequisites for the products 
  197. listed. 
  198.  
  199.  
  200. ΓòÉΓòÉΓòÉ 3.2. Prerequisites for AIX ΓòÉΓòÉΓòÉ
  201.  
  202. In order to run the toolkit, you should have the following installed on your 
  203. computer. 
  204.  
  205. o Either DB2/6000 version 2, or CAE/6000, with access to database servers 
  206.   running DB2 Version 2 Common Server. 
  207.  
  208.   The common server is currently available on OS/2 (DB2 for OS/2 Version 
  209.   Release 1) and AIX (DB2 for AIX Version 2 Release 1). Other implementations 
  210.   will be available during 1995. 
  211.  
  212. o Visualizer Query for AIX Version 1 Release 2. 
  213.  
  214. You must also have any products which are prerequisites for the products 
  215. listed. 
  216.  
  217.  
  218. ΓòÉΓòÉΓòÉ 3.3. Installation Steps ΓòÉΓòÉΓòÉ
  219.  
  220.  
  221. ΓòÉΓòÉΓòÉ 3.3.1. Install The Toolkit on OS/2 ΓòÉΓòÉΓòÉ
  222.  
  223.  1. Place the toolkit diskette in your diskette drive. 
  224.  
  225.     The instructions that follow are for drive a:.  Modify the instructions if 
  226.     you are using a different drive. 
  227.  
  228.  2. From an OS/2 Window or Full Screen, enter the command: 
  229.  
  230.     A: 
  231.  
  232.  3. In the same OS/2 Window or Full Screen, enter the command: 
  233.  
  234.     INSTALL 
  235.  
  236. The install routine will prompt for a directory in which to install the toolkit 
  237. objects.  The toolkit requires about 500kbytes of disk space. A folder 
  238. containing icons for the 3 utilities, the on line reference manual and the on 
  239. line version of this guide will be created on the OS/2 desktop. 
  240.  
  241. Press the Cancel button to exit the installer. 
  242.  
  243.  
  244. ΓòÉΓòÉΓòÉ 3.3.2. Install The Toolkit on AIX ΓòÉΓòÉΓòÉ
  245.  
  246. The same installation diskette image contains the AIX and OS/2 versions of the 
  247. toolkit.  To install the toolkit on AIX 
  248.  
  249.  1. Place the installation disketted in the diskette drive of your Risc 
  250.     System/6000 
  251.  
  252.  2. Make your home directory the current directory. 
  253.  
  254.     cd $HOME 
  255.  
  256.  3. Make a temporary directory 
  257.  
  258.     mkdir tktemp 
  259.  
  260.  4. A shell script is provided to copy the contents of the installation 
  261.     diskette to your temporary directory.  Copy that script to your temporary 
  262.     directory: 
  263.  
  264.     dosread -a tkcopy ./tkcopy 
  265.  
  266.  5. Run the shell script to copy the files from the diskette to your temporary 
  267.     directory: 
  268.  
  269.     . tkcopy 
  270.  
  271.  6. Run the shell script tkinst to install the toolkit in the location of your 
  272.     choice: 
  273.  
  274.     . tkinst 
  275.  
  276.     When requested, you may enter the path in which the Query for AIX product 
  277.     files are installed. This has a default 
  278.  
  279.     /usr/lpp/VQ6000 
  280.  
  281.     Press ENTER to accept the default, or type the correct path and then press 
  282.     ENTER. 
  283.  
  284.     You will then be prompted for a path into which to install the toolkit. 
  285.     This will default to a subdirectory named db2tools in the home directory of 
  286.     the user installing the toolkit. 
  287.  
  288.     Press ENTER to accept the default, or type a path name of your own and then 
  289.     press ENTER. 
  290.  
  291.     The install script will set the file permissions on the toolkit files to 
  292.     allow all users to execute the toolkit. 
  293.  
  294. To be able to run the toolkit, the user must run the file tktprofile, which 
  295. will have been created in the directory you specified as the toolkit 
  296. installation directory.  This can be run from the command line as follows: 
  297.  
  298. /installpath/tktprofile 
  299.  
  300. where installpath is the directory to which the toolkit was installed. 
  301.  
  302. Add this line to the profile (.profile) of each user who will use the toolkit. 
  303.  
  304.  
  305. ΓòÉΓòÉΓòÉ 4. Demonstration Preparation ΓòÉΓòÉΓòÉ
  306.  
  307. The demonstration will be done using the database DEMO, which should be created 
  308. and populated prior to starting the demonstration. 
  309.  
  310. Note:  If you wish to run the demonstration in a Client/Server environment, you 
  311. should transfer the contents of the DEMO subdirectory to the server, and run 
  312. the following process on the server.  You should then catalog the database on 
  313. the client where the demonstration will be run. 
  314.  
  315. From a command line : 
  316.  
  317.  1. Make the directory where the toolkit was installed the current directory. 
  318.     For example: 
  319.  
  320.     cd db2tools 
  321.  
  322.  2. Change directory to the demo sub-directory. 
  323.  
  324.     cd demo 
  325.  
  326.  3. Create the demonstration database. 
  327.  
  328.     db2 -v -fdemodb.sql 
  329.  
  330.     Note:  The instructions above will create a DB2/2 database in the default 
  331.     location, and create tablespaces of approximately 12 Megabytes on drive C. 
  332.     You may customise this setup by editing the file demodb.sql and following 
  333.     the instructions in the lines starting -- **.  If you wish to create the 
  334.     demonstration database on a DB2/6000 server, you must customise the file 
  335.     'demodb.sql' to your environment. 
  336.  
  337.     If you are using the AIX version of the toolkit, you will need to bind 
  338.     Visualizer for AIX to the new database. For a default installation, the 
  339.     steps are as follows : 
  340.  
  341.     cd /usr/lpp/VQ6000/sql 
  342.  
  343.     db2 connect to demo 
  344.  
  345.     . ftqix 
  346.  
  347. Before you start the demonstration, ensure that database manager is running. 
  348. Enter the command: 
  349.  
  350. db2 connect to demo 
  351.  
  352. from an OS/2 command line, to check that the database can be accessed. 
  353.  
  354. The 3 parts of the demonstration must be done in order. 
  355.  
  356. Throughout the demonstration you will find paragraphs in this style, which 
  357. provide material for discussion. 
  358.  
  359. On completion of the demonstration, you can delete any tables, triggers and 
  360. user defined types you have created by running the command cleanup, from the 
  361. subdirectory demo under the toolkit install directory. 
  362.  
  363.  
  364. ΓòÉΓòÉΓòÉ 4.1. Database Privileges Required ΓòÉΓòÉΓòÉ
  365.  
  366. The demonstration was produced using the default user id USERID on a DB2 for 
  367. OS/2 standalone system.  USERID was also the instance owner. 
  368.  
  369. To create the demonstration database you must have SYSADM or SYSCTRL 
  370. privileges. 
  371.  
  372. To run the demonstration you should have DBADM privilege on the DEMO database. 
  373.  
  374. If you use an ID other than USERID, you should use that ID wherever USERID is 
  375. used in the demonstration. 
  376.  
  377.  
  378. ΓòÉΓòÉΓòÉ 5. Demonstrate the Create Type Utility ΓòÉΓòÉΓòÉ
  379.  
  380. Distinct Types are a powerful feature of the database. 
  381.  
  382. o They allow data types which reflect the business class of the data, not just 
  383.   the physical representation. 
  384.  
  385. o Functions can be restricted to particular Distinct Types. 
  386.  
  387. o User defined functions will encapsulate business logic which will only be 
  388.   appropriate to certain data types. 
  389.  
  390. o When developing new applications, you should seriously consider implementing 
  391.   Distinct Types for future protection. 
  392.  
  393. o In this demonstration, we create a type called AGEINMONTHS, which is used to 
  394.   represent the age of employees. 
  395.  
  396.  
  397. ΓòÉΓòÉΓòÉ 5.1. Demonstration steps ΓòÉΓòÉΓòÉ
  398.  
  399.  1. Start the Create Types utility, by double clicking on the appropriate icon. 
  400.  
  401.  2. Select the DEMO database 
  402.  
  403.     There may be some delay while the utility connects to the database, and 
  404.     checks for the existence of tables and views which are required.  These 
  405.     tables and views will be installed the first time a database is used. 
  406.  
  407.     You will see a dialog stating that database has not yet been enabled for 
  408.     the utility. 
  409.  
  410.  3. Select 'Yes' for automatic enablement. 
  411.  
  412.     Tables and views are required to provide 'Function' information for this 
  413.     utility.  These will be installed automatically. 
  414.  
  415.     Completed Create Type Form 
  416.  
  417.  4. Leave Type Schema blank, to default to current authorization ID 
  418.  
  419.  5. Enter AGEINMONTHS as the type name 
  420.  
  421.     This may be entered in upper or lower case, but the field is case 
  422.     sensitive. 
  423.  
  424.  6. Select From Source Data type INTEGER 
  425.  
  426.     The details relating to the Type creation are complete. This would be easy 
  427.     in SQL.  However, when you create a new type, no functions are available. 
  428.     This utility will replicate all functions which use the base type as a 
  429.     parameter.  We have the choice of selecting the functions to replicate 
  430.     individually, or of replicating all of the functions.  We can choose to 
  431.     replicate all of the functions, then use the selection features to deselect 
  432.     some.  Using the selection capabilities, we can select all of the functions 
  433.     from a single schema at once. 
  434.  
  435.  7. Choose Replicate all functions 
  436.  
  437.     This will generate the SQL to replicate the functions. We can examine and 
  438.     modify the SQL for individual functions.  Let's look at the MAX function. 
  439.  
  440.  8. Scroll down and double click on the MAX function. 
  441.  
  442.     Here is a simple function that will tell us who the oldest employee is. 
  443.  
  444.  9. Scroll back up and double click on the first occurrence of INSERT. 
  445.  
  446.     Edit SQL Form for Insert Function 
  447.  
  448.      This is a more complex example. The base function has 4 parameters - 
  449.     VARCHAR,INTEGER,INTEGER,VARCHAR.  We can see that the utility is generating 
  450.     three functions from this. 
  451.  
  452.         VARCHAR, AGEINMONTHS, INTEGER, VARCHAR
  453.         VARCHAR, INTEGER, AGEINMONTHS, VARCHAR
  454.         VARCHAR, AGEINMONTHS, AGEINMONTHS, VARCHAR
  455.  
  456. 10. Close the Edit SQL window by pressing the OK button 
  457.  
  458.     Now let's save the SQL and see how much SQL has been generated: 
  459.  
  460. 11. Press the Save Text... button and save as CRETYPE.SQL. 
  461.  
  462. 12. From another session, open the created SQL using the enhanced editor(EPM). 
  463.  
  464.     Notice the number of lines of SQL generated.  If we had user defined 
  465.     functions in the database, these would also be replicated as required. 
  466.  
  467. 13. Now close the editor 
  468.  
  469. 14. Select the CREATE button 
  470.  
  471. 15. Reply No to the dialog concerning warning messages from DB2. 
  472.  
  473.     Replying 'Yes' to this dialog can result in a large number of warning 
  474.     messages. It is possible to use Ctrl-Break to halt processing in this case. 
  475.     Database changes will then be rolled back. 
  476.  
  477.     This utility is a must for anybody wishing to work with Distinct Types. 
  478.     Applying the changes to the database will take some time.  When this is 
  479.     complete, we get the option to roll back the changes. 
  480.  
  481. 16. Commit the changes to the database when requested. 
  482.  
  483. 17. Select the Close button to exit the utility. 
  484.  
  485.  
  486. ΓòÉΓòÉΓòÉ 6. Demonstrate the Create Table Utility ΓòÉΓòÉΓòÉ
  487.  
  488. The create table utility is specifically designed for DB2 Version 2 Common 
  489. Server databases. It offers the following capabilities 
  490.  
  491. o Specify tablespaces 
  492.  
  493. o Columns with distinct types 
  494.  
  495. o Specify a primary key 
  496.  
  497. o Specify additional indexes 
  498.  
  499. o Specify table constraints 
  500.  
  501. o Specify referential constraints 
  502.  
  503. o Name all of the constraints - important for error messages 
  504.  
  505. The table definition can be based on an existing table in any database, or 
  506. started from scratch.  The SQL generated can be saved to a file which can be 
  507. executed directly from the DB2 command line processor.  The comments in the 
  508. saved SQL contain control information for the utility.  Saved SQL may also be 
  509. reloaded by the utility and used as the basis of other definitions. 
  510.  
  511. The utility can be used to generate table creation SQL (DDL) to be included in 
  512. application programs. 
  513.  
  514.  
  515. ΓòÉΓòÉΓòÉ 6.1. Demonstration steps ΓòÉΓòÉΓòÉ
  516.  
  517.  1. Double Click on the Visualizer Create DB2 Table 
  518.  
  519.     Select method of table creation 
  520.  
  521.     A completely new table definition can be created using the Template option. 
  522.     A definition previously saved from this utility can be modified, or a table 
  523.     can be modelled on an existing database table.  The new table can be 
  524.     created in a different database from the model table. 
  525.  
  526.  2. Select Model table on Existing table 
  527.  
  528.  3. Select Database DEMO 
  529.  
  530.     There may be a delay while the utility connects to the database and checks 
  531.     for the existence of data in system catalog tables. 
  532.  
  533.  4. Select Table schema <current schema> 
  534.  
  535.  5. Select Table name EMPLOYEE 
  536.  
  537.  6. Press OK 
  538.  
  539.     This will bring up the main Create DB2 Table panel. This panel has 3 major 
  540.     sections, used to supply all the details of the new table being created. 
  541.  
  542.  
  543. ΓòÉΓòÉΓòÉ 6.1.1. General Information ΓòÉΓòÉΓòÉ
  544.  
  545. Create DB2 Table - General Information 
  546.  
  547.  1. Select Database DEMO 
  548.  
  549.     This is the database in which the new table will be created. 
  550.  
  551.  2. Leave Table Schema as USERID 
  552.  
  553.     You should leave this as the default value 
  554.  
  555.  3. Enter the Table name WORKERS 
  556.  
  557.  4. Table Comment New table based on Employee 
  558.  
  559.  5. Select Specified Tablespace 
  560.  
  561.     The ability to specify tablespaces is a new feature or DB2 version 2.  If 
  562.     you have migrated a database from Version 1, all tables will be in a 
  563.     default tablespace.  You may wish to use this utility to re-create the 
  564.     tables in specified tablespaces. 
  565.  
  566.  6. Choose the tablespace REGULAR from the list box 
  567.  
  568.     Because REGULAR is a Database managed tablespace, you are given the options 
  569.     to select the Index Tablespace and the Long Tablespace.  Performance 
  570.     benefits may be derived by placing indexes on a separate physical drive: 
  571.  
  572.  7. Choose the tablespace INDEXSPACE as Index TableSpace 
  573.  
  574.  8. Choose the tablespace LONGSPACE as the Long tableSpace 
  575.  
  576.  9. Select No data capture 
  577.  
  578.     The data capture function is provided to facilitate propagation of changes 
  579.     to the data in the table. 
  580.  
  581. 10. Do note select OK but continue into Column Details. 
  582.  
  583.  
  584. ΓòÉΓòÉΓòÉ 6.1.2. Column Details ΓòÉΓòÉΓòÉ
  585.  
  586. Select the column details entry in the top value set control. 
  587.  
  588. Create DB2 Table - Column Details 
  589.  
  590.  1. Enter the name AGE in the Column name entry. 
  591.  
  592.  2. Select the Type Name list. 
  593.  
  594.     Note that any of the system data types may be selected. 
  595.  
  596.  3. Select the Type schema USERID 
  597.  
  598.     Note that the Position field has changed to 15, which will cause a new 
  599.     column to be added after the existing columns.  If you changed this value, 
  600.     the existing columns would be rearranged. 
  601.  
  602.  4. Select Type Name AGEINMONTHS 
  603.  
  604.     Note that this is the user defined type we created earlier. 
  605.  
  606.  5. Select Accept Nulls 
  607.  
  608.  6. Check With Default 
  609.  
  610.     Press HELP here for an explanation. We could also have entered a Column 
  611.     Remark 
  612.  
  613.  7. Select Add 
  614.  
  615.  8. Do note select OK but continue into Table Constraints. 
  616.  
  617.  
  618. ΓòÉΓòÉΓòÉ 6.1.3. Table Constraints ΓòÉΓòÉΓòÉ
  619.  
  620. Select TABLE CONSTRAINTS from the top value set 
  621.  
  622. The table constraints section is divided into 4 subsections, allowing the 
  623. specification of the different constraint types. 
  624.  
  625.  
  626. ΓòÉΓòÉΓòÉ 6.1.3.1. Primary Key ΓòÉΓòÉΓòÉ
  627.  
  628. The Primary Key section should already be selected. 
  629.  
  630. Create DB2 Table - Primary Key Constraint 
  631.  
  632.  1. Select the column EMPNO from the list of available columns 
  633.  
  634.  2. Press the Add> button 
  635.  
  636.     This will create a primary key of 1 column. 
  637.  
  638.  
  639. ΓòÉΓòÉΓòÉ 6.1.3.2. Indexes ΓòÉΓòÉΓòÉ
  640.  
  641. Select Indexes from the lower value set. 
  642.  
  643. Create DB2 Table - Index Constraints 
  644.  
  645. We are creating an index of two columns.  We could create any number of 
  646. secondary indexes.  Indexes are used to improve database performance, and can 
  647. also be used to ensure that indexed column values are unique. 
  648.  
  649.  1. From the list of available columns, Select FIRSTNME 
  650.  
  651.  2. Press the Add> button 
  652.  
  653.  3. From the list of available columns, Select LASTNAME 
  654.  
  655.  4. Press the Add> button 
  656.  
  657.  5. Press the Add button below the list of available columns. 
  658.  
  659.     The index can now be named, and a comment added. 
  660.  
  661.  6. Position the cursor in the Index Name column of Row 1 of the list box at 
  662.     the top of the panel. 
  663.  
  664.  7. Enter the Index Name FORENAME 
  665.  
  666.  8. In the Remarks column enter Index on FIRSTNME,LASTNAME 
  667.  
  668.  
  669. ΓòÉΓòÉΓòÉ 6.1.3.3. Check Clauses ΓòÉΓòÉΓòÉ
  670.  
  671. Select the Check Clauses entry in the lower value set. 
  672.  
  673. Check clauses provided the capability to define rules controlling the data 
  674. values that can be placed in a column.  This facility allows these rules to be 
  675. specified as part of the database, rather than in application programs. 
  676.  
  677. Create DB2 Table - Check Constraints 
  678.  
  679.  1. In the Check Clause control, enter the text Salary < 100000 
  680.  
  681.  2. Press the Add button 
  682.  
  683.  3. Position the cursor in the Constraint Name column of Row 1 of the list box 
  684.     at the top of the panel. 
  685.  
  686.  4. Enter the Name MAX_SALARY 
  687.  
  688.     Note that names for constraints should be chosen carefully. The constraint 
  689.     name will appear in the error message generated by an attempt to break the 
  690.     constraint rules. 
  691.  
  692.  5. In the Remarks column enter Maximum Salary 
  693.  
  694.  
  695. ΓòÉΓòÉΓòÉ 6.1.3.4. Table References ΓòÉΓòÉΓòÉ
  696.  
  697. Select the Table References entry in the lower value set. 
  698.  
  699. Table references, or referential constraints, enable the values allowed in 
  700. columns of a table to be limited to those which match primary key values in 
  701. another table. 
  702.  
  703. Create DB2 Table - Table References 
  704.  
  705.  1. Select referenced table schema USERID 
  706.  
  707.  2. Select referenced table name DEPARTMENT 
  708.  
  709.  3. Select referenced key column DEPTNO 
  710.  
  711.     This is the only primary key column for the DEPARTMENT table. 
  712.  
  713.  4. Select column WORKDEPT from the list of Potential matches. 
  714.  
  715.     The potential matches are the columns of the tables being created that 
  716.     could be used to match against the selected key column of the referenced 
  717.     table.  Where the referenced table has more than one key column, each one 
  718.     of these must be matched. 
  719.  
  720.     Delete Rule and Update Rule options can be selected. 
  721.  
  722.  5. Select Add 
  723.  
  724.  6. Enter a Constraint Name of Dept. 
  725.  
  726.  7. Enter Remarks Referential Constraint. 
  727.  
  728.  
  729. ΓòÉΓòÉΓòÉ 6.1.3.5. Save and Examine the SQL ΓòÉΓòÉΓòÉ
  730.  
  731.  1. Select the Save Text... button 
  732.  
  733.  2. Save as CRETAB.SQL. 
  734.  
  735.  3. Examine CRETAB.SQL with the OS/2 Enhanced Editor (EPM) 
  736.  
  737.     The top portion of the file contains control information which is used when 
  738.     the file is read by this utility.  Show all the statements that have been 
  739.     generated. 
  740.  
  741.     This file can be executed directly by the DB2 command line processor. 
  742.  
  743.  4. Close the editor and return to the main panel. 
  744.  
  745.  5. Select OK to create the table 
  746.  
  747.  6. Confirm that you want the changes committed when requested. 
  748.  
  749. Immediate applications for which this utility may be useful. 
  750.  
  751. o Add new features to tables from migrated databases. 
  752.  
  753.   Specify tablespaces, name existing constraints etc.  You can quickly modify 
  754.   the created SQL to export the data to an IXF file, drop the old table, create 
  755.   the new table and re-import the data. 
  756.  
  757. o Clone tables to another database 
  758.  
  759. o Generate SQL to include in applications 
  760.  
  761. o A table can be modified using Alter statements.  This utility will then 
  762.   produce updated DDL for the new table. 
  763.  
  764.  
  765. ΓòÉΓòÉΓòÉ 7. Demonstrate the Create Trigger Utility ΓòÉΓòÉΓòÉ
  766.  
  767. Triggers are probably the most obviously powerful feature of DB2 v2.  They are 
  768. an excellent feature for client/server applications, as they can be used to 
  769. minimise the distributed application logic by defining this within the 
  770. database. Use triggers for: 
  771.  
  772. o Data validation 
  773.  
  774. o Related updates 
  775.  
  776. o Audit trail production 
  777.  
  778. o Validation - A trigger can produce a customised error message 
  779.  
  780. The syntax of triggers is quite difficult - we help to get this right by 
  781. minimising the SQL the user must write. 
  782.  
  783.  
  784. ΓòÉΓòÉΓòÉ 7.1. Demonstration steps ΓòÉΓòÉΓòÉ
  785.  
  786.  1. Double click on the Create Trigger icon. 
  787.  
  788.     Select The Model for a Trigger 
  789.  
  790.     As for tables, triggers can be created from an existing trigger, from a 
  791.     saved Create Trigger statement, or from an empty template.  In this 
  792.     demonstration we will work from an empty template. 
  793.  
  794.  2. Select Model trigger on Template 
  795.  
  796.  3. Press OK 
  797.  
  798.  
  799. ΓòÉΓòÉΓòÉ 7.1.1. Create DB2 Trigger Details ΓòÉΓòÉΓòÉ
  800.  
  801. Create DB2 Trigger Details 
  802.  
  803. This form allows the creation of general information relating to the trigger 
  804.  
  805.  1. Select the database DEMO 
  806.  
  807.     There may be some delay while the utility connects to the database. 
  808.  
  809.  2. Leave the trigger schema as the default. 
  810.  
  811.  3. Enter a trigger name MAX_INCREASE 
  812.  
  813.  4. Enter the comment Limit salary increase 
  814.  
  815.  5. Select Apply trigger To each row 
  816.  
  817.     A single SQL statement can update more than one row in the table.  The 
  818.     trigger can be defined so that it is actioned once for each row, or once 
  819.     for the whole statement. We have chosen to action this trigger once for 
  820.     each row. 
  821.  
  822.  6. Select Apply trigger After database changes 
  823.  
  824.     Triggers defined as 'Before database changes' will be applied before any 
  825.     updates in the statement activating the trigger.  The trigger actions will 
  826.     not cause any other triggers to be activated.  Triggers defined as 'After 
  827.     database changes' will be applied after the updates in the statement 
  828.     activating the trigger, and the trigger actions may activate further 
  829.     triggers. 
  830.  
  831.  7. Select the Table schema USERID 
  832.  
  833.  8. Select the Table name WORKERS 
  834.  
  835.  9. Select the Action Update 
  836.  
  837.     Triggers can be activated by Deletes, Inserts or Updates on the specified 
  838.     table.  For update actions, columns may also be specified.  If no columns 
  839.     are specifed, the trigger will be activated by updates on any column in the 
  840.     table. 
  841.  
  842. 10. Select the column SALARY 
  843.  
  844. 11. Click on the ... button to expand additional conditions 
  845.  
  846.     Create DB2 Trigger Conditions 
  847.  
  848.     In the trigger conditions and trigger actions section, the SQL can 
  849.     reference the values of the rows affected by the statement causing the 
  850.     trigger both before and after the updates caused by the statement. 
  851.  
  852.     The values of the current row before and after the updates can be 
  853.     referenced by the Old and New correlation names.  The values of the set of 
  854.     affected rows can be referenced by the old and new temporary table names. 
  855.     Old and New correlation names can only be referenced for triggers defined 
  856.     as 'For each row'.  Old values cannot be referenced in a trigger actioned 
  857.     by Insert.  New values cannot be referenced in a trigger actioned by 
  858.     delete. 
  859.  
  860. 12. Enter the following in the multi line edit control: 
  861.  
  862.     newcorr.salary > oldcorr.salary * 1.1 
  863.  
  864.      The utility provides default correlation and temporary table names, which 
  865.     can be overridden if other names are preferred.  This trigger will only be 
  866.     actioned if salary is increased by more than 10%. 
  867.  
  868. 13. Select OK to close the window 
  869.  
  870. 14. Click on the ... button to expand SQL statements to be executed 
  871.  
  872.     Create DB2 Trigger Actions 
  873.  
  874. 15. Enter the following values in the main control: 
  875.  
  876.     Signal SQLSTATE '75001' ('Salary increase must not exceed 10%') 
  877.  
  878.     This trigger will prevent salary increases of more than 10% and give a 
  879.     meaningful error message.  We could have extended the trigger to allow some 
  880.     users to make these changes. 
  881.  
  882. 16. Select OK to close the window 
  883.  
  884. 17. Select Save text... 
  885.  
  886. 18. Save SQL to a file CRETRIG.SQL 
  887.  
  888. 19. Examine CRETRIG.SQL with the OS/2 Enhanced Editor (EPM), to view the SQL 
  889.     that has been created. 
  890.  
  891. 20. Close the editor. 
  892.  
  893. 21. Press the Apply button to apply the trigger to the database. 
  894.  
  895.     Note that by using Apply instead of OK we can now amend the trigger 
  896.     definition and apply it again.  We could also apply the same trigger 
  897.     definition to a different database. 
  898.  
  899. 22. Don't forget to run cleanup before you repeat this demonstration. 
  900.  
  901. This is a very simple use of a trigger.  Triggers can be defined such that 
  902. application programs only need to update one table, with other dependent 
  903. updates being defined in triggers. 
  904.  
  905. This utility eases the trigger definition process.  Triggers can be rapidly 
  906. cloned across databases.  The SQL generated can be saved to files, and executed 
  907. directly from the DB2 command line processor. 
  908.  
  909. Note:  Where a trigger contains multiple 'action' statements, these must be 
  910. separated by a semicolon.  Because of this, the tilde character ╤Ä is used as 
  911. the terminator for the complete Create Trigger statement.  Use saved files from 
  912. the Command Line Processor using the td option, for example: 
  913.  
  914. db2 connect to demo db2 -v -fcretrig.sql -td╤Ä db2 connect reset 
  915.  
  916. You can try the above commands, and see the SQL passed to the DB2 command line 
  917. processor.  The commands will not all be successful because the trigger already 
  918. exists. 
  919.  
  920.  
  921. ΓòÉΓòÉΓòÉ 8. Further Information ΓòÉΓòÉΓòÉ
  922.  
  923. For further information contact IBM EAS Product Support: 
  924.  
  925. IBM Internal        EASPS at WINVMD 
  926.  
  927. IEA                 GBIBM8L2 at IBMMAIL 
  928.  
  929. Internet            GBIBM8L2@IBMMAIL.COM 
  930.  
  931. Fax                 +44 (0)1926 410764 
  932.  
  933.  
  934. ΓòÉΓòÉΓòÉ 9. Printing This Document ΓòÉΓòÉΓòÉ
  935.  
  936. This document is installed as an on line document as part of the toolkit, and 
  937. can be viewed by double clicking on the 'Tutorial' icon. 
  938.  
  939. You can obtain a hard copy of this document by selecting Print and then All 
  940. Sections from the Services menu of the on line document viewer. 
  941.  
  942. A PostScript version of the document is also available.  This is available from 
  943. the library of the Visualizer Section of IBMDB2 Forum on CompuServe.  Download 
  944. the file TKDEMO.ZIP to your DOS or OS/2 workstation.  You must uncompress this 
  945. file using either PKUNZIP or PKUNZIP2, and then print the resulting postscript 
  946. file. 
  947.  
  948.  1. Open an OS/2 Window or Full Screen 
  949.  
  950.  2. Copy the file TKDEMO.ZIP to a temporary directory 
  951.  
  952.  3. Make the temporary directory the current directory 
  953.  
  954.  4. Uncompress the file 
  955.  
  956.     PKUNZIP TKDEMO.ZIP 
  957.  
  958.              or
  959.     PKUNZIP2 TKDEMO.ZIP 
  960.  
  961.  5. Copy the file to a postscript printer. If your postscript printer is on 
  962.     LPT1: 
  963.  
  964.     COPY TOOLKITD.PS LPT1 
  965.  
  966.  
  967. ΓòÉΓòÉΓòÉ 10. How to Obtain EAS Documents ΓòÉΓòÉΓòÉ
  968.  
  969. Further copies of this document, or others in this series, are available from 
  970. the Visualizer section of the library in the IBMDB2Forum on CompuServe. 
  971.  
  972. For each package the following files exist: 
  973.  
  974. o PackageName.ZIP 
  975.  
  976.   This ZIP file contains the full document, including illustrations, in 
  977.   postscript form.  The ZIP file also contains sample files related to the 
  978.   document and bitmaps of each document illustration.  Each package contains a 
  979.   README describing the files in that package. 
  980.  
  981. o PackageName.TXT 
  982.  
  983.   This file contains the document in ASCII form.  Figure descriptions are 
  984.   included, along with the file name of the bitmap for the related artwork from 
  985.   the .ZIP file. You may include the bitmaps in the text using most current 
  986.   word processors. 
  987.  
  988.   or 
  989.  
  990. o PackageName.INF 
  991.  
  992.   As documents are updated, they will be supplied as INF files instead of ASCII 
  993.   files.  This format can be viewed using the INF viewer supplied with OS/2, or 
  994.   using Windows and AIX viewers available from the same CompuServe library as 
  995.   the documents.  INF files contain all illustrations and artwork, and can be 
  996.   printed using the capabilities of the INF viewer. 
  997.  
  998. Packages Available Include: 
  999.  
  1000. VQM         Visualizer Query for OS/2 - hints and tips for Query Manager Users 
  1001.  
  1002. VDB2        Using Visualizer (for OS/2 or AIX) with DB2 for MVS or DB2/VM 
  1003.  
  1004. VDB2WS      Using Visualizer (for OS/2 or AIX) with DB2/2 or DB2/6000 
  1005.  
  1006. VAS400      Using Visualizer (for OS/2 or AIX) with AS/400 Systems 
  1007.  
  1008. VWAS40      Visualizer for Windows with AS/400 Systems 
  1009.  
  1010. Instructions for obtaining materials via the IBM network are provided on the 
  1011. following page. 
  1012.  
  1013. IBM Employees may obtain the documents in tersed LIST3820 format, together with 
  1014. associated files, using the following commands: 
  1015.  
  1016. TOOLS SENDTO WINVMB TOOLS EASWGS GET PackageName PACKAGE
  1017. Packages Available Include: 
  1018.  
  1019. VISQM       Visualizer Query for OS/2.  Hints and tips for Query Manager Users 
  1020.  
  1021. VISDB2      Using Visualizer (for OS/2 or AIX) with DB2 for MVS or DB2/VM 
  1022.  
  1023. VISDB2WS    Using Visualizer (for OS/2 or AIX) with DB2/2 or DB2/6000 
  1024.  
  1025. VISAS400    Using Visualizer (for OS/2 or AIX) with AS/400 Systems 
  1026.  
  1027. VWAS40      Visualizer for Windows with AS/400 Systems - Connectivity Guide 
  1028.  
  1029. TOOLKIT     The Visualizer DB2 Version 2 Toolkit - Programs and Documentation 
  1030.  
  1031. The EASWGS disk contains over 100 marketing and technical deliverables.  To 
  1032. obtain a complete list, enter the following command: 
  1033.  
  1034. TOOLS SENDTO WINVMB TOOLS EASWGS LIST * PACKAGE
  1035.