home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / sql / update.shr < prev    next >
Text File  |  1988-02-02  |  5KB  |  180 lines

  1.  
  2.  
  3.  
  4.  
  5.          UPDATE                                                          
  6.  
  7.          SYNTAX:
  8.  
  9.          UPDATE column_name [,column_name...] 
  10.          FROM file_name
  11.  
  12.          It is assumed that the user understands the basics of the select
  13.          statement.   The syntax for the update command is similar to the 
  14.          syntax for the select command.  The basic difference is that with 
  15.          the select, the data is displayed and with the update, you are 
  16.          allowed to change all data elements retrieved. 
  17.          
  18.          EXAMPLES: 
  19.          
  20.          The following examples use the cust table which was created in 
  21.          the tutorial:
  22.  
  23.          Retrieve all fields for customer code c2 so we can change the 
  24.          name and the rating.  SSQL will display the current value.  If 
  25.          you do not want it changed, press ENTER. The '*' signifies 'all 
  26.          columns'.
  27.          
  28.          update * 
  29.          from cust 
  30.          where code='c2'; 
  31.          code            : c2                      
  32.          code            :                         
  33.          name            : Techoharps              
  34.          name            : Technoharps             
  35.          st              : OR                      
  36.          st              :                         
  37.          rating          : 15                      
  38.          rating          : 12                      
  39.          [ 1 updated ]                             
  40.  
  41.          To be more specific, we could just retrieve the name and rating:
  42.  
  43.          update name, rating
  44.          from cust 
  45.          where code = 'c2'; 
  46.  
  47.          name            : Technoharps             
  48.          name            : Techoharps              
  49.          rating          : 12
  50.          rating          : 20
  51.          [ 1 updated ]                             
  52.          
  53.  
  54.          Update all fields for all customers in Arizona
  55.  
  56.          update * 
  57.          from cust
  58.          where st='AZ';
  59.  
  60.  
  61.                                    UPDATE-1
  62.          
  63.  
  64.  
  65.  
  66.  
  67.  
  68.          Update all fields for customer named Organomice
  69.  
  70.          update * 
  71.          from cust
  72.          where name='Organomice';
  73.          
  74.          If you did not remember exactly how the customer name was 
  75.          spelled, you could use the like clause to update the customer 
  76.          name(s) that begin with 'Organo'.
  77.  
  78.          update * 
  79.          from cust 
  80.          where name like 'Organo%';
  81.  
  82.  
  83.          The next example is a bit more involved.  We want to increase the 
  84.          rating of branch b4 customers who have purchased above average 
  85.          quantities (overall).
  86.  
  87.          ALL DETAILS ON THE ADVANCED USE OF THE WHERE CLAUSE ARE TO BE 
  88.          FOUND IN THE FULL DOCUMENTATION WHICH YOU CAN GET BY 
  89.          REGISTERING!!
  90.  
  91.          There are a variety of components in the where clause:
  92.  
  93.          1.  We use a subquery to select only "distinct" codes.  Without the 
  94.          distinct modifier we would be prompted for multiple instances of 
  95.          the customer if the branch b4 customer purchased above average 
  96.          quantities more than once.                    
  97.  
  98.          2.  (cc = code) 
  99.          Since the the rows we want to update are partially based on the 
  100.          branch code, we need to join the cust table and the s table since 
  101.          the rating is in the cust table and the branch code is in the s 
  102.          table.  We join them by referring to both cust and s in the from 
  103.          clause and in the where clause we set the common columns equal to 
  104.          each other (cc = code).  
  105.          
  106.          3.  bc = 'b4' (branch code is equal to b4)
  107.  
  108.          4.  qty > (select avg(qty)
  109.                     from s)
  110.              This first calculates the average quantity of all the rows in 
  111.              the s table. Then it makes sure that the qty for the branch 
  112.              b4 customer is over that average. 
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.                                    UPDATE-2
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.          update code, rating 
  128.          from cust where code in
  129.           (select distinct code
  130.            from cust, s 
  131.            where cc=code 
  132.              and bc='b4' 
  133.              and qty > (select avg(qty)
  134.                                  from s));
  135.  
  136.          code            : c1
  137.          code            :   
  138.          rating          : 20
  139.          rating          : 21
  140.          [ 1 updated ]       
  141.          
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.                                    UPDATE-3
  179.  
  180.