home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / rsql.zip / RSQL.ANS < prev    next >
Text File  |  1994-01-13  |  88KB  |  1,212 lines

  1. EXEC DBA: CONTINUE_ON_ERROR
  2.  
  3.  
  4. EXEC DBA: STARTUSE SAMPLE S
  5.  
  6.  OK. sql_rcd = 0         Execution time =     5.2800 secs
  7.  
  8.  
  9. --  *------------------------------------------------------------------*
  10. --  * SCRIPT  1:  A simple SQL Query Demonstration                     *
  11. --  *------------------------------------------------------------------*
  12.  
  13. --  ****** Display all rows and columns from the ORG table        ******
  14. EXEC SQL: SELECT * FROM ORG
  15.           ORDER BY DIVISION, LOCATION
  16.  
  17.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  18.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  19.  10        Head Office     160      Corporate   New York                                                                                                                                                                    
  20.  38        South Atlantic  30       Eastern     Atlanta                                                                                                                                                                     
  21.  15        New England     50       Eastern     Boston                                                                                                                                                                      
  22.  20        Mid Atlantic    10       Eastern     Washington                                                                                                                                                                  
  23.  42        Great Lakes     100      Midwest     Chicago                                                                                                                                                                     
  24.  51        Plains          140      Midwest     Dallas                                                                                                                                                                      
  25.  84        Mountain        290      Western     Denver                                                                                                                                                                      
  26.  66        Pacific         270      Western     San Francisco                                                                                                                                                               
  27.  
  28.  Total Number of Rows SELECTED = 8
  29.  
  30.  OK. sql_rcd = 0         Execution time =     4.1500 secs
  31.  
  32.  
  33. --  ****** Display all rows and columns from the STAFF table      ******
  34. EXEC SQL: SELECT * FROM STAFF
  35.           ORDER BY NAME
  36.  
  37.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  38.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  39.  180     Abrahams   38      Clerk  3       12009.75    236.50                                                                                                                                                               
  40.  330     Burke      66      Clerk  1       10988.00     55.50                                                                                                                                                               
  41.  240     Daniels    10      Mgr    5       19260.25  NULL                                                                                                                                                                   
  42.  300     Davis      84      Sales  5       15454.50    806.10                                                                                                                                                               
  43.  340     Edwards    84      Sales  7       17844.00   1285.00                                                                                                                                                               
  44.  140     Fraye      51      Mgr    6       21150.00  NULL                                                                                                                                                                   
  45.  350     Gafney     84      Clerk  5       13030.50    188.00                                                                                                                                                               
  46.  320     Gonzales   66      Sales  4       16858.20    844.00                                                                                                                                                               
  47.  310     Graham     66      Sales  13      21000.00    200.30                                                                                                                                                               
  48.  50      Hanes      15      Mgr    10      20659.80  NULL                                                                                                                                                                   
  49.  80      James      20      Clerk  NULL    13504.60    128.20                                                                                                                                                               
  50.  260     Jones      10      Mgr    12      21234.00  NULL                                                                                                                                                                   
  51.  170     Kermisch   15      Clerk  4       12258.50    110.10                                                                                                                                                               
  52.  90      Koonitz    42      Sales  6       18001.75   1386.70                                                                                                                                                               
  53.  270     Lea        66      Mgr    9       18555.50  NULL                                                                                                                                                                   
  54.  210     Lu         10      Mgr    10      20010.00  NULL                                                                                                                                                                   
  55.  230     Lundquist  51      Clerk  3       13369.80    189.65                                                                                                                                                               
  56.  30      Marenghi   38      Mgr    5       17506.75  NULL                                                                                                                                                                   
  57.  160     Molinare   10      Mgr    7       22959.20  NULL                                                                                                                                                                   
  58.  120     Naughton   38      Clerk  NULL    12954.75    180.00                                                                                                                                                               
  59.  110     Ngan       15      Clerk  5       12508.20    206.60                                                                                                                                                               
  60.  40      O'Brien    38      Sales  6       18006.00    846.55                                                                                                                                                               
  61.  20      Pernal     20      Sales  8       18171.25    612.45                                                                                                                                                               
  62.  100     Plotz      42      Mgr    7       18352.80  NULL                                                                                                                                                                   
  63.  60      Quigley    38      Sales  NULL    16808.30    650.25                                                                                                                                                               
  64.  290     Quill      84      Mgr    10      19818.00  NULL                                                                                                                                                                   
  65.  70      Rothman    15      Sales  7       16502.83   1152.00                                                                                                                                                               
  66.  10      Sanders    20      Mgr    7       18357.50  NULL                                                                                                                                                                   
  67.  200     Scoutten   42      Clerk  NULL    11508.60     84.20                                                                                                                                                               
  68.  220     Smith      51      Sales  7       17654.50    992.80                                                                                                                                                               
  69.  190     Sneider    20      Clerk  8       14252.75    126.50                                                                                                                                                               
  70.  250     Wheeler    51      Clerk  6       14460.00    513.30                                                                                                                                                               
  71.  150     Williams   51      Sales  6       19456.50    637.65                                                                                                                                                               
  72.  280     Wilson     66      Sales  9       18674.50    811.50                                                                                                                                                               
  73.  130     Yamaguchi  42      Clerk  6       10505.90     75.60                                                                                                                                                               
  74.  
  75.  Total Number of Rows SELECTED = 35
  76.  
  77.  OK. sql_rcd = 0         Execution time =     0.3700 secs
  78.  
  79.  
  80. --  *------------------------------------------------------------------*
  81. --  * SCRIPT  2:  A demonstration of the WHERE search clause           *
  82. --  *------------------------------------------------------------------*
  83.  
  84. --  ****** A simple relational predicate with no duplicate rows   ******
  85. EXEC SQL: SELECT DISTINCT *
  86.           FROM ORG
  87.           WHERE DIVISION = 'EASTERN'
  88.  
  89.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  90.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  91.  
  92.  Total Number of Rows SELECTED = 0
  93.  
  94.  OK. sql_rcd = 0         Execution time =     0.4300 secs
  95.  
  96.  
  97. --  ****** Using OR to connect relational predicates              ******
  98. EXEC SQL: SELECT *
  99.           FROM STAFF
  100.           WHERE (JOB = 'SALES' AND YEARS > 8)
  101.              OR (JOB = 'SALES' AND COMM >= 1200)
  102.  
  103.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  104.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  105.  
  106.  Total Number of Rows SELECTED = 0
  107.  
  108.  OK. sql_rcd = 0         Execution time =     0.5100 secs
  109.  
  110.  
  111. --  ****** Wild card pattern-matching using the LIKE predicate    ******
  112. EXEC SQL: SELECT ID, NAME
  113.           FROM STAFF
  114.           WHERE NAME LIKE 'LU%'
  115.  
  116.  ID      NAME                                                                                                                                                                                                               
  117.  ------  ---------                                                                                                                                                                                                          
  118.  
  119.  Total Number of Rows SELECTED = 0
  120.  
  121.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  122.  
  123.  
  124. --  ****** The BETWEEN predicate for comparing ranges of values   ******
  125. EXEC SQL: SELECT ID, NAME, SALARY
  126.           FROM STAFF
  127.           WHERE SALARY BETWEEN 20000 AND 21000
  128.  
  129.  ID      NAME       SALARY                                                                                                                                                                                                  
  130.  ------  ---------  --------                                                                                                                                                                                                
  131.  50      Hanes      20659.80                                                                                                                                                                                                
  132.  210     Lu         20010.00                                                                                                                                                                                                
  133.  310     Graham     21000.00                                                                                                                                                                                                
  134.  
  135.  Total Number of Rows SELECTED = 3
  136.  
  137.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  138.  
  139.  
  140. --  ****** The IN predicate for testing group membership          ******
  141. EXEC SQL: SELECT ID, NAME, DEPT
  142.           FROM STAFF
  143.           WHERE DEPT IN (66, 84)
  144.  
  145.  ID      NAME       DEPT                                                                                                                                                                                                    
  146.  ------  ---------  ------                                                                                                                                                                                                  
  147.  270     Lea        66                                                                                                                                                                                                      
  148.  280     Wilson     66                                                                                                                                                                                                      
  149.  290     Quill      84                                                                                                                                                                                                      
  150.  300     Davis      84                                                                                                                                                                                                      
  151.  310     Graham     66                                                                                                                                                                                                      
  152.  320     Gonzales   66                                                                                                                                                                                                      
  153.  330     Burke      66                                                                                                                                                                                                      
  154.  340     Edwards    84                                                                                                                                                                                                      
  155.  350     Gafney     84                                                                                                                                                                                                      
  156.  
  157.  Total Number of Rows SELECTED = 9
  158.  
  159.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  160.  
  161.  
  162. --  ****** The NULL predicate for testing NULL values             ******
  163. EXEC SQL: SELECT ID, NAME, DEPT
  164.           FROM STAFF
  165.           WHERE YEARS IS NULL
  166.  
  167.  ID      NAME       DEPT                                                                                                                                                                                                    
  168.  ------  ---------  ------                                                                                                                                                                                                  
  169.  60      Quigley    38                                                                                                                                                                                                      
  170.  80      James      20                                                                                                                                                                                                      
  171.  120     Naughton   38                                                                                                                                                                                                      
  172.  200     Scoutten   42                                                                                                                                                                                                      
  173.  
  174.  Total Number of Rows SELECTED = 4
  175.  
  176.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  177.  
  178.  
  179. --  *------------------------------------------------------------------*
  180. --  * SCRIPT  3:  A demonstration of the ORDER by clause               *
  181. --  *------------------------------------------------------------------*
  182.  
  183. --  ****** The default sorting order is ascending (ASC)           ******
  184. EXEC SQL: SELECT DEPT, NAME, ID
  185.           FROM STAFF
  186.           WHERE (DEPT = 10 OR DEPT = 20)
  187.           ORDER BY DEPT, NAME
  188.  
  189.  DEPT    NAME       ID                                                                                                                                                                                                      
  190.  ------  ---------  ------                                                                                                                                                                                                  
  191.  10      Daniels    240                                                                                                                                                                                                     
  192.  10      Jones      260                                                                                                                                                                                                     
  193.  10      Lu         210                                                                                                                                                                                                     
  194.  10      Molinare   160                                                                                                                                                                                                     
  195.  20      James      80                                                                                                                                                                                                      
  196.  20      Pernal     20                                                                                                                                                                                                      
  197.  20      Sanders    10                                                                                                                                                                                                      
  198.  20      Sneider    190                                                                                                                                                                                                     
  199.  
  200.  Total Number of Rows SELECTED = 8
  201.  
  202.  OK. sql_rcd = 0         Execution time =     0.1400 secs
  203.  
  204.  
  205. --  ****** You can override the default by specifying (DESC)      ******
  206. EXEC SQL: SELECT *
  207.           FROM ORG
  208.           ORDER BY DIVISION DESC, DEPTNUMB DESC
  209.  
  210.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  211.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  212.  84        Mountain        290      Western     Denver                                                                                                                                                                      
  213.  66        Pacific         270      Western     San Francisco                                                                                                                                                               
  214.  51        Plains          140      Midwest     Dallas                                                                                                                                                                      
  215.  42        Great Lakes     100      Midwest     Chicago                                                                                                                                                                     
  216.  38        South Atlantic  30       Eastern     Atlanta                                                                                                                                                                     
  217.  20        Mid Atlantic    10       Eastern     Washington                                                                                                                                                                  
  218.  15        New England     50       Eastern     Boston                                                                                                                                                                      
  219.  10        Head Office     160      Corporate   New York                                                                                                                                                                    
  220.  
  221.  Total Number of Rows SELECTED = 8
  222.  
  223.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  224.  
  225.  
  226. --  *------------------------------------------------------------------*
  227. --  * SCRIPT  4:  A demonstration of simple NESTED Queries             *
  228. --  *------------------------------------------------------------------*
  229.  
  230. --  ****** The inner SELECT returns a single value                ******
  231. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  232.           FROM STAFF
  233.           WHERE JOB = 'Sales'
  234.           AND SALARY > (
  235.           SELECT AVG(SALARY) FROM STAFF WHERE JOB = 'Mgr')
  236.  
  237.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  238.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  239.  Graham     Sales  13      21000.00    200.30                                                                                                                                                                               
  240.  
  241.  Total Number of Rows SELECTED = 1
  242.  
  243.  OK. sql_rcd = 0         Execution time =     0.2200 secs
  244.  
  245.  
  246. --  ****** The inner SELECT returns multiple values               ******
  247. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  248.           FROM STAFF
  249.           WHERE JOB = 'Sales'
  250.           AND YEARS > ALL (
  251.           SELECT YEARS FROM STAFF WHERE JOB = 'Mgr')
  252.  
  253.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  254.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  255.  Graham     Sales  13      21000.00    200.30                                                                                                                                                                               
  256.  
  257.  Total Number of Rows SELECTED = 1
  258.  
  259.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  260.  
  261.  
  262. --  ****** Different tables involved in inner and outer queries   ******
  263. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  264.           FROM STAFF
  265.           WHERE JOB = 'Sales'
  266.           AND DEPT IN (
  267.           SELECT DEPTNUMB FROM ORG WHERE DIVISION = 'Eastern')
  268.           ORDER BY YEARS
  269.  
  270.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  271.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  272.  O'Brien    Sales  6       18006.00    846.55                                                                                                                                                                               
  273.  Rothman    Sales  7       16502.83   1152.00                                                                                                                                                                               
  274.  Pernal     Sales  8       18171.25    612.45                                                                                                                                                                               
  275.  Quigley    Sales  NULL    16808.30    650.25                                                                                                                                                                               
  276.  
  277.  Total Number of Rows SELECTED = 4
  278.  
  279.  OK. sql_rcd = 0         Execution time =     0.9000 secs
  280.  
  281.  
  282. --  *------------------------------------------------------------------*
  283. --  * SCRIPT  5:  A demonstration of compount NESTED queries           *
  284. --  *------------------------------------------------------------------*
  285.  
  286. --  ****** Two subqueries at the same level                       ******
  287. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  288.           FROM STAFF
  289.           WHERE JOB = 'Sales'
  290.           AND (salary + comm) >
  291.               (SELECT MIN(salary) FROM staff where job='Mgr')
  292.           AND dept IN
  293.               (SELECT deptnumb from org WHERE division='Eastern')
  294.           ORDER BY job, salary
  295.  
  296.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  297.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  298.  Rothman    Sales  7       16502.83   1152.00                                                                                                                                                                               
  299.  O'Brien    Sales  6       18006.00    846.55                                                                                                                                                                               
  300.  Pernal     Sales  8       18171.25    612.45                                                                                                                                                                               
  301.  
  302.  Total Number of Rows SELECTED = 3
  303.  
  304.  OK. sql_rcd = 0         Execution time =     0.3700 secs
  305.  
  306.  
  307. --  ****** A subquery inside another subquery                     ******
  308. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  309.           FROM STAFF
  310.           WHERE JOB = 'Sales'
  311.           AND years > ALL
  312.               (SELECT years FROM staff where job='Mgr'
  313.                AND dept IN
  314.                    (SELECT deptnumb FROM org WHERE division='Eastern'))
  315.           ORDER BY years
  316.  
  317.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  318.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  319.  Graham     Sales  13      21000.00    200.30                                                                                                                                                                               
  320.  
  321.  Total Number of Rows SELECTED = 1
  322.  
  323.  OK. sql_rcd = 0         Execution time =     0.1600 secs
  324.  
  325.  
  326. --  *------------------------------------------------------------------*
  327. --  * SCRIPT  6:  A demonstration of CORRELATED subqueries             *
  328. --  *------------------------------------------------------------------*
  329.  
  330. --  ****** Inner query runs once for each job category            ******
  331. EXEC SQL: SELECT NAME, JOB, YEARS, SALARY, COMM
  332.           FROM STAFF S
  333.           WHERE SALARY =
  334.               (SELECT MAX(SALARY) FROM STAFF WHERE JOB = S.JOB)
  335.           ORDER BY JOB, SALARY
  336.  
  337.  NAME       JOB    YEARS   SALARY    COMM                                                                                                                                                                                   
  338.  ---------  -----  ------  --------  --------                                                                                                                                                                               
  339.  Wheeler    Clerk  6       14460.00    513.30                                                                                                                                                                               
  340.  Molinare   Mgr    7       22959.20  NULL                                                                                                                                                                                   
  341.  Graham     Sales  13      21000.00    200.30                                                                                                                                                                               
  342.  
  343.  Total Number of Rows SELECTED = 3
  344.  
  345.  OK. sql_rcd = 0         Execution time =     0.4000 secs
  346.  
  347.  
  348. --  *------------------------------------------------------------------*
  349. --  * SCRIPT  7:  A demonstration of set operators                     *
  350. --  *------------------------------------------------------------------*
  351.  
  352. --  ****** Merging two queries with the INTESECT set operator     ******
  353. EXEC SQL: SELECT NAME, JOB
  354.           FROM STAFF
  355.           WHERE JOB = 'Sales'
  356.           INTERSECT
  357.           SELECT NAME, JOB
  358.           FROM STAFF
  359.           WHERE YEARS > 10
  360.  
  361.  1          2                                                                                                                                                                                                               
  362.  ---------  -----                                                                                                                                                                                                           
  363.  Graham     Sales                                                                                                                                                                                                           
  364.  
  365.  Total Number of Rows SELECTED = 1
  366.  
  367.  OK. sql_rcd = 0         Execution time =     0.2200 secs
  368.  
  369.  
  370. --  ****** Merging two queries with the EXCEPT set operator       ******
  371. EXEC SQL: SELECT NAME, JOB
  372.           FROM STAFF
  373.           WHERE JOB = 'Sales'
  374.           EXCEPT
  375.           SELECT NAME, JOB
  376.           FROM STAFF
  377.           WHERE YEARS > 10
  378.           ORDER BY 1
  379.  
  380.  1          2                                                                                                                                                                                                               
  381.  ---------  -----                                                                                                                                                                                                           
  382.  Davis      Sales                                                                                                                                                                                                           
  383.  Edwards    Sales                                                                                                                                                                                                           
  384.  Gonzales   Sales                                                                                                                                                                                                           
  385.  Koonitz    Sales                                                                                                                                                                                                           
  386.  O'Brien    Sales                                                                                                                                                                                                           
  387.  Pernal     Sales                                                                                                                                                                                                           
  388.  Quigley    Sales                                                                                                                                                                                                           
  389.  Rothman    Sales                                                                                                                                                                                                           
  390.  Smith      Sales                                                                                                                                                                                                           
  391.  Williams   Sales                                                                                                                                                                                                           
  392.  Wilson     Sales                                                                                                                                                                                                           
  393.  
  394.  Total Number of Rows SELECTED = 11
  395.  
  396.  OK. sql_rcd = 0         Execution time =     0.1300 secs
  397.  
  398.  
  399. --  ****** Merging two queries with the UNION set operator        ******
  400. EXEC SQL: SELECT NAME, JOB
  401.           FROM STAFF
  402.           WHERE JOB = 'Sales'
  403.           UNION
  404.           SELECT NAME, JOB
  405.           FROM STAFF
  406.           WHERE YEARS > 10
  407.           ORDER BY 1
  408.  
  409.  1          2                                                                                                                                                                                                               
  410.  ---------  -----                                                                                                                                                                                                           
  411.  Davis      Sales                                                                                                                                                                                                           
  412.  Edwards    Sales                                                                                                                                                                                                           
  413.  Gonzales   Sales                                                                                                                                                                                                           
  414.  Graham     Sales                                                                                                                                                                                                           
  415.  Jones      Mgr                                                                                                                                                                                                             
  416.  Koonitz    Sales                                                                                                                                                                                                           
  417.  O'Brien    Sales                                                                                                                                                                                                           
  418.  Pernal     Sales                                                                                                                                                                                                           
  419.  Quigley    Sales                                                                                                                                                                                                           
  420.  Rothman    Sales                                                                                                                                                                                                           
  421.  Smith      Sales                                                                                                                                                                                                           
  422.  Williams   Sales                                                                                                                                                                                                           
  423.  Wilson     Sales                                                                                                                                                                                                           
  424.  
  425.  Total Number of Rows SELECTED = 13
  426.  
  427.  OK. sql_rcd = 0         Execution time =     0.1700 secs
  428.  
  429.  
  430. --  *------------------------------------------------------------------*
  431. --  * SCRIPT  8:  A demonstration of JOINS                             *
  432. --  *------------------------------------------------------------------*
  433.  
  434. --  ****** Two-table equijoin using fully qualified names         ******
  435. EXEC SQL: SELECT ORG.DEPTNAME, ORG.LOCATION, STAFF.NAME, STAFF.SALARY
  436.           FROM STAFF, ORG
  437.           WHERE ORG.MANAGER = STAFF.ID
  438.           ORDER BY ORG.DEPTNAME
  439.  
  440.  DEPTNAME        LOCATION       NAME       SALARY                                                                                                                                                                           
  441.  --------------  -------------  ---------  --------                                                                                                                                                                         
  442.  Great Lakes     Chicago        Plotz      18352.80                                                                                                                                                                         
  443.  Head Office     New York       Molinare   22959.20                                                                                                                                                                         
  444.  Mid Atlantic    Washington     Sanders    18357.50                                                                                                                                                                         
  445.  Mountain        Denver         Quill      19818.00                                                                                                                                                                         
  446.  New England     Boston         Hanes      20659.80                                                                                                                                                                         
  447.  Pacific         San Francisco  Lea        18555.50                                                                                                                                                                         
  448.  Plains          Dallas         Fraye      21150.00                                                                                                                                                                         
  449.  South Atlantic  Atlanta        Marenghi   17506.75                                                                                                                                                                         
  450.  
  451.  Total Number of Rows SELECTED = 8
  452.  
  453.  OK. sql_rcd = 0         Execution time =     0.1800 secs
  454.  
  455.  
  456. --  ****** Two-table equijoin using correlation names             ******
  457. EXEC SQL: SELECT O.DEPTNAME, O.LOCATION, S.NAME, S.SALARY
  458.           FROM STAFF S, ORG O
  459.           WHERE O.MANAGER = S.ID
  460.           ORDER BY O.DEPTNAME
  461.  
  462.  DEPTNAME        LOCATION       NAME       SALARY                                                                                                                                                                           
  463.  --------------  -------------  ---------  --------                                                                                                                                                                         
  464.  Great Lakes     Chicago        Plotz      18352.80                                                                                                                                                                         
  465.  Head Office     New York       Molinare   22959.20                                                                                                                                                                         
  466.  Mid Atlantic    Washington     Sanders    18357.50                                                                                                                                                                         
  467.  Mountain        Denver         Quill      19818.00                                                                                                                                                                         
  468.  New England     Boston         Hanes      20659.80                                                                                                                                                                         
  469.  Pacific         San Francisco  Lea        18555.50                                                                                                                                                                         
  470.  Plains          Dallas         Fraye      21150.00                                                                                                                                                                         
  471.  South Atlantic  Atlanta        Marenghi   17506.75                                                                                                                                                                         
  472.  
  473.  Total Number of Rows SELECTED = 8
  474.  
  475.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  476.  
  477.  
  478. --  *------------------------------------------------------------------*
  479. --  * SCRIPT  9:  Creating Tables, Inserts, Updates and Deletes        *
  480. --  *------------------------------------------------------------------*
  481.  
  482. --  ****** Create the staff table's twin                          ******
  483. EXEC SQL: CREATE TABLE NEWSTAFF
  484.                        (ID        SMALLINT NOT NULL,
  485.                         NAME      VARCHAR(9),
  486.                         DEPT      SMALLINT,
  487.                         JOB       CHAR(5),
  488.                         YEARS     SMALLINT,
  489.                         SALARY    DECIMAL(7,2),
  490.                         COMM      DECIMAL(7,2))
  491.  
  492.  OK. sql_rcd = 0         Execution time =     2.2800 secs
  493.  
  494.  
  495. --  ****** Insert two rows into the NEWSTAFF table                ******
  496. EXEC SQL: INSERT INTO NEWSTAFF
  497.           VALUES (500, 'Bob', 99, 'Nerds', 5, 45000.00, 0)
  498.  
  499.  OK. sql_rcd = 0         Execution time =     0.1600 secs
  500.  
  501.  
  502. EXEC SQL: INSERT INTO NEWSTAFF
  503.           VALUES (501, 'Dan', 99, 'Nerds', 4, 42000.00, 0)
  504.  
  505.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  506.  
  507.  
  508. --  ****** Use a subquery to insert rows from STAFF               ******
  509. EXEC SQL: INSERT INTO NEWSTAFF
  510.                  SELECT * FROM STAFF
  511.                  WHERE  JOB = 'Sales' AND SALARY > 18000
  512.  
  513.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  514.  
  515.  
  516. --  ****** COMMIT the work done so far                            ******
  517. EXEC SQL: COMMIT
  518.  
  519.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  520.  
  521.  
  522. --  ****** Let's see what we've got                               ******
  523. EXEC SQL: SELECT * FROM NEWSTAFF
  524.           ORDER BY SALARY DESC
  525.  
  526.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  527.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  528.  500     Bob        99      Nerds  5       45000.00     00.00                                                                                                                                                               
  529.  501     Dan        99      Nerds  4       42000.00     00.00                                                                                                                                                               
  530.  310     Graham     66      Sales  13      21000.00    200.30                                                                                                                                                               
  531.  150     Williams   51      Sales  6       19456.50    637.65                                                                                                                                                               
  532.  280     Wilson     66      Sales  9       18674.50    811.50                                                                                                                                                               
  533.  20      Pernal     20      Sales  8       18171.25    612.45                                                                                                                                                               
  534.  40      O'Brien    38      Sales  6       18006.00    846.55                                                                                                                                                               
  535.  90      Koonitz    42      Sales  6       18001.75   1386.70                                                                                                                                                               
  536.  
  537.  Total Number of Rows SELECTED = 8
  538.  
  539.  OK. sql_rcd = 0         Execution time =     0.4200 secs
  540.  
  541.  
  542. --  ****** Let's give ourselves a good raise                      ******
  543. EXEC SQL: UPDATE NEWSTAFF
  544.           SET SALARY = SALARY * 1.80
  545.           WHERE JOB = 'Nerds'
  546.  
  547.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  548.  
  549.  
  550. --  ****** Things look better now                                 ******
  551. EXEC SQL: SELECT * FROM NEWSTAFF
  552.           WHERE SALARY + COMM > 21000
  553.           ORDER BY SALARY DESC
  554.  
  555.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  556.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  557.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  558.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  559.  310     Graham     66      Sales  13      21000.00    200.30                                                                                                                                                               
  560.  
  561.  Total Number of Rows SELECTED = 3
  562.  
  563.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  564.  
  565.  
  566. --  ****** Here's how DELETE works                                ******
  567. EXEC SQL: DELETE FROM NEWSTAFF
  568.           WHERE JOB = 'Sales'
  569.  
  570.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  571.  
  572.  
  573. --  ****** Let's see what happened                                ******
  574. EXEC SQL: SELECT * FROM NEWSTAFF
  575.           ORDER BY SALARY DESC
  576.  
  577.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  578.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  579.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  580.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  581.  
  582.  Total Number of Rows SELECTED = 2
  583.  
  584.  OK. sql_rcd = 0         Execution time =     0.1100 secs
  585.  
  586.  
  587. --  *------------------------------------------------------------------*
  588. --  * SCRIPT 10:  A demonstration of COMMIT/ROLLBACK                   *
  589. --  *------------------------------------------------------------------*
  590.  
  591. --  ****** Start a unit of recovery                               ******
  592. EXEC SQL: COMMIT
  593.  
  594.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  595.  
  596.  
  597. --  ****** Let's see what we've got to start                      ******
  598. EXEC SQL: SELECT * FROM NEWSTAFF
  599.  
  600.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  601.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  602.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  603.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  604.  
  605.  Total Number of Rows SELECTED = 2
  606.  
  607.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  608.  
  609.  
  610. --  ****** Delete all the rows                                    ******
  611. EXEC SQL: DELETE FROM NEWSTAFF
  612.  
  613.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  614.  
  615.  
  616. --  ****** Show the empty table                                   ******
  617. EXEC SQL: SELECT * FROM NEWSTAFF
  618.  
  619.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  620.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  621.  
  622.  Total Number of Rows SELECTED = 0
  623.  
  624.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  625.  
  626.  
  627. --  ****** Undo the transaction                                   ******
  628. EXEC SQL: ROLLBACK
  629.  
  630.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  631.  
  632.  
  633. --  ****** Show the resurrected rows                              ******
  634. EXEC SQL: SELECT * FROM NEWSTAFF
  635.  
  636.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  637.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  638.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  639.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  640.  
  641.  Total Number of Rows SELECTED = 2
  642.  
  643.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  644.  
  645.  
  646. --  *------------------------------------------------------------------*
  647. --  * SCRIPT 11:  Data integrity with NO NULLS                         *
  648. --  *------------------------------------------------------------------*
  649.  
  650. --  ****** Here's a way to insert missing values in regular cols  ******
  651. EXEC SQL: INSERT INTO NEWSTAFF (ID, NAME, DEPT, JOB)
  652.           VALUES (506, 'Ray', 99, 'Nerds')
  653.  
  654.  OK. sql_rcd = 0         Execution time =     0.0400 secs
  655.  
  656.  
  657. --  ****** Here's another way                                     ******
  658. EXEC SQL: INSERT INTO NEWSTAFF
  659.           VALUES (507, 'Dan', 99, 'Nerds', NULL, NULL, NULL)
  660.  
  661.  OK. sql_rcd = 0         Execution time =     0.0000 secs
  662.  
  663.  
  664. --  ****** Let's see what we've got                               ******
  665. EXEC SQL: SELECT * FROM NEWSTAFF
  666.  
  667.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  668.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  669.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  670.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  671.  506     Ray        99      Nerds  NULL    NULL      NULL                                                                                                                                                                   
  672.  507     Dan        99      Nerds  NULL    NULL      NULL                                                                                                                                                                   
  673.  
  674.  Total Number of Rows SELECTED = 4
  675.  
  676.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  677.  
  678.  
  679. --  ****** Watch what happens when inserting into a NOT NULL col  ******
  680. EXEC SQL: INSERT INTO NEWSTAFF
  681.           VALUES (NULL, 'Joe', 99, 'Nerds', 1, 30000.00, 0.0)
  682.  
  683.  ERROR, sql_code = -407
  684.  
  685.  SQL0407N  Assignment of a null value to a NOT NULL column "ID" was attempted
  686.  on UPDATE or INSERT. SQLSTATE=23502 
  687.  
  688.  ------ ADDITIONAL SQLCA Info --------
  689.  SQLERRMC's:  ID
  690.  SQLERRP: SQLRSVAL
  691.  SQLERRD:     -30850         0         0         0         0         0 
  692.  
  693.  
  694. --  ****** Let's get rid of the garbage data                      ******
  695. EXEC SQL: ROLLBACK
  696.  
  697.  OK. sql_rcd = 0         Execution time =     0.0400 secs
  698.  
  699.  
  700. --  *------------------------------------------------------------------*
  701. --  * SCRIPT 12:  Data integrity with UNIQUE keys                      *
  702. --  *------------------------------------------------------------------*
  703.  
  704. --  ****** Let's try inserting a duplicate record into NEWSTAFF   ******
  705. EXEC SQL: INSERT INTO NEWSTAFF
  706.           VALUES (501, 'Dan', 99, 'Nerds', 4, 75600.00, 0)
  707.  
  708.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  709.  
  710.  
  711. --  ****** As you can see we're not protected against duplicates  ******
  712. EXEC SQL: SELECT * FROM NEWSTAFF
  713.  
  714.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  715.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  716.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  717.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  718.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  719.  
  720.  Total Number of Rows SELECTED = 3
  721.  
  722.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  723.  
  724.  
  725. --  ****** Let's get rid of this entry                            ******
  726. EXEC SQL: ROLLBACK
  727.  
  728.  OK. sql_rcd = 0         Execution time =     0.0000 secs
  729.  
  730.  
  731. --  ****** Let's protect ourselves, add a UNIQUE index on ID      ******
  732. EXEC SQL: CREATE UNIQUE INDEX XID ON NEWSTAFF (ID)
  733.  
  734. --  ****** Let's see if ENTITY INTEGRITY really works             ******
  735. EXEC SQL: INSERT INTO NEWSTAFF
  736.           VALUES (501, 'Dan', 99, 'Nerds', 4, 42000.00, 0)
  737.  
  738.  ERROR, sql_code = -104
  739.  
  740.  SQL0104N  An unexpected token "-" was found following "D ON NEWSTAFF (ID)  ".
  741.  Expected tokens may include:  "END-OF-STATEMENT". SQLSTATE=37501 
  742.  
  743.  ------ ADDITIONAL SQLCA Info --------
  744.  SQLERRMC's:  - D ON NEWSTAFF (ID)   END-OF-STATEMENT
  745.  SQLERRP: SQLRPRSR
  746.  SQLERRD:     -30850         0         0         0         0         0 
  747.  
  748.  
  749. --  ****** COMMIT the work done so far                            ******
  750. EXEC SQL: COMMIT
  751.  
  752.  OK. sql_rcd = 0         Execution time =     0.0000 secs
  753.  
  754.  
  755. --  *------------------------------------------------------------------*
  756. --  * SCRIPT 13:  Data integrity using views with CHECK option         *
  757. --  *------------------------------------------------------------------*
  758.  
  759. --  ****** Create a view PROTECT_STAFF with CHECK option          ******
  760. EXEC SQL: CREATE VIEW PROTECT_STAFF
  761.           AS SELECT * FROM NEWSTAFF
  762.           WHERE JOB = 'Nerds' OR JOB = 'Sales'
  763.           WITH CHECK OPTION
  764.  
  765.  OK. sql_rcd = 0         Execution time =     0.9400 secs
  766.  
  767.  
  768. --  ****** Let's see what's in this view so far                   ******
  769. EXEC SQL: SELECT * FROM PROTECT_STAFF
  770.  
  771.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  772.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  773.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  774.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  775.  
  776.  Total Number of Rows SELECTED = 2
  777.  
  778.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  779.  
  780.  
  781. --  ****** Let's put some more data in PROTECT_STAFF              ******
  782. EXEC SQL: INSERT INTO PROTECT_STAFF
  783.           SELECT * FROM STAFF
  784.           WHERE JOB = 'Sales' and SALARY > 20000
  785.  
  786.  OK. sql_rcd = 0         Execution time =     0.1600 secs
  787.  
  788.  
  789. --  ****** Now let's insert another well-paid nerd                ******
  790. EXEC SQL: INSERT INTO PROTECT_STAFF
  791.           VALUES (508, 'Joe', 99, 'Nerds', 4, 99000.00, 0.0)
  792.  
  793.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  794.  
  795.  
  796. --  ****** Watch what happens when we try to insert a Mgr         ******
  797. EXEC SQL: INSERT INTO PROTECT_STAFF
  798.           VALUES (603, 'Sam', 99, 'Mgr', 4, 89000.00, 0.0)
  799.  
  800.  ERROR, sql_code = -161
  801.  
  802.  SQL0161N  The resulting row of the INSERT or UPDATE does not conform to the
  803.  view definition. SQLSTATE=23501 
  804.  
  805.  ------ ADDITIONAL SQLCA Info --------
  806.  SQLERRP: SQLRIISS
  807.  SQLERRD:     -30850         0         0         0         0         0 
  808.  
  809.  
  810. --  ****** COMMIT the work done so far                            ******
  811. EXEC SQL: COMMIT
  812.  
  813.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  814.  
  815.  
  816. --  *------------------------------------------------------------------*
  817. --  * SCRIPT 14:  Enforcing Data Integrity with Referential Integrity  *
  818. --  *------------------------------------------------------------------*
  819.  
  820. --  ****** Create the ORG table's cousin                          ******
  821. EXEC SQL: CREATE TABLE NEWORG
  822.           (DEPTNUMB SMALLINT NOT NULL PRIMARY KEY,
  823.            DEPTNAME VARCHAR(14),
  824.            MANAGER  SMALLINT,
  825.            DIVISION VARCHAR(10),
  826.            LOCATION VARCHAR(13))
  827.  
  828.  OK. sql_rcd = 0         Execution time =     1.7500 secs
  829.  
  830.  
  831. --  ****** Let's put some data into it                            ******
  832. EXEC SQL: INSERT INTO NEWORG
  833.           VALUES (99, 'Software', NULL, 'Oceanview', 'Hawaii')
  834.  
  835.  OK. sql_rcd = 0         Execution time =     0.0400 secs
  836.  
  837.  
  838. EXEC SQL: INSERT INTO NEWORG
  839.           VALUES (90, 'Multimedia', NULL, 'Oceanview', 'Tahiti')
  840.  
  841.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  842.  
  843.  
  844. --  ****** Let's start with only Nerds in the NEWSTAFF table      ******
  845. EXEC SQL: DELETE FROM NEWSTAFF
  846.           WHERE JOB <> 'Nerds'
  847.  
  848.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  849.  
  850.  
  851. --  ****** Let's see what we've got so far                        ******
  852. EXEC SQL: SELECT * FROM NEWORG
  853.  
  854.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  855.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  856.  99        Software        NULL     Oceanview   Hawaii                                                                                                                                                                      
  857.  90        Multimedia      NULL     Oceanview   Tahiti                                                                                                                                                                      
  858.  
  859.  Total Number of Rows SELECTED = 2
  860.  
  861.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  862.  
  863.  
  864. EXEC SQL: SELECT * FROM NEWSTAFF
  865.  
  866.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  867.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  868.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  869.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  870.  508     Joe        99      Nerds  4       99000.00     00.00                                                                                                                                                               
  871.  
  872.  Total Number of Rows SELECTED = 3
  873.  
  874.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  875.  
  876.  
  877. --  ****** Make DEPT a foreign key to primary key in NEWORG       ******
  878. EXEC SQL: ALTER TABLE NEWSTAFF
  879.           FOREIGN KEY KEYNAME1(DEPT) REFERENCES NEWORG
  880.           ON DELETE RESTRICT
  881.  
  882.  OK. sql_rcd = 0         Execution time =     0.7200 secs
  883.  
  884.  
  885. --  ****** Insert data for a valid dept                           ******
  886. EXEC SQL: INSERT INTO NEWSTAFF
  887.           VALUES (605, 'Newhire', 99, 'Nerds', 0, 29000.00, 0.0)
  888.  
  889.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  890.  
  891.  
  892. EXEC SQL: INSERT INTO NEWSTAFF
  893.           VALUES (607, 'Newhire', 90, 'Nerds', 0, 29000.00, 0.0)
  894.  
  895.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  896.  
  897.  
  898. --  ****** Watch what happens when inserting with an invalid dept ******
  899. EXEC SQL: INSERT INTO NEWSTAFF
  900.           VALUES (609, 'Newhire', 20, 'Nerds', 0, 29000.00, 0.0)
  901.  
  902.  ERROR, sql_code = -530
  903.  
  904.  SQL0530N  The insert or update value of FOREIGN KEY "KEYNAME1" is not equal to
  905.  some value of the primary key of the parent table. SQLSTATE=23503 
  906.  
  907.  ------ ADDITIONAL SQLCA Info --------
  908.  SQLERRMC's:  KEYNAME1
  909.  SQLERRP: SQLRIEPC
  910.  SQLERRD:     -30850         0         0         0         0         0 
  911.  
  912.  
  913. --  ****** COMMIT the work done so far                            ******
  914. EXEC SQL: COMMIT
  915.  
  916.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  917.  
  918.  
  919. --  *------------------------------------------------------------------*
  920. --  * SCRIPT 15:  Enforcing Delete Rules with Referential Integrity    *
  921. --  *------------------------------------------------------------------*
  922.  
  923. --  ****** Observe how the default DELETE rule protects dependents******
  924. EXEC SQL: DELETE FROM NEWORG
  925.           WHERE LOCATION = 'Tahiti'
  926.  
  927.  ERROR, sql_code = -532
  928.  
  929.  SQL0532N  A parent row cannot be deleted because the relationship "KEYNAME1"
  930.  restricts the deletion. SQLSTATE=23504 
  931.  
  932.  ------ ADDITIONAL SQLCA Info --------
  933.  SQLERRMC's:  KEYNAME1
  934.  SQLERRP: SQLRIEDC
  935.  SQLERRD:     -30850         0         0         0         0         0 
  936.  
  937.  
  938. --  ****** COMMIT the work done so far                            ******
  939. EXEC SQL: COMMIT
  940.  
  941.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  942.  
  943.  
  944. --  ****** Let's see what data is in our tables                   ******
  945. EXEC SQL: SELECT * FROM NEWORG
  946.  
  947.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  948.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  949.  99        Software        NULL     Oceanview   Hawaii                                                                                                                                                                      
  950.  90        Multimedia      NULL     Oceanview   Tahiti                                                                                                                                                                      
  951.  
  952.  Total Number of Rows SELECTED = 2
  953.  
  954.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  955.  
  956.  
  957. EXEC SQL: SELECT * FROM NEWSTAFF
  958.  
  959.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  960.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  961.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  962.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  963.  605     Newhire    99      Nerds  0       29000.00     00.00                                                                                                                                                               
  964.  508     Joe        99      Nerds  4       99000.00     00.00                                                                                                                                                               
  965.  607     Newhire    90      Nerds  0       29000.00     00.00                                                                                                                                                               
  966.  
  967.  Total Number of Rows SELECTED = 5
  968.  
  969.  OK. sql_rcd = 0         Execution time =     0.1200 secs
  970.  
  971.  
  972. --  ****** DROP the referential constraint                        ******
  973. EXEC SQL: ALTER TABLE NEWSTAFF
  974.           DROP FOREIGN KEY KEYNAME1
  975.  
  976.  OK. sql_rcd = 0         Execution time =     0.1900 secs
  977.  
  978.  
  979. --  ****** Let's change the referential constraint to SET NULLS   ******
  980. EXEC SQL: ALTER TABLE NEWSTAFF
  981.           FOREIGN KEY KEYNAME1(DEPT) REFERENCES NEWORG
  982.           ON DELETE SET NULL
  983.  
  984.  OK. sql_rcd = 0         Execution time =     0.1600 secs
  985.  
  986.  
  987. --  ****** COMMIT the change                                      ******
  988. EXEC SQL: COMMIT
  989.  
  990.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  991.  
  992.  
  993. --  ****** Let's see what REFERENTIAL INTEGRITY does for us now   ******
  994. EXEC SQL: DELETE FROM NEWORG
  995.           WHERE LOCATION = 'Tahiti'
  996.  
  997.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  998.  
  999.  
  1000. --  ****** The Tahiti dept is dropped (this is the parent row)    ******
  1001. EXEC SQL: SELECT * FROM NEWORG
  1002.  
  1003.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  1004.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  1005.  99        Software        NULL     Oceanview   Hawaii                                                                                                                                                                      
  1006.  
  1007.  Total Number of Rows SELECTED = 1
  1008.  
  1009.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1010.  
  1011.  
  1012. --  ****** The Tahiti dept in child rows is set to NULL           ******
  1013. EXEC SQL: SELECT * FROM NEWSTAFF
  1014.  
  1015.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  1016.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  1017.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  1018.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  1019.  605     Newhire    99      Nerds  0       29000.00     00.00                                                                                                                                                               
  1020.  508     Joe        99      Nerds  4       99000.00     00.00                                                                                                                                                               
  1021.  607     Newhire    NULL    Nerds  0       29000.00     00.00                                                                                                                                                               
  1022.  
  1023.  Total Number of Rows SELECTED = 5
  1024.  
  1025.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1026.  
  1027.  
  1028. --  ****** Let's get back our data                                ******
  1029. EXEC SQL: ROLLBACK
  1030.  
  1031.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1032.  
  1033.  
  1034. --  ****** DROP the referential constraint                        ******
  1035. EXEC SQL: ALTER TABLE NEWSTAFF
  1036.           DROP FOREIGN KEY KEYNAME1
  1037.  
  1038.  OK. sql_rcd = 0         Execution time =     0.1000 secs
  1039.  
  1040.  
  1041. --  ****** Let's change the referential constraint to CASCADE     ******
  1042. EXEC SQL: ALTER TABLE NEWSTAFF
  1043.           FOREIGN KEY KEYNAME1(DEPT) REFERENCES NEWORG
  1044.           ON DELETE CASCADE
  1045.  
  1046.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  1047.  
  1048.  
  1049. --  ****** COMMIT the change                                      ******
  1050. EXEC SQL: COMMIT
  1051.  
  1052.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  1053.  
  1054.  
  1055. --  ****** Let's see what REFERENTIAL INTEGRITY does for us now   ******
  1056. EXEC SQL: DELETE FROM NEWORG
  1057.           WHERE LOCATION = 'Hawaii'
  1058.  
  1059.  OK. sql_rcd = 0         Execution time =     0.0400 secs
  1060.  
  1061.  
  1062. --  ****** The parent dept for Hawaii is gone                     ******
  1063. EXEC SQL: SELECT * FROM NEWORG
  1064.  
  1065.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  1066.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  1067.  90        Multimedia      NULL     Oceanview   Tahiti                                                                                                                                                                      
  1068.  
  1069.  Total Number of Rows SELECTED = 1
  1070.  
  1071.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1072.  
  1073.  
  1074. --  ****** All the Hawaii employees (the child rows) are also gone******
  1075. EXEC SQL: SELECT * FROM NEWSTAFF
  1076.  
  1077.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  1078.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  1079.  607     Newhire    90      Nerds  0       29000.00     00.00                                                                                                                                                               
  1080.  
  1081.  Total Number of Rows SELECTED = 1
  1082.  
  1083.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1084.  
  1085.  
  1086. --  ****** Let's get back our data, we made the point             ******
  1087. EXEC SQL: ROLLBACK
  1088.  
  1089.  OK. sql_rcd = 0         Execution time =     0.0000 secs
  1090.  
  1091.  
  1092. --  *------------------------------------------------------------------*
  1093. --  * SCRIPT 16:  Cascaded Delete rules with Referential Integrity     *
  1094. --  *------------------------------------------------------------------*
  1095.  
  1096. --  ****** Specify the PRIMARY key for NEWSTAFF                   ******
  1097. EXEC SQL: ALTER TABLE NEWSTAFF PRIMARY KEY (ID)
  1098.  
  1099.  OK. sql_rcd = 0         Execution time =     0.9700 secs
  1100.  
  1101.  
  1102. --  ****** Create the SPOUSES table                               ******
  1103. EXEC SQL: CREATE TABLE SPOUSES
  1104.           (SPOUSE_NAME VARCHAR(20),
  1105.            TELEPHONE   VARCHAR(10),
  1106.            EMPL_ID     SMALLINT,
  1107.            FOREIGN KEY HOMENUM(EMPL_ID) REFERENCES NEWSTAFF
  1108.            ON DELETE CASCADE)
  1109.  
  1110.  OK. sql_rcd = 0         Execution time =     0.7200 secs
  1111.  
  1112.  
  1113. --  ****** Insert some data into spouses                          ******
  1114. EXEC SQL: INSERT INTO SPOUSES
  1115.           VALUES ('Michiko', '4152223333', 501)
  1116.  
  1117.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  1118.  
  1119.  
  1120. EXEC SQL: INSERT INTO SPOUSES
  1121.           VALUES ('Jeri', '4154445555', 500)
  1122.  
  1123.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1124.  
  1125.  
  1126. --  ****** Let's take stock of what we've got                     ******
  1127. EXEC SQL: SELECT * FROM NEWORG
  1128.  
  1129.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  1130.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  1131.  99        Software        NULL     Oceanview   Hawaii                                                                                                                                                                      
  1132.  90        Multimedia      NULL     Oceanview   Tahiti                                                                                                                                                                      
  1133.  
  1134.  Total Number of Rows SELECTED = 2
  1135.  
  1136.  OK. sql_rcd = 0         Execution time =     0.1900 secs
  1137.  
  1138.  
  1139. EXEC SQL: SELECT * FROM NEWSTAFF
  1140.  
  1141.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  1142.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  1143.  500     Bob        99      Nerds  5       81000.00     00.00                                                                                                                                                               
  1144.  501     Dan        99      Nerds  4       75600.00     00.00                                                                                                                                                               
  1145.  605     Newhire    99      Nerds  0       29000.00     00.00                                                                                                                                                               
  1146.  508     Joe        99      Nerds  4       99000.00     00.00                                                                                                                                                               
  1147.  607     Newhire    90      Nerds  0       29000.00     00.00                                                                                                                                                               
  1148.  
  1149.  Total Number of Rows SELECTED = 5
  1150.  
  1151.  OK. sql_rcd = 0         Execution time =     0.0900 secs
  1152.  
  1153.  
  1154. EXEC SQL: SELECT * FROM SPOUSES
  1155.  
  1156.  SPOUSE_NAME           TELEPHONE   EMPL_ID                                                                                                                                                                                  
  1157.  --------------------  ----------  -------                                                                                                                                                                                  
  1158.  Michiko               4152223333  501                                                                                                                                                                                      
  1159.  Jeri                  4154445555  500                                                                                                                                                                                      
  1160.  
  1161.  Total Number of Rows SELECTED = 2
  1162.  
  1163.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1164.  
  1165.  
  1166. --  ****** Watch this cruel & unusual punishment: No more Hawaii  ******
  1167. EXEC SQL: DELETE FROM NEWORG
  1168.           WHERE  LOCATION = 'Hawaii'
  1169.  
  1170.  OK. sql_rcd = 0         Execution time =     0.1300 secs
  1171.  
  1172.  
  1173. --  ****** Let's see the the damage this has created              ******
  1174. EXEC SQL: SELECT * FROM NEWORG
  1175.  
  1176.  DEPTNUMB  DEPTNAME        MANAGER  DIVISION    LOCATION                                                                                                                                                                    
  1177.  --------  --------------  -------  ----------  -------------                                                                                                                                                               
  1178.  90        Multimedia      NULL     Oceanview   Tahiti                                                                                                                                                                      
  1179.  
  1180.  Total Number of Rows SELECTED = 1
  1181.  
  1182.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  1183.  
  1184.  
  1185. EXEC SQL: SELECT * FROM NEWSTAFF
  1186.  
  1187.  ID      NAME       DEPT    JOB    YEARS   SALARY    COMM                                                                                                                                                                   
  1188.  ------  ---------  ------  -----  ------  --------  --------                                                                                                                                                               
  1189.  607     Newhire    90      Nerds  0       29000.00     00.00                                                                                                                                                               
  1190.  
  1191.  Total Number of Rows SELECTED = 1
  1192.  
  1193.  OK. sql_rcd = 0         Execution time =     0.0300 secs
  1194.  
  1195.  
  1196. EXEC SQL: SELECT * FROM SPOUSES
  1197.  
  1198.  SPOUSE_NAME           TELEPHONE   EMPL_ID                                                                                                                                                                                  
  1199.  --------------------  ----------  -------                                                                                                                                                                                  
  1200.  
  1201.  Total Number of Rows SELECTED = 0
  1202.  
  1203.  OK. sql_rcd = 0         Execution time =     0.0600 secs
  1204.  
  1205.  
  1206. --  ****** Let's bring back our data, we made the point           ******
  1207. EXEC SQL: ROLLBACK
  1208.  
  1209.  OK. sql_rcd = 0         Execution time =     0.3500 secs
  1210.  
  1211.  
  1212.