home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / database / ssql31a.arj / INSTDOC.EXE / SSQL.DOC < prev   
Encoding:
Text File  |  1992-04-12  |  214.7 KB  |  5,757 lines

  1.  
  2.  
  3.  
  4.                                      SSQL
  5.                                   VERSION 3.1
  6.  
  7.  
  8.                     COPYRIGHT (C) 1987-1992 BY STEVE SILVA
  9.     
  10.                             SILVAWARE
  11.                             3902 NORTH 87TH STREET
  12.                             SCOTTSDALE, AZ 85251
  13.  
  14.  
  15.                      Special thanks to the hard-working 
  16.                      students in CIS425 at the DeVry 
  17.                      Institute of Technology, Phoenix, Arizona.
  18.  
  19.      *********************************************************************
  20.      *** THE LATEST VERSION IS AVAILABLE THROUGH:                      ***
  21.      *** The SilvaWare BBS (602) 945-6181 (Details on page INTRO-12)   ***
  22.      *** The SQLBBS   (312) 589-0508 (Details on page INTRO-13)        ***
  23.      *** CompuServe in the IBMAPPS forum under Databases               ***
  24.      *********************************************************************
  25.  
  26.      *********************************************************************
  27.      *** Do you have a question?  I enjoy hearing from you.  The       ***
  28.      *** fastest response is by contacting my BBS (see page INTRO-12)  ***
  29.      *** or on CompuServe (73177,2771). You can also contact me        ***
  30.      *** (Steve Silva) by mail.                                        ***
  31.      *********************************************************************
  32.  
  33.      *********************************************************************
  34.      *** IMPORTANT NOTE FOR PRINTERS!!!!                               ***
  35.      *** It is assumed that your printer can print the extended        ***
  36.      *** character set.  If it can't, the samples of windows and such  ***
  37.      *** won't look the way they should.                               ***
  38.      *********************************************************************
  39.  
  40.      *********************************************************************
  41.      *** FOR A COPY OF THE ORDER FORM, TYPE:                           ***
  42.      *** COPY ORDER.FOR PRN                                            ***
  43.      *********************************************************************
  44.  
  45.      *********************************************************************
  46.      *** >> UNIX VERSION NOW AVAILABLE!!!!! <<                         ***
  47.      *** See INTRO-15 for details.                                     ***
  48.      *********************************************************************
  49.  
  50.      *********************************************************************
  51.      *** If you have a PC XT or compatible, SSQL may run too slow. Use ***
  52.      *** SSQLENG directly.  See INTRO-4, WORD-1, SCRIPT-1 for info.    ***
  53.      *********************************************************************
  54.  
  55.      
  56.  
  57.  
  58.                                  TABLE OF CONTENTS
  59.          
  60.         OVERVIEW
  61.             Enhancements From Earlier Versions.................INTRO-1
  62.             The Cost of These Wonderful Things.................INTRO-2
  63.             The Future of SSQL.................................INTRO-3
  64.             The Components of SSQL.............................INTRO-4
  65.         Installation...........................................INTRO-5
  66.         Fine-Tuning............................................INTRO-5
  67.         Start Up Options.......................................INTRO-5
  68.         Quick Start............................................INTRO-6
  69.         The Basics of the SSQL Environment.....................INTRO-6
  70.         Differences In The Registered Version..................INTRO-7
  71.         Permission to copy ....................................INTRO-9
  72. ********LICENSE AGREEMENT - PLEASE READ .......................INTRO-9
  73.         SSQL Specifications....................................INTRO-10
  74.         Contents of Disk ......................................INTRO-10
  75.         dBase Compatibility ...................................INTRO-10
  76.         Key Words Needed to Understand the Documentation.......INTRO-11
  77.         Syntax Diagrams .......................................INTRO-12
  78.         SilvaWare BBS - latest version and support too.........INTRO-12
  79.         SSQLBBS - An excellent BBS in the Chicago area.........INTRO-13
  80.         UNIX VERSION AVAILABLE!!...............................INTRO-15
  81.         
  82.         The Text Editor........................................ENV-1
  83.                 Block Commands.................................ENV-1
  84.                 Cursor Movement Commands.......................ENV-1
  85.                 Insert & Delete Commands.......................ENV-1
  86.                 Miscellaneous Editor Commands..................ENV-2
  87.         File
  88.                 Open...........................................ENV-2
  89.                         The File Dialog Box....................ENV-2
  90.                 New............................................ENV-3
  91.                 Save...........................................ENV-3
  92.                 Save As........................................ENV-4
  93.                 Input..........................................ENV-4
  94.                 Change Dir.....................................ENV-4
  95.                 Print..........................................ENV-5
  96.                 DOS Shell......................................ENV-5
  97.                 Exit...........................................ENV-5
  98.         Edit
  99.                 Undo...........................................ENV-5
  100.                 Cut............................................ENV-6
  101.                 Copy...........................................ENV-6
  102.                 Paste..........................................ENV-6
  103.                 Show Clipboard.................................ENV-6
  104.                 Clear..........................................ENV-6
  105.         Search
  106.                 Find...........................................ENV-7
  107.                 Replace........................................ENV-7
  108.                 Again..........................................ENV-7
  109.  
  110.  
  111.  
  112.         Window
  113.                 Size/Move......................................ENV-7
  114.                 Zoom...........................................ENV-8
  115.                 Tile...........................................ENV-8
  116.                 Cascade........................................ENV-8
  117.                 Next...........................................ENV-9
  118.                 Previous.......................................ENV-9
  119.                 Close..........................................ENV-9
  120.         SQL
  121.                 Process........................................ENV-9
  122.                 Form...........................................ENV-10
  123.                 Report.........................................ENV-14
  124.  
  125.          THE EXAMPLE DATABASE .................................DATABASE-1
  126.          USING YOUR OWN WORD PROCESSOR FROM WITHIN SSQLENG.....WORD-1
  127.          USING SCRIPT FILES IN SSQL AND SSQLENG................SCRIPT-1
  128.          PUTTING COMMENTS IN SCRIPT FILES (OR ANY FILE)........SCRIPT-1
  129.          DOS SHELL FROM SSQLENG................................SCRIPT-1
  130.  
  131.          
  132.          EXTRACTING DATA FROM A SINGLE TABLE...................SELECT-1
  133.            Distinct ...........................................SELECT-2
  134.            Where ..............................................SELECT-3
  135.            search_expression ..................................SELECT-3
  136.            Special Search Expression - is null, is not null ...SELECT-5
  137.            Special Search Expression - like, not like .........SELECT-6
  138.            And, Or, Not .......................................SELECT-7
  139.            Any ................................................SELECT-11
  140.            In..................................................SELECT-12
  141.            All.................................................SELECT-12
  142.            Between constant AND constant.......................SELECT-13
  143.            Importance of the NULL (Unknown value)..............SELECT-13
  144.            Mathmatical Functions Avg, Min, Max, Sum, Count.....SELECT-15
  145.            Column Calculations (+, -, *, /) ...................SELECT-18
  146.            Group by, Having ...................................SELECT-19
  147.            Order by ...........................................SELECT-20
  148.            Redirectto .........................................SELECT-21
  149.       
  150.          JOINING...............................................JOIN-1
  151.          SIMPLE SUBQUERIES.....................................SUB-1
  152.          CORRELATED SUBQUERIES.................................COR-1
  153.          CONVERTING ENGLISH TO SQL.............................ENG-1
  154.          CREATE A TABLE........................................CREATE-1
  155.          CREATE A VIEW.........................................VIEW-1
  156.          DROP A TABLE/VIEW.....................................DROP-1
  157.          INSERT DATA INTO A TABLE..............................INSERT-1
  158.          UPDATE DATA IN A TABLE................................UPDATE-1
  159.          DELETE DATA FROM A TABLE..............................DELETE-1
  160.          TUTORIAL..............................................TUTORIAL-1
  161.  
  162.  
  163.  
  164.          
  165.      *** OVERVIEW ***
  166.      SSQL 3.1 is a FULLY-FUNCTIONAL, COMPLETE ANSI SQL implementation
  167.      with the exception of the security features (GRANT, REVOKE).  It
  168.      has everything else:
  169.  
  170.      CREATE                SELECT
  171.          table         any       not exists
  172.          view          all       group by
  173.          index         in        having
  174.      INSERT            like      order by
  175.          values        not       is null
  176.          w/ select     and       is not null
  177.      UPDATE            or        column calculations (+,-,/,*)
  178.      DELETE            exists    aggregate functions 
  179.      DROP                        (avg,min,max,sum, count)
  180.      
  181.      FULL SUPPORT FOR NULLS
  182.      UP TO 14 TABLES IN JOINS
  183.      UP TO 14 LEVELS OF SUBQUERIES
  184.      IT EVEN SUPPORTS CORRELATED SUBQUERIES!!!!!
  185.      
  186.      IT CAN DIRECTLY ACCESS ANY DBASE III-COMPATIBLE FILE (without memo
  187.      fields)
  188.      ALSO, ANY FILE CREATED THROUGH SSQL CAN BE ACCESSED BY A DBASE
  189.      III-COMPATIBLE PROGRAM!!
  190.  
  191.  
  192.      INDEXING THAT MAKES JOINS FASTER THAN DBASE OR ORACLE!!
  193.  
  194.      MANY ADD-ONS THAT MAKE IT EASIER TO WORK WITH:
  195.        Integrated Environment  with full text editor 
  196.        Form Generator (data entry/update/delete/view)
  197.        Report Generator    
  198.        Import/Export text files
  199.        Programmer's interface
  200.      
  201.      
  202.      *** ENHANCEMENTS (PROBLEMS CORRECTED) FROM PREVIOUS VERSIONS ***
  203.      I have been working on this product off-and-on for five years. 
  204.      User feedback has noted some areas that needed of improvement.
  205.      I have addressed the following problems in the current version: 
  206.  
  207.      *1.  SSQL 2.3 IS EXTREMELY USER-UNFRIENDLY.  DIFFICULT TO MAKE
  208.      CORRECTIONS.  DIFFICULT TO SEE, PRINT, AND SAVE RESULTS.
  209.  
  210.      SSQL has a fully-integrated multi-window text editor with full
  211.      mouse-support.  The query is entered in a text window on the left
  212.      half of the screen and the result is in a text window on the right
  213.      half of the screen.  Either window can be easily saved and/or
  214.      printed.
  215.      
  216.      *2.   SSQL 2.3 HAS NO ON-LINE HELP
  217.      Well, now it does.  There is full help for the SSQL environment but only
  218.      the registered users get the full SQL help.
  219.      
  220.  
  221.                                    INTRO-1
  222.  
  223.      *3.  THE MAJOR PROBLEM WITH SSQL 2.2 HAS BEEN THAT IT IS SLOOOOOOW WHEN
  224.      LARGE TABLES ARE JOINED.  
  225.  
  226.      Indexes are now available to increase processing speeds
  227.      dramatically.  Although the shareware version allows you to
  228.      create indexes, only the version with the index option will process
  229.      indexes.  However, the results are dramatic. I did the
  230.      following test on an IBM PC AT using the SSQLENG program directly. 
  231.      Results will be slower using the SSQL environment.
  232.  
  233.                Time In Seconds To Display Various Queries
  234.      
  235.      Number of Rows Joined    SSQLENG   Oracle 5.1    dBase IV 1.1
  236.      
  237.      100 X 100                    5.5           16              38
  238.      100 X 100 X 100             10.0           22              39
  239.      1000 X 1000 10 rows          3.5          123              24
  240.      1000 X 1000 All rows        45.0          157             108
  241.      
  242.      
  243.      
  244.      The dBase results could probably be made better by some
  245.      fine-tuning.  Also, the only version of Oracle available to me is
  246.      an old one.  I hope newer ones are faster.  My current version
  247.      implements only the mostcritical aspects of indexing.  Indexes are
  248.      used when testing for equality and when the tests are linked with
  249.      ANDs.   
  250.  
  251.      Without indexes, (i.e. the shareware version) SSQL would take MANY
  252.      hours to join two tables with 1000 rows each. 
  253.      I got tired trying to find out exactly how long. 
  254.      
  255.  
  256.      *4.  DATA ENTRY IS DIFFICULT AT BEST IN SSQL 2.2.
  257.      
  258.      I have included an easy-to-use program that generates data entry
  259.      screens called FORM.  The screens can be easily
  260.      modified by any text editor.  Besides the basic add, update, view,
  261.      and delete, you can create help screens, cross-
  262.      reference data in another table, test for ranges,  and test for a
  263.      certain set of data.  You can move back and forth from
  264.      entry to entry and there is full data editing capabilities.
  265.  
  266.  
  267.      *5.  REPORT FORMATTING IS PRIMITIVE IN SSQL 2.2.
  268.      
  269.      I have included a report generator that does all the basics -
  270.      subtotals, grand totals, page breaks, page numbering,
  271.      current date, headings, footers, calculations, etc.
  272.      
  273.      
  274.      
  275.  
  276.  
  277.  
  278.  
  279.  
  280.                                    INTRO-2
  281.      *6.  THERE IS NO PROGRAMMING INTERFACE IN SSQL 2.2
  282.  
  283.      Now you can include SSQL in your favorite programming language
  284.      (including batch files).  I have created an extensive command line
  285.      inteface that I use in the SSQL, SSQLFORM and SSQLRPT programs. 
  286.      The interface allows you to access everything that SSQL can do. 
  287.      You can call it from within C (spawn) or even a batch file. 
  288.      Although I haven't tried it, I assume it will work with Pascal and
  289. `    even some dialects of Basic.
  290.  
  291.      I also offer the Turbo C source code for FORM and REPORT to show
  292.      how the calls to SSQL can be integrated into a program.  I admit
  293.      that I am not real proud of the code I produced in version 1.0.  I
  294.      started off with the intention of offering the code so I tried to
  295.      write it in an easy-to-understand style.  Well, as often happens,
  296.      the problem was a bit more involved than I thought and there are a
  297.      few kludges here and there. Oh well, it may give you some ideas!!
  298.      
  299.      No, I do not offer the source code of SSQL.
  300.      
  301.      ** THE COST OF THESE WONDERFUL THINGS **
  302.  
  303.      I have enclosed an order form for your convenience.  It is in the
  304.      file ORDER.FOR. At the DOS prompt type: COPY ORDER.FOR PRN
  305.  
  306.      SSQL 3.1 -                               $35.00
  307.      
  308.      SSQL PRO 3.1 (The version with indexes)  $45.00
  309.  
  310.      SSQL PRO + source code for FORM & REPORT
  311.               + Programmer's Interface        $65.00
  312.  
  313.      Shipping is $3.00 USA (Priority Mail) , $7.00 foreign (airmail)   
  314.  
  315.      ***** FOREIGN CUSTOMERS *****
  316.      CHECKS SHOULD BE PAYABLE THROUGH A U.S. BANK.  INTERNATIONAL MONEY
  317.      ORDERS ARE GOOD. 
  318.      IF THIS IS IMPOSSIBLE, WRITE A LOCAL CHECK (in U.S. $) AND ADD $15
  319.      (MY COST) FOR PROCESSING A FOREIGN CHECK.  SO FAR, I HAVE HAD NO
  320.      PROBLEM WITH CASH BEING SENT THROUGH THE MAIL.
  321.      
  322.      *** THE FUTURE ***
  323.      I have been working on this product for five years with a major
  324.      upgrade at least every year.  Although I refuse to make any
  325.      promises,  this is what I am working on as of April, 1992:
  326.      
  327.  
  328.  
  329.  
  330.                                    INTRO-3
  331.  
  332.      *1.  SQL2.  There is a new SQL specification that is very complex.
  333.      I have tried working on it but I have found that I need help in
  334.      converting the VERY CRIPTIC specifications to algorithms.  One
  335.      person that was going to help me dropped out. If there is anyone
  336.      out there willing to help, I sure would appreciate it.  I need
  337.      someone who can take the specifications and convert it to a set of
  338.      test data along with the various operations and the results based
  339.      on the test data.  I realize it is a monumental task.  The first
  340.      thing I would like to work on is the catalog area because this is
  341.      what users have been requesting.  However, I would accept help in
  342.      any of the many areas in the new specification.  I would also like
  343.      to know if there are any good articles related to SQL2.
  344.  
  345.      *2.  Extensive on-line tutorial with integrated testing.
  346.  
  347.      *** THE COMPONENTS OF SSQL ***
  348.  
  349.      There are four components to the SSQL environment.  These four components
  350.      correspond to the four programs:
  351.      SSQL.EXE     - The user-friendly control program
  352.      SSQLENG.EXE  - The SQL engine that does the processing
  353.      SSQLFORM.EXE - A form generator that makes data entry easy
  354.      SSQLRPT.EXE  - A report generator to create formatted reports
  355.      
  356.      * SSQL.EXE *
  357.      This is the main program that you will typically use to enter your SQL
  358.      commands.  Pressing F4 will take the command and send it to SSQLENG.EXE
  359.      for processing and the answer will be displayed in an text window.  You
  360.      can also access the form and report generators.
  361.  
  362.      A complete help system is included, not only for the editor environment,
  363.      but also for SQL syntax and examples.
  364.  
  365.      You enter commands in a full-featured multi-window text editor.  The
  366.      editor uses the older WordStar(tm) style commands and the newer CUA(tm)-
  367.      compatible commands that are popular in Windows 3.0 programs.
  368.  
  369.      * SSQLENG.EXE *
  370.      This is the SQL database engine that does all the work.  This can be run
  371.      as an independent program.  When you run this, you simply type a command
  372.      and get a response. The command can be on multiple lines.
  373.  
  374.      It is useful when you want to avoid the overhead of
  375.      SSQL.EXE which limits the output to about 32,000 characters.  By using
  376.      SSQLENG.EXE directly, the limitation increases 10 times to about 320,000. 
  377.      The input file sizes in either case is unlimited.
  378.  
  379.      There are some non-standard utilities available when using SSQLENG.EXE
  380.      directly. See WORD-1 and SCRIPT-1.
  381.  
  382.      * SSQLFORM.EXE *
  383.      This the very basic forms generator for SSQL.  It allows data entry,
  384.      update, delete, view, and browsing on a row by row basis.  It can be run
  385.      from DOS by typing: SSQLFORM <filename>
  386.      where <filename> is a valid table name.  See details in the SSQLFORM
  387.      section.
  388.  
  389.  
  390.                                   INTRO-4
  391.  
  392.      * SSQLRPT.EXE *
  393.      This is a basic report generator which can be run by typing:
  394.      SSQLRPT <filename>
  395.      where <filename> is any valid report file.  See details in the REPORT
  396.      section.
  397.  
  398.      *** INSTALLATION AND FINE-TUNING SSQL ***
  399.  
  400.      The following must (normally) reside in the same directory:
  401.      SSQL.EXE
  402.      SSQLENG.EXE
  403.      SSQL.HLP
  404.      SSQLEDIT.HLP
  405.      SSQLENG.ERR
  406.      SSQLFORM.EXE
  407.      SSQLRPT.EXE
  408.      
  409.      You can set a PATH to the directory that contains the above files.
  410.      Your data files must reside in your default directory (the directory you
  411.      are in).  You can also put everything in the same directory.
  412.  
  413.      *** FINE-TUNING ***
  414.  
  415.      When you press F4 to process, SSQL calls the program called SSQLENG.EXE.
  416.      The result of the processing usually is written to QUERY.TXT.
  417.      Control is returned to SSQL and QUERY.TXT is read into an edit screen.
  418.      The speed of this can be greatly enhanced if SSQLENG.EXE is in a RAM
  419.      disk with at least 150K.  Assuming that your RAM disk is D:
  420.      1.   Copy SSQLENG.EXE to D:
  421.      2.   Set the environment variable SSQL to D: with the following command:
  422.           SET SSQL=D:\
  423.      Now when you run SSQL and press F4, SSQL will use D: to load SSQLENG,
  424.      and D: will be used for all temporary files.
  425.      
  426.      *** START UP OPTIONS ***
  427.  
  428.      SSQL /n 
  429.           Opens a new file (the default)
  430.      SSQL *.QRY 
  431.           Goes to Open File and displays all the *.QRY files
  432.      SSQL DEMO.QRY 
  433.           Opens DEMO.QRY in an edit window
  434.      SSQL /w 
  435.           Opens a new file that is as wide as the screen.  It can be used
  436.           with file names and wild cards.
  437.      SSQL /l
  438.           For monochrome LCD screens that do not have highlighting.  It may not
  439.           work on all computers, especially the original (pre-XT) PCs.  You can
  440.           tell it doesn't work by the fact that it locks up.  Use /m which is
  441.           similar.
  442.      SSQL /m
  443.           Regular monochrome displays
  444.  
  445.      SSQL /b
  446.           25-line mode.  This will override the automatic 50-line mode for VGA
  447.           screens.
  448.  
  449.                                   INTRO-5
  450.  
  451.      Any logical combination is OK such as:
  452.      SSQL /m /w *.QRY
  453.      
  454.  
  455.      *** QUICK START ***
  456.      
  457.      At DOS prompt type: SSQL  (and press ENTER)
  458.      Now you are in the SSQL environment.
  459.      Press ENTER at the Intro screen.
  460.      Type the command, e.g.
  461.       select *
  462.       from branch;
  463.  
  464.      Press F4 to process the command.
  465.      
  466.      The answer will appear in a edit window on the right.  You can now edit
  467.      it, print it, save it - anything you can do with a regular file.
  468.      Press F5 to make the query fill the screen.  You can now use the standard
  469.      edit keys to move around the response if it is larger than the screen.
  470.  
  471.      If you want a new query, press ALT-F3 to close the output window on the
  472.      right, otherwise the "desktop" gets cluttered and you will soon run out of
  473.      space.
  474.  
  475.      To erase the previous query quickly, press Shift-PgUp (assuming the cursor
  476.      is at the end of the query), and Ctrl-Del.
  477.  
  478.      The Shift-PgUp marks the entire query, and the Ctrl-Del erases the block.
  479.  
  480.      *** THE BASICS OF THE SSQL ENVIRONMENT ***
  481.  
  482.      When you first type SSQL, and press ENTER the following appears at the
  483.      top:
  484.      File  Edit  Search  Windows SQL
  485.  
  486.      and at the bottom:
  487.      F1 Help  Shift-F1 SQL Help  F3 Open  Alt-F3 Close  F4 Process  F5 Zoom
  488.  
  489.      Since you will usually be using SSQL for entering SQL queries, you will
  490.      probably have little need for the Edit, Search and Windows options. 
  491.      Most of what you need is along the bottom of the screen.  
  492.  
  493.      <F1> Virtually everything that follows is already in over 45 help screens
  494.      that you can access by pressing F1 while in SSQL.
  495.  
  496.      To exit help, press ESC.
  497.  
  498.      Help screens sometimes have highlighted words and phrases which correspond
  499.      to additional help screens.  You can use the TAB key to move to a
  500.      highligted area and press the ENTER key to go to that help screen.
  501.  
  502.      <Shift-F1> will get you SQL help only if you are using the registered
  503.      version.
  504.  
  505.  
  506.  
  507.  
  508.                                    INTRO-6
  509.      <F3> will take you to the File Open screen (see page ).  The default list
  510.      is *.QRY but you can change it to whatever you want.
  511.  
  512.      <ALT-F3> closes a file that is open.  It is important to close a file that
  513.      you are not going to use any more.  Every time you process a query, a
  514.      new file is opened. 
  515.  
  516.      <F4> processes a SQL statement in the active window (the window where
  517.      the cursor is).  If it is a query, the response will appear on the right
  518.      side of the screen. If it is another type of statement such as "create
  519.      table", a message box will appear telling you that the table has been
  520.      created.
  521.  
  522.      <F5> zooms in the active window (makes it fill the screen).  Pressing it
  523.      again makes it shrink back to its original size.  This is especially
  524.      useful if the response to your query is over 40 columns wide.  When the
  525.      response is displayed, press F5 to make it fill the screen.
  526.  
  527.  
  528.      *** DIFFERENCES IN THE REGISTERED VERSION ***
  529.          
  530.      ** PROGRAM
  531.          There is no difference in the registered version except that it has
  532.          full help for SQL syntax.  However, registered users can get a version
  533.         (for an extra $10) that processes indexes.  Indexes can make joins
  534.         (combining data from different files) 40 times or more faster. 
  535.  
  536.      ** DISK-BASED DOCUMENTATION (UNREGISTERED USERS)
  537.          In order to fit everything on a single disk, there is only 
  538.          enough documentation on some commands to explain the basics. 
  539.          However, it does show all the ANSI SQL commands so you can see 
  540.          for yourself that the commands actually work. You do not get 
  541.          documentation on most of the utilities that make working in 
  542.          this environment a bit easier and allow you, for example, to 
  543.          output any query to a dBase file which gives you the ability to 
  544.          customize reports. 
  545.  
  546.      ** FULL DOCUMENTATION (REGISTERED USERS)
  547.  
  548.          Actually, it is much more than just documentation.  There are
  549.          two manuals totaling over 450 pages.  It was produced with a
  550.          desk-top publishing package, printed on a laser printer, and bound.
  551.  
  552.          One manual is APPLYING SQL.  Besides explaining all
  553.          the details of syntax, it shows the common constructs used in 
  554.          SQL to make creating your own queries much easier.  Much of 
  555.          each chapter is centered around translating English into SQL 
  556.          and how to note peculiarities and ambiguities in English that 
  557.          can cause problems in writing queries.
  558.  
  559.          Support is offered through my BBS ((602)945-6181),by mail
  560.          or through CompuServe (73177,2771).
  561.  
  562.  
  563.                               INTRO-7
  564.  
  565.          One of the most complex (and useful) topics in SQL is the 
  566.          correlated subquery.  Most authors give you a page or two on 
  567.          the topic.  That is not nearly enough for you to be able to use 
  568.          it on your own.  My chapter on correlated subqueries is over 40 
  569.          pages.  One good gauge on the quality of a book on SQL is how 
  570.          well it explains the correlated subquery and how well it 
  571.          conveys the importance of the topic.  It is an essential part 
  572.          of SQL that opens up various categories of queries that cannot 
  573.          be accomplished any other way.
  574.          
  575.          The last chapter on queries is CONVERTING ENGLISH TO SQL.  It 
  576.          gives you over 50 pages on approaches to converting English to 
  577.          SQL and dozens of examples.  It explains how to break down a 
  578.          sentence in English that simplifies its conversion.  Many of 
  579.          the common words used in queries are analyzed.  As is often the 
  580.          case with English, the same word in different contexts can 
  581.          significantly change the meaning of the sentence and its 
  582.          conversion to SQL.  Much of the chapter explains how to convert 
  583.          sentences with negation which can be VERY tricky.  For 
  584.          example, let's anylze the following query:
  585.          
  586.          List the purchase dates when all of the following products were 
  587.          purchased: MW, NM, GC. 
  588.          
  589.          The query, although a bit complex when solving it SQL, is 
  590.          nonetheless rather straightforward.  When we negate the query 
  591.          it gets even more confusing (and realistic!!): 
  592.  
  593.          List the purchase dates when all of the following products were 
  594.          NOT purchased: MW, NM, GC. 
  595.          (if all the products were purchased, skip the date) 
  596.  
  597.          List the purchase dates when NONE of the following products were 
  598.          purchased: MW, NM, GC.
  599.          (if any of the products were purchased, skip the date)
  600.  
  601.          List the purchase dates when something other than the following 
  602.          products were purchased: MW, NM, GC. 
  603.  
  604.          Once you see the solutions to these queries you will better 
  605.          understand why SQL is "structured" and it should give you a 
  606.          more critical understanding of English queries.
  607.  
  608.          The full chapters on the joins and subqueries have many more 
  609.          examples and detailed explanations.
  610.  
  611.          The power of SQL can be TOTALLY lost if the user does not 
  612.          understand the basics of data normalization. Normalization 
  613.          involves the rules for creating tables.  If the tables are not 
  614.          organized correctly, SQL cannot be used to its full potential.  
  615.          It is important to note that the topic is discussed with the 
  616.          non-technical end-user in mind.  Since there have not been any 
  617.          widely available SQL programs, all the books on normalization 
  618.          tend to be very theoretical and academically oriented.  
  619.  
  620.  
  621.  
  622.                               INTRO-8
  623.  
  624.          You also get full information on utilities to delete columns from 
  625.          tables, modify column names, change the width of columns, create 
  626.          tables which are subsets of a current table, etc. 
  627.  
  628.          *** PERMISSION TO COPY ***
  629.  
  630.          Please copy this disk and give it to a friend (or anybody else).
  631.          However, the following restrictions apply:
  632.          1.  No changes can be made to the distribution disk, including the 
  633.          documentation.
  634.          2. You cannot copy or reproduce the printed manual.
  635.  
  636.          *** LICENSE AGREEMENT ***
  637.          You are given a 60-day license to use SSQL.  Anything beyond that
  638.          is as illegal and unethical as using any commercial software.
  639.  
  640.          SSQL is shareware, also referred to as user-supported software.
  641.          Try it.  If you like it and use it, register it.  It is well-worth
  642.          the money.  If if doesn't suit your needs, you are not out a lot
  643.          of money.
  644.  
  645.         Shareware is a marketing concept to keep marketing costs down.
  646.         The savings is passed on to the user.   Please help support it.
  647.  
  648.          CLASSROOM USE.  If SSQL is used in a class, each student must be
  649.          a registered user.  Contact SilvaWare for significant volume
  650.          discounts. (HEY! It may be a good time discuss ethics with your
  651.          students!)
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                               INTRO-9
  658.  
  659.  
  660.  
  661.         *** SSQL SPECIFICATIONS ***
  662.  
  663.         Requires only one floppy diskette 
  664.         Minimum Memory 640K,  need 510K usable memory for SSQL if you want to
  665.         use FORM and REPORT
  666.         Maximum number of columns 128
  667.         Maximum length of a row 500 bytes (characters)
  668.         Maximum number of rows 
  669.         It is limited by the overall size of the output.  Under SSQL, the limit
  670.         is about 32K.  If you use SSQLENG directly, the limit is about 320K.
  671.         The tables on the disk can be larger, you just could not display all
  672.         the rows and columns at once.  Also, you could join multiple large
  673.         tables as long as the resultant table was not over 320K. This means
  674.         that the output of a table with rows 100 characters wide could contain
  675.         about 4,000 rows (about 70 pages of report).
  676.         
  677.         Maximum levels of subqueries 14 (reduced by # of indexes in use)
  678.         Maximum number of tables you can join 14 (reduced by # of indexes)
  679.         
  680.         ** CONTENTS OF DISK ***
  681.         READ.ME       - Last minute information
  682.         SSQL.EXE      - The integrated environment
  683.         SSQLENG.EXE   - The database engine
  684.         SSQLFORM.EXE  - The FORM Generator (data entry)
  685.         SSQLRPT.EXE   - The REPORT Generator
  686.         SSQLENG.ERR   - Contains error messages
  687.         EDIT.FIL      - The text file with the location of your word processor
  688.                       - (for use with SSQLENG only!)
  689.         BRANCH.DBF, PROD.DBF, EMP.DBF, MANU.DBF, SALES.DBF, CUST.DBF,
  690.         BRANCH.SQD, PROD.SQD, EMP.SQD, MANU.SQD, SALES.SQD, CUST.SQD,
  691.                       - The tables used in the documentation
  692.         SSQL.DOC      - Documentation
  693.         ORDER.FOR     - Order form. type: COPY ORDER.FOR PRN 
  694.                         for a printout
  695.         PRINTDOC.BAT  - The batch file to print the documentation
  696.         RPT1.FRM TO RPT7.FRM - Report formats for the REPORT program
  697.  
  698.          *** DBASE COMPATIBILITY ***
  699.  
  700.          SSQL creates dBase III compatible files and it can read any 
  701.          dBase file with the following exceptions:
  702.                 No memo data type
  703.                 No field length over 132
  704.  
  705.          A significant difference between SQL and dBase is the ability 
  706.          of SQL to store a null (see SELECT-13 for more information).  
  707.          Even dBase IV cannot store a null.  The way I have done it is 
  708.          that character columns are filled with blanks, and numeric 
  709.          columns contain -0 (negative zero).  You will never see a -0 
  710.          when using SSQL, the column value will be blank.  This 
  711.          information might be useful if you manipulate files created by 
  712.          SSQL in dBase.
  713.  
  714.                               INTRO-10
  715.  
  716.  
  717.          You will notice that every .DBF file created by SSQL has a
  718.          corresponding .SQD file.  This stores information specific to 
  719.          file creation - Not Null and Unique (see CREATE-2 for more 
  720.          information).  If you access a dBase file not 
  721.          created through SSQL, the .DBF file is created with Not Null set 
  722.          for every column and no uniqueness constraints. 
  723.  
  724.          If you change the number of fields in a dBase file after it has 
  725.          been accessed by SSQL, you should delete the corresponding .SQD 
  726.          file. Otherwise, the Not Null and Uniqueness constraints will 
  727.          not be correct.  
  728.  
  729.  
  730.          *** KEY WORDS NEEDED TO UNDERSTAND THE DOCUMENTATION ***
  731.  
  732.          * SQL - Structured Query Language. A standard method of 
  733.          interacting with a database.  It is pronounced "SEQUEL"!!  
  734.          However,  over the years I have noticed more and more people 
  735.          saying the letters S-Q-L.
  736.          
  737.          * TABLE - A table is typically known as a FILE in other systems.
  738.          You may ask why they don't just call a table a file.  It is 
  739.          because that although normally a table does refer to a specific 
  740.          file, a table can refer to something that spans two or more 
  741.          files.  This can be done by "creating a view" (see 
  742.          documentation). If you read a book on relational databases, they 
  743.          will probably refer to a table as a relation. 
  744.  
  745.          * ROW - A row corresponds to a record or a portion of a record in 
  746.          a file.  In relational theory it is called a tuple.
  747.  
  748.          * COLUMN - A column is typically known as a field in other 
  749.          systems.  In relational theory it is called an attribute.
  750.  
  751.          The above names were created to give relational databases a 
  752.          consistent and accurate view of data.
  753.  
  754.          EXAMPLE:
  755.          You may have a TABLE named sales which contains COLUMNs called 
  756.          date, custnum, partnum and quantity.  Every time you made a 
  757.          sale, you would add a ROW of data to the TABLE.
  758.  
  759.                       COLUMNS
  760.            -----------------------------
  761.           |         |        |         |
  762.          date    custnum  partnum  quantity
  763.          ------  -------  -------  --------
  764.          880201  8524     AD873         928       <-- ROW
  765.          880203  7687     VF8709         87       <-- ROW
  766.          ----------------------------------
  767.                         ^
  768.                         |
  769.                       TABLE 
  770.  
  771.  
  772.  
  773.                               INTRO-11
  774.  
  775.  
  776.         *** SYNTAX DIAGRAMS ***
  777.         Many chapters begin with a syntax diagram that concisely
  778.         describes a command and its options.  It takes some practice to
  779.         read them but there are only four basic components:
  780.  
  781.         lowercase_letters - they describe something that you must supply. 
  782.         The rules for correctly writing this part of the command follow
  783.         in the documentation.  It may represent a single word, such as
  784.         the syntax for table_name or a rather complicated series of words
  785.         that is needed for a column_definition.
  786.  
  787.         [  ] - items between square brackets are optional.
  788.  
  789.         ...   -  three dots mean previous item may be repeated.
  790.  
  791.         UPPER CASE LETTERS and all other symbols such as '(', ')', ';',
  792.         '.', and ','  - these must be typed as shown.  Although when
  793.         typing the command, you can use lower case instead of upper case
  794.         letters.  For example, refer to the syntax diagram for creating 
  795.         tables.
  796.         
  797.         Since CREATE TABLE is in uppercase, it must
  798.         always be typed as shown.  The items in lowercase such as
  799.         table_name, column_definition, and uniqueness_constraint must be
  800.         replaced by information you supply.  The rules for forming the
  801.         phrases required by the words in lowercase will be contained in
  802.         the chapter.  The item called column_definition is repeated.  The
  803.         first occurance is outside of the square brackets which means
  804.         that it is required.  The second occurance of column_definition
  805.         is within square brackets which means that it is optional.  There
  806.         is a comma after the left square bracket which means that if you
  807.         add another column_definition, there must be a comma before it. 
  808.         There are three dots after the second occurance of
  809.         column_definition which means that column_definitions can be more
  810.         than two column_definitions.  Other symbols, such as '(', ')' and
  811.         ';' must be included at the positions shown in the example.  In
  812.          SQL, spacing and lines make no difference.  You can create a
  813.         table with all the components on a single line or put each word
  814.         on a separate line.
  815.  
  816.      *** The SilvaWare BBS - LATEST VERSION AND SUPPORT TOO
  817.      Currently, it will support 1200/2400 bps but by the end of the year I
  818.      should have support for 9600 and 14400.  The phone number is
  819.      (602) 945-6181. Use the standard settings of 8-N-1.
  820.      The intro screen of my BBS displays the current version of SSQL so you
  821.      don't even have to go to the File section.  If you have a question,
  822.      just leave a message for the sysop.
  823.                          
  824.  
  825.  
  826.  
  827.                               INTRO-12
  828.  
  829.  
  830.      SIGN-ON PROCEDURES
  831.      If you just want to download something, use the name SSQL USER and the
  832.      password is SSQL.
  833.      If you have a question, use your own name when you sign-on and it will
  834.      ask you a few more questions like where you are from and what you want
  835.      your password to be.
  836.  
  837.      TO DOWNLOAD A COPY
  838.      Note the exact name of the file on the intro screen.  Once you have
  839.      gotten through the sign-on procedures:
  840.      Press F to get to the FILES section
  841.      Press D to download the file
  842.      Type in the name of the file and press ENTER
  843.      (It will ask for another name - just press ENTER)
  844.  
  845.      What you do next is up to your communications package.  I assume that
  846.      you know something about uploading and downloading.  If you don't,
  847.      practice on a local BBS first.  The first file you need to download is
  848.      PKUNZIP because all files on my BBS (and most others) are "ZIPPED"
  849.      to organize files, save space, and save time in downloading.
  850.           I hope to make my BBS a meeting place for people interested in SQL.
  851.      If you have any interesting information about SQL, feel free to upload
  852.      it.
  853.  
  854.      *** SQLBBS - ANOTHER PLACE TO GET THE LATEST VERSION ***
  855.           The SQLBBS is a BBS in the Chicago area. It is free to everyone.  You
  856.      can download whatever you want on the first call.  It even allows you to
  857.      use Oracle (the most popular SQL DBMS) on-line.  The BBS phone number is:
  858.      (312) 589-0508
  859.      Currently supported baud rates are 9600/2400/1200/300
  860.      The following is the fastest way to find and download the most current
  861.      version of SSQL.  It assumes that you are somewhat familiar with BBSs.
  862.  
  863.      My comment are in the blocks surrounded by asterisks, the other information
  864.      is what you see on the screen.
  865.      **********************************************************************
  866.      * WHEN YOU FIRST LOG-IN TO THE BBS YOU WILL SEE THE FOLLOWING MAIN   *
  867.      * MENU.  PRESS 'F' TO GET TO THE FILE MENU                           *
  868.      **********************************************************************
  869.                   ------*>>>   RBBS-PC  MAIN MENU   <<<*------
  870.      ----- MAIL ---------- SYSTEM ---------- UTILITIES ------ ELSEWHERE ---
  871.       [E]nter Messages  [*]nswer Questions [H]elp (or ?)       [D]oors
  872.       [K]ill Messages   [B]ulletins        [*]oin Conferences  [F]iles
  873.       [P]ersonal Mail   [C]omment to Sysop [*]iew Conf. Mail   [G]oodbye
  874.       [R]ead Messages   [I]nitial Welcome  [X]pert on/off      [Q]uit
  875.       [S]can Messages   [O]perator Page                        [U]tilities
  876.       [T]opic of Msgs   [*]ho's on          * = unavailable    [*]Library
  877.      ----------------------------------------------------------------------
  878.        Current time: 07:54 AM  Minutes remaining: 42     Security: 5
  879.      ----------------------------------------------------------------------
  880.      MAIN command <?,B,C,D,E,F,H,I,K,O,P,Q,R,S,T,U,X>! F
  881.  
  882.  
  883.  
  884.  
  885.                               INTRO-13
  886.  
  887.  
  888.      **********************************************************************
  889.      * AFTER PRESSING 'F', YOU WILL BE AT THE FILE MENU.                  *
  890.      * PRESS 'S' TO SEARCH FOR A FILE                                     *
  891.      **********************************************************************
  892.  
  893.            ------*>>>   RBBS-PC  FILE MENU   <<<*------
  894.      -- TRANSFER ------ INFORMATION --- UTILITIES --- ELSEWHERE -
  895.       [D]ownload file   [L]ist files   [H]elp (or ?)   [G]oodbye
  896.       [*]ersonal dwnld  [N]ew files    [X]pert on/off  [Q]uit
  897.       [U]pload file     [S]earch files
  898.                         [V]iew archives      * = unavailable
  899.      ------------------------------------------------------------
  900.            Current time: 07:54 AM     Minutes remaining: 42
  901.      ------------------------------------------------------------
  902.  
  903.      FILE command <?,D,G,H,L,N,Q,S,U,V,X>! S
  904.  
  905.      **********************************************************************
  906.      * WHEN IT ASKS WHAT TO SEARCH FOR, TYPE                              *
  907.      * SSQL                                                               *
  908.      * IMPORTANT!!!! MAKE SURE THAT SSQL IS IN UPPER-CASE!!!!!            *
  909.      **********************************************************************
  910.  
  911.      Search for (in file name/desc, wildcards name only, [ENTER] quits)? SSQL
  912.  
  913.      **********************************************************************
  914.      * THEN IT WILL ASK YOU WHICH DIRECTORIES                             *
  915.      * PRESS 'A' FOR ALL DIRECTORIES                                      *
  916.      **********************************************************************
  917.  
  918.      * Ctrl-K(^K) / ^X aborts. ^S suspends ^Q resumes *
  919.      UPLD
  920.      DOWNLD
  921.      What directory(s) (U)pload,A)ll,L)ist,E)xtended +/-, [Q]uit)? A
  922.      Scanning Directory DOWNLD for SSQL
  923.  
  924.      **********************************************************************
  925.      THE FOLLOWING LINE WILL GIVE YOU THE CURRENT VERSION.                *
  926.      IF IT IS HIGHER THAN SSQL31, YOU MAY WANT TO DOWNLOAD IT.            *
  927.      **********************************************************************
  928.  
  929.      SSQL31   ZIP   (THE REST IS DATE AND SIZE INFORMATION)
  930.      End list.  R)elist, [Q]uit, or download what?
  931.  
  932.      **********************************************************************
  933.      * IF YOU WANT TO DOWNLOAD IT, JUST TYPE THE NAME OF THE FILE         *
  934.      * (IN THIS CASE SSQL31.ZIP), AND FOLLOW THE INSTRUCTIONS             *
  935.      **********************************************************************
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.                               INTRO-14
  943.  
  944.  
  945.  
  946.      *** UNIX VERSION NOW AVAILABLE ***
  947.  
  948.      I have ported the SSQLENG portion of the package to SCO Unix.  SSQLENG
  949.      is the SQL engine and can be used directly from DOS.  See INTRO-4,
  950.      WORD-1, and SCRIPT-1 for details.  It works exactly the way it does in
  951.      DOS.  It handles multi-user problems correctly too.  If it is run with
  952.      tables that are read-only, you can use the SELECT statement, but
  953.      INSERT, UPDATE, and DELETE will return an error message.  You can only
  954.      use CREATE in directories where you have access, otherwise it will
  955.      give you an error message.
  956.  
  957.      You can get the shareware version for free by accessing the SSQL BBS
  958.      and downloading it from the SCO Unix file area.  You can also send me
  959.      $10 and I will send it to you on a 3 1/2" or 5 1/4" DOS disk.
  960.  
  961.      DIFFERENCES BETWEEN THE SHAREWARE VERSION AND THE REGISTERED VERSION
  962.      1. The shareware version has an obnoxious intro screen that begs for
  963.      money.
  964.      2. The shareware version comes with a 60-day license.  Naturally, there
  965.      is no limit on the registered version.
  966.      3. The registered version allows the use of indexing which speeds up
  967.      joins and subqueries many, many times.
  968.      4. The registered version has on-line help.
  969.      5. You get a bound, laser-printed manual of over 400 pages which is
  970.      virtually the same as the one you get with a DOS registration.
  971.      6. Registered users can purchase extra copies of the manual for $15.
  972.  
  973.      COST OF THE REGISTERED VERSION FOR SCO UNIX
  974.      Only $100 per CPU plus $3 shipping in U.S. (priority mail), or
  975.      $7 foreign (airmail).
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.                               INTRO-15
  1001.  
  1002.  
  1003.  
  1004.      *** THE TEXT EDITOR ***
  1005.  
  1006.  
  1007.      The commands for the file editor are standard to most editors.
  1008.      You can use the ancient WordStar(tm) commands, or the more modern
  1009.      CUA(tm) commands used on products such as Borland languages and
  1010.      WordPerfect for Windows.  Actually it is not 100% CUA but it comes close. 
  1011.      Some CUA cominations will not work on the original (pre-XT) PCs and
  1012.      compatibles.
  1013.  
  1014.      ** Block Commands 
  1015.      
  1016.      * Mark A Block 
  1017.      Move the cursor to one end of the block.  While holding down the shift key
  1018.      use the arrow keys to move to the other end of the block.  Now it is
  1019.      marked.
  1020.       Or...
  1021.       Begin mark Ctrl-K B
  1022.       End mark   Ctrl-K K
  1023.       Mark word  Ctrl K T
  1024.      Once a block is marked, you can use the following:
  1025.       Copy    Ctrl-K C  
  1026.       Move    Ctrl-K V 
  1027.       Delete  Ctrl-K Y or Ctrl-Del
  1028.  
  1029.      Or you can use the edit commands:
  1030.       Ctrl-Ins   Copy block to clipboard
  1031.       Shift-Del  Cut block and put in clipboard
  1032.       Shift-Ins  Paste block from clipboard
  1033.      
  1034.  
  1035.       ** Cursor Movement Commands 
  1036.                                                       
  1037.        Character left  |  Left arrow or Ctrl-S
  1038.        Character right |  Right arrow or Ctrl-D
  1039.        Word left       |  Ctrl-Left arrow or Ctrl-A
  1040.        Word right      |  Ctrl-Right arrow or Ctrl-F
  1041.        Line up         |  Up arrow or Ctrl-E
  1042.        Line down       |  Down arrow or Ctrl-X
  1043.        Page up         |  PgUp or Ctrl-R
  1044.        Page down       |  PgDn or Ctrl-C
  1045.                   
  1046.  
  1047.        ** Insert & Delete Commands
  1048.  
  1049.        Insert mode on/off     | Ins or Ctrl-V
  1050.        Delete line            | Ctrl-Y
  1051.        Delete to end of line  | Ctrl-Q Y
  1052.        Delete character left  | Backspace or Ctrl-H
  1053.        Delete character       | Del or Ctrl-G
  1054.        Delete word right      | Ctrl-T
  1055.      
  1056.                                          ENV-1
  1057.  
  1058.  
  1059.  
  1060.  
  1061.       * Miscellaneous Editor Commands
  1062.  
  1063.        Menu bar                 | F10
  1064.        Save and Edit            | F2
  1065.        New File                 | F3
  1066.        Close Active Window      | ALT-F3
  1067.        Find                     | Ctrl-Q F
  1068.        Find and Replace         | Ctrl-Q A
  1069.        Repeat last Find/Replace | Ctrl-L
  1070.        Cancel Operation         | Esc
  1071.  
  1072.      ** File  (Alt-F) 
  1073.  
  1074.      The File menu offers choices for opening files, changing
  1075.      directories, and exiting the program.
  1076.  
  1077.      * File-Open  (F3) 
  1078.  
  1079.      The Open command displays the Open a File dialog box. In this dialog box
  1080.      you select the text file you want edit.
  1081.  
  1082.      The Open a File dialog box contains:
  1083.       The File Dialog Box 
  1084.  
  1085.      The File dialog box contains an input box, a file list,
  1086.      a file information panel, the standard button Cancel,
  1087.      one other action button (Open if you selected File
  1088.      Open or OK if you selected Save As...), plus a history
  1089.      list that's attached to the Name Input Box.
  1090.       Name Input Box
  1091.       ┌─ Name ──────────────────────────┐
  1092.       │                                 │
  1093.       └─────────────────────────────────┘
  1094.      The Name input box is where you enter the name of the file to load,
  1095.      or the file-name mask to use as a filter for the Files list box
  1096.      (for example, *.QRY).
  1097.  
  1098.                                          ENV-2
  1099.  
  1100.  
  1101.  
  1102.       ┌ Files ──────────────────────────┐
  1103.       │                                 │
  1104.       │  FILENM01.QRY  │  FILENM09.QRY  │
  1105.       │  FILENM02.QRY  │  FILENM10.QRY  │
  1106.       │  FILENM03.QRY  │  FILENM11.QRY  │
  1107.       │  FILENM04.QRY  │  FILENM12.QRY  │
  1108.       │  FILENM05.QRY  │  ..            │
  1109.       │  FILENM06.QRY  │  \MOREXAMP     │
  1110.       │  FILENM07.QRY  │  \TOURS        │
  1111.       │  FILENM08.QRY  │  \ANSWERS.DIR  │
  1112.       │  ▒▒▒■▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒   │
  1113.       └─────────────────────────────────┘
  1114.      The Files list box lists the names of files in the current
  1115.      directory that match the file-name mask in the Name input box,
  1116.      plus the parent directory and all subdirectories.
  1117.  
  1118.       ┌──────────────────────────────────────────┐
  1119.       │ C:\TP\EXAMPLES\*.QRY                     │
  1120.       │ HELLO.QRY       52  Jul 14,1990  12:00pm │
  1121.       └──────────────────────────────────────────┘
  1122.      The File information panel shows the path name, file name, date, time,
  1123.      and size of the selected file.
  1124.  
  1125.      None of the items on this information panel are selectable. 
  1126.       ┌──────────┐
  1127.       │ [ Open ] │
  1128.       └──────────┘
  1129.      The Open button opens a new Edit window and places the selected file in
  1130.      that window.
  1131.  
  1132.       ┌────────────┐
  1133.       │ [ Cancel ] │
  1134.       └────────────┘
  1135.  
  1136.      If you choose Cancel, nothing changes and no action occurs, and the
  1137.      dialog box is put away.
  1138.  
  1139.      (Esc always cancels a dialog box, even if a Cancel button does not
  1140.      appear.)
  1141.      
  1142.      * File-New
  1143.  
  1144.      File New opens up a edit box to start entering new information.
  1145.      When you exit, it will ask whether you want to save it to a file.
  1146.  
  1147.      * File-Save  (F2)
  1148.  
  1149.      Save the current file.  If it is untitled (you selected
  1150.      File-New), you will be asked for a name.
  1151.  
  1152.                                          ENV-3
  1153.  
  1154.  
  1155.  
  1156.      * File-Save As... 
  1157.  
  1158.      Use the Save As... selection when you want to name an untitled file
  1159.      (you selected File|New), or you want to save a file under a different
  1160.      name.
  1161.      The original file will be left unchanged.
  1162.      The  command displays the Save a File dialog box. In this dialog box you
  1163.      can select the file name you want to save it under or enter a new name.
  1164.  
  1165.      For a description of the dialog box, see File Open above.
  1166.  
  1167.      * File-Change Dir...
  1168.  
  1169.      File-Change Dir brings up the Change Directory
  1170.      dialog box, in which you can change the current directory.
  1171.  
  1172.      The Change Directory dialog box consists of an input box, a list box,
  1173.      the standard OK and Help buttons, and two other buttons (Chdir and
  1174.      Revert).
  1175.  
  1176.       ┌ Directory Name ─────────────────────┐
  1177.       │                                     │
  1178.       └─────────────────────────────────────┘
  1179.      The Directory Name input box is where you type in the path of the new
  1180.      directory.
  1181.  
  1182.       ┌ Directory Tree ─────────────────────┐
  1183.       │   Drives                            │
  1184.       │   └─┬C:\                            │
  1185.       │     └─┬SSQL                         │
  1186.       │       └──TABLES                     │
  1187.       └─────────────────────────────────────┘
  1188.  
  1189.                                     ENV-4
  1190.  
  1191.  
  1192.  
  1193.      The Directory Tree list box enables you to navigate directories by using
  1194.      the selecting bar and pressing Enter.
  1195.  
  1196.      If you're using the keyboard, press Enter to make the selected directory
  1197.      be the current directory, then choose OK or press Esc to exit the dialog
  1198.      box.
  1199.  
  1200.       ┌──────────┐
  1201.       │ [Chdir ] │
  1202.       └──────────┘
  1203.      The Chdir button changes the current directory once you've
  1204.      selected or typed in a directory name.
  1205.  
  1206.       ┌──────────┐
  1207.       │ [Revert] │
  1208.       └──────────┘
  1209.      The Revert button goes back to the previous directory, as long as you
  1210.      haven't yet exited the dialog box.
  1211.  
  1212.      * File-Print
  1213.  
  1214.      Prints whatever is in the current window and then advances to the top of
  1215.      the next page.
  1216.  
  1217.      * File-DOS Shell
  1218.  
  1219.      With the DOS Shell command, you can leave the program temporarily to
  1220.      perform a DOS command or run another program.
  1221.  
  1222.      To return to this program, type EXIT at the DOS prompt.
  1223.  
  1224.      * File-Exit    (Alt-X)
  1225.  
  1226.      The Exit command terminates this program.
  1227.  
  1228.      ** Edit (ALT-E)
  1229.  
  1230.      The Edit menu conatins commands to undo the previous edit
  1231.      operation, cut a block of text and put it in the
  1232.      clipboard, copy a block of text into the clipboard,
  1233.      paste the most recent block put in the clipboard, show
  1234.      the clipboard which contains ALL previous blocks put
  1235.      in the clipboard, and clear which clears the block from
  1236.      the screen and does not put it in the clipboard.
  1237.  
  1238.      * Edit-Undo
  1239.  
  1240.      The Undo command takes back the last editing command you
  1241.      performed on a line.
  1242.  
  1243.                                     ENV-5
  1244.  
  1245.  
  1246.  
  1247.      * Edit-Cut (Shift-Del)
  1248.  
  1249.      The Cut command removes the blocked text form your document and puts the
  1250.      text in the clipboard.
  1251.  
  1252.      You can then move to another location in your document and choose paste
  1253.      to take the cut text out of the clipboard and put it in your document.
  1254.  
  1255.      The text stays in your clipboard so you can paste it more than once.
  1256.  
  1257.  
  1258.      * Edit-Copy (Ctrl-Ins) 
  1259.  
  1260.      The Copy command leaves the selected text intact and places a copy of it
  1261.      in the clipboard.
  1262.  
  1263.      You can then move to another location in your document and choose paste
  1264.      to take the cut text out of the clipboard and put it in your document.
  1265.  
  1266.      The text stays in your clipboard so you can paste it more than once.
  1267.  
  1268.      * Edit-Paste (Shift-Ins)
  1269.  
  1270.      The paste command copies the selected text from the clipboard into the
  1271.      document at the cursor.
  1272.  
  1273.      * Edit-Show Clipboard
  1274.  
  1275.      The Show Clipboard command opens the clipboard window which stores the
  1276.      text you cut and copied from other windows.
  1277.  
  1278.      The currently selected (highlighted) text is the text SSQL uses when
  1279.      you choose paste:.
  1280.  
  1281.      * Edit-Clear (Ctrl-Del)
  1282.  
  1283.      The clear command removes the selected text but does not put it into the
  1284.      clipboard.
  1285.  
  1286.      The cleared text is retrievable with the undo command.
  1287.  
  1288.      ** Search
  1289.  
  1290.      The Search menu contains command to find text in the current file, replace
  1291.      existing text with new text, and search again for text to find or replace.
  1292.  
  1293.                                     ENV-6
  1294.  
  1295.  
  1296.  
  1297.      * Search-Find (Ctrl-Q F) 
  1298.  
  1299.      The find command displays a dialog box where you enter the text that you
  1300.      want to find.  It starts searching where the cursor is to the bottom of
  1301.      the file.  If you want to search for the same text again, press Ctrl-L
  1302.      or select Search Again
  1303.  
  1304.      * Search-Replace (Ctrl-Q A)
  1305.  
  1306.      The Replace command combines the Find command with the ability to replace
  1307.      the text it finds with new text.  You will be asked to enter the text
  1308.      to find.  Remember to press the Tab to get to the entry for the
  1309.      replacement text.  Use the SearchAgain option to find and replace the next
  1310.      occurence of text.
  1311.  
  1312.      * Search-Again (Ctrl-L)
  1313.  
  1314.      The Search Again command repeats the last Find or Replace.
  1315.  
  1316.      ** Window  (Alt-W)
  1317.  
  1318.      The Window menu contains commands to close, move and perform other
  1319.      window-management commands.
  1320.  
  1321.      Most of the windows in this program have all the standard window
  1322.      elements, including scroll bars, a close box, and zoom icons. 
  1323.      For more about windows and their elements, refer to windows. 
  1324.  
  1325.      * Window-Size/Move (Ctrl-F5)
  1326.  
  1327.      Choose this command to change the size or position of the active window.
  1328.  
  1329.  
  1330.      * Size
  1331.  
  1332.      If you press Shift while you use the arrow keys, you can change the size
  1333.      of the active window. Once you've adjusted its size or position, press
  1334.      Enter.
  1335.  
  1336.      If a window has a Resize corner, you can drag that corner to resize the
  1337.      window.
  1338.  
  1339.  
  1340.      * Move
  1341.      
  1342.      When you choose Window│Size/Move, the active window moves in response to
  1343.      the arrow keys. Once you've moved the window to where you want it, press
  1344.      Enter.
  1345.  
  1346.                                     ENV-7
  1347.  
  1348.  
  1349.  
  1350.      You can also move a window by dragging its title bar with the mouse.
  1351.  
  1352.      * Window-Zoom (F5)
  1353.      
  1354.      Choose Zoom to resize the active window to the maximum size. If the
  1355.      window is already zoomed, you can choose this command to restore it to
  1356.      its previous size.
  1357.  
  1358.      You can also double-click anywhere on the window's title bar (except
  1359.      where an icon appears) to zoom or unzoom the window. 
  1360.  
  1361.      * Window-Tile
  1362.  
  1363.      Choose Window-Tile to tile all file viewers on the desktop.
  1364.  
  1365.       ┌────────────────────────────────────┐
  1366.       │ ╔════════════1══╗┌────────────2──┐ │
  1367.       │ ║               ║│               │ │
  1368.       │ ║               ║│               │ │
  1369.       │ ║               ║│               │ │
  1370.       │ ║               ║│               │ │
  1371.       │ ╚═══════════════╝└───────────────┘ │
  1372.       │ ┌────────────3──┐┌────────────4──┐ │
  1373.       │ │               ││               │ │
  1374.       │ │               ││               │ │
  1375.       │ │               ││               │ │
  1376.       │ │               ││               │ │
  1377.       │ └───────────────┘└───────────────┘ │
  1378.       └────────────────────────────────────┘
  1379.                   Tiled Windows
  1380.  
  1381.      * Window-Cascade 
  1382.  
  1383.      Choose Window-Cascade to stack all file viewers on the desktop. 
  1384.       ┌──────────────────────────1─┐
  1385.       │┌─────────────────────────2─┐
  1386.       ││┌────────────────────────3─┐
  1387.       │││╔═══════════════════════4═╗
  1388.       │││║                         ║
  1389.       │││║                         ║
  1390.       │││║                         ║
  1391.       │││║                         ║
  1392.       │││║                         ║
  1393.       └└└╚═════════════════════════╝
  1394.              Cascaded Windows
  1395.  
  1396.  
  1397.                                     ENV-8
  1398.  
  1399.  
  1400.      * WindowNext 
  1401.  
  1402.      Choose Next to cycle forwards through the windows on the desktop. 
  1403.  
  1404.      * Window-Previous
  1405.  
  1406.      Choose Previous to cycle backwards through the windows on the desktop.
  1407.  
  1408.      * Window-Close  (Alt-F3)
  1409.  
  1410.      Choose Close to close the active window.
  1411.  
  1412.      You can also click the Close box in the upper right corner to close a
  1413.      window.
  1414.  
  1415.  
  1416.      ** SQL
  1417.  
  1418.      * File-Process (F4)
  1419.  
  1420.      The Process command takes the text that is displayed and sends it to the
  1421.      SSQL engine to be processed.  If it is a query, the response will be in a
  1422.      text file called QUERY.TXT that is opened to the right of the query.
  1423.      Other commands will have a response in a box to describe what occurred.
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.                                     ENV-9
  1458.  
  1459.          
  1460.          
  1461.      *** FORM ***
  1462.          The Form program allows you to insert, update, view and delete 
  1463.          in a much more user-friendly environment than standard SQL 
  1464.          offers.  It will generate a standard form for a table that can 
  1465.          then be modified - or you can start from scratch.  Each column 
  1466.          in the table can have a: 
  1467.               Help screen
  1468.               Column validation - it exists in another table
  1469.               Column validation - it is within a particular range
  1470.               Column validation - it is within a set of values
  1471.          
  1472.          With the column validation, if a mistake is made, the correct 
  1473.          values are displayed on the screen. 
  1474.          
  1475.          You have full data editing capabilities and you can easily move 
  1476.          from prompt to prompt to make changes.  You can even change the 
  1477.          colors. 
  1478.          
  1479.          You can retrieve rows of information from the form based on 
  1480.          simple search criteria and then page through the rows 
  1481.          retrieved. 
  1482.          
  1483.          The current version is limited to tables with less than 24 
  1484.          rows. 
  1485.          
  1486.          However, as an incentive for you to register, only the basics 
  1487.          are given to you.  The order form is on the last page of the 
  1488.          SSQL documentation or you can just print ORDER.FRM. 
  1489.          
  1490.          
  1491.          * SUMMARY OF KEYS WITHIN THE FORM *
  1492.  
  1493.          UP/DOWN ARROWS will move you from prompt to prompt.
  1494.  
  1495.          LEFT/RIGHT ARROWS will move you within the prompt as long as 
  1496.          there is text. 
  1497.  
  1498.          HOME KEY will take you to the beginning of the text.
  1499.  
  1500.          END KEY will take you to the end of the text.
  1501.  
  1502.          INSERT KEY will switch between the insert mode and the 
  1503.          overwrite mode. 
  1504.  
  1505.          DELETE KEY will delete the character where the cursor is at. 
  1506.  
  1507.          PAGEUP (used in Update/View mode) will display the previous row 
  1508.          of information. 
  1509.  
  1510.          PAGEDOWN (used in Update/View mode) will display the next row 
  1511.          of information. 
  1512.  
  1513.  
  1514.                                     ENV-10
  1515.  
  1516.  
  1517.          ESC will cancel the entry and exit the form program.
  1518.  
  1519.          ENTER will move you from prompt to prompt.  At the last prompt, 
  1520.          it will accept the form and insert the information into the 
  1521.          table. 
  1522.          
  1523.          F1   When help has been defined for the form, a window containing 
  1524.               the help message will be displayed.
  1525.          
  1526.          F2   Takes the information you have already entered in the form and
  1527.               searches the table for a match.  All rows that match are
  1528.               retrieved.  You can use the PAGEUP/PAGEDOWN to display the
  1529.               other rows.
  1530.          
  1531.          F4   Displays the column value entered in the previous row.  This
  1532.               is useful when, for example, you need to enter the same city
  1533.               over and over.  Type the city once.  For the next row of
  1534.               information, just press the F4 when you get to the city
  1535.               column.
  1536.          
  1537.          ALT-I     Puts you in INSERT mode.
  1538.          ALT-V     Puts you in VIEW mode and turns off column validation.
  1539.          ALT-U     UPDATEs what is on the screen.
  1540.          ALT-D     DELETEs based on what is on the screen.
  1541.          
  1542.          
  1543.          * GENERATING A FORM *
  1544.          In order to more easily explain the commands in the form, let's
  1545.          start by having the form program generate a form for us.  To do
  1546.          that:
  1547.          1.   Make sure that the form program is accessable from the
  1548.               directory with your table files.  We will be using the CUST
  1549.               table that is on the SSQL distribution disk.
  1550.          2.   Type: form cust (and press ENTER)
  1551.          
  1552.          The form program generates a file called CUST.SCR which 
  1553.          describes the form and then runs the form and puts you in the 
  1554.          insert mode. Press ESC to exit the form. 
  1555.          
  1556.          Now you have a file on your disk called CUST.SCR which looks 
  1557.          like: 
  1558.          
  1559.          D1 Data Entry for CUST
  1560.          D1 CODE   ~~
  1561.          D1 NAME   ~~~~~~~~~~~~~~~
  1562.          D1 ST     ~~
  1563.          D1 RATING ~~
  1564.          CO CODE
  1565.          CO NAME
  1566.          CO ST
  1567.          CO RATING
  1568.          TA CUST
  1569.          
  1570.          The first two columns on every line must contain a command.  The
  1571.          commands are:
  1572.  
  1573.                                     ENV-11
  1574.  
  1575.  
  1576.          D1   This displays information on the screen.  Anything you type
  1577.               will appear on the screen EXCEPT the underline.  The ~ (til)
  1578.               shows where data entry will occur.  Every ~ must have
  1579.               a corresponding CO line which defines the column.  The first
  1580.               ~s will be matched with the first CO line, the second
  1581.               ~s will be matched with the second CO line, and so on.
  1582.               The ~s do not have to be as wide as the column is
  1583.               defined.  If, for example, the column is defined as being 80
  1584.               characters, you can have ~s only 30 characters wide.
  1585.               It will create a text window.  Although you can only see 30
  1586.               characters at a time, you can still enter the full 80
  1587.               characters.
  1588.          
  1589.          CO   A single valid column name in the table must follow the CO
  1590.               command.  There must be a CO command for every set of ~s that
  1591.               appears in the D1 commands.
  1592.          
  1593.          TA   The table name is put here.
  1594.          
  1595.          As for the ordering of the commands, the only thing to remember 
  1596.          is that the CO command must follow the corresponding ~s.
  1597.          We could rewrite the cust screen to be any of the following: 
  1598.          
  1599.          TA CUST
  1600.          D1 Data Entry for CUST
  1601.          D1 CODE   ~~
  1602.          CO CODE
  1603.          D1 NAME   ~~~~~~~~~~~~~~~
  1604.          CO NAME
  1605.          D1 ST     ~~
  1606.          CO ST
  1607.          D1 RATING ~~
  1608.          CO RATING
  1609.          
  1610.          D1******************************************************
  1611.          D1*              CUSTOMER FORM                         *
  1612.          D1* Customer Number  ~~  Company Name  ~~~~~~~~~~~~~~~ *
  1613.          D1* State ~~ Rating ~~                                 *
  1614.          D1******************************************************
  1615.          CO CODE
  1616.          CO NAME
  1617.          CO ST
  1618.          CO RATING
  1619.          TA CUST
  1620.          
  1621.          (The following reverses ST and RATING to show that you do not have
  1622.          to put the columns in the same order as they exist in the table.)
  1623.  
  1624.                                     ENV-12
  1625.  
  1626.  
  1627.          D1******************************************************
  1628.          D1*              CUSTOMER FORM                         *
  1629.          D1* Customer Number  ~~  Company Name  ~~~~~~~~~~~~~~~ *
  1630.          D1* Rating ~~  State ~~                                *
  1631.          D1******************************************************
  1632.          CO CODE
  1633.          CO NAME
  1634.          CO RATING
  1635.          CO ST
  1636.          TA CUST
  1637.          
  1638.          
  1639.          *** INSERT ***
  1640.          You are in the insert mode when you start the form.  Refer to 
  1641.          the SPECIAL KEYS section for your options within the form.  For 
  1642.          a NULL, press the ENTER key without entering anything for the 
  1643.          column. Pressing the ENTER key at the last prompt will insert 
  1644.          the row into the table. 
  1645.          
  1646.          *** UPDATE ***
  1647.          NOTE: The Update will only work on tables where the Unique 
  1648.          constraint has been specified.  By using the Unique on one or 
  1649.          more columns when the table is created, it determines the key 
  1650.          for the table.  For more information, see the Create Table 
  1651.          instructions in SSQL.DOC. There are three basic steps to 
  1652.          updating: 
  1653.          1.   Press ALT-V to get into the VIEW mode.  
  1654.          2.   Retrieve the row you want to update.  You retrieve rows by 
  1655.               entering data in one or more prompts on the form and pressing
  1656.               F2.  It will retrieve all rows that are equal to the data you
  1657.               entered.  For example, you want to change the rating on a
  1658.               customer in Arizona, but you forget the exact name.  You could
  1659.               move to the ST prompt, type AZ, and press F2.  The first
  1660.               customer in AZ would be displayed. You could then use PgUp and
  1661.               PgDn to find the correct customer.
  1662.          3.   Change the data in any of the prompts.  If more than one row
  1663.               was retrieved, you can press PageDn to move to the next row. 
  1664.               When you have changed the row you want updated, Press Alt-U.
  1665.          
  1666.          *** VIEW ***
  1667.          The only difference between update and view is that in view, no
  1668.          data is changed.
  1669.          1.   Press ALT-V to get into the view mode.  
  1670.          2.   Retrieve the row(s) you want to view.  You retrieve rows by
  1671.               entering data in one or more prompts on the form and pressing
  1672.               F2.  It will retrieve all rows that are equal to the data you
  1673.               entered.  For example, if you wanted to view all the customers
  1674.               in Arizona, you would move to the ST prompt, type AZ, and
  1675.               press F2.  The first customer in AZ would be displayed.  
  1676.          3.   If more than one row was retrieved, you can press PageDn to
  1677.               move to the next row - then PageUp move back a row.
  1678.          
  1679.          *** DELETE ***
  1680.          To delete a row, you follow the steps for updating except at step
  1681.          3 you press ALT-D to delete the row being displayed.
  1682.  
  1683.                                     ENV-13
  1684.  
  1685.  
  1686.          ***REPORT Version 1.0 - The SSQL Report Generator ***
  1687.  
  1688.  
  1689.          *** OVERVIEW ***
  1690.          SSQL Report allows you to produce a formatted report.  You use 
  1691.          SSQL (or any text editor).  Instead of just reading the 
  1692.          documentation, it is a good idea to first study and run the 
  1693.          examples and then come back to the documentation.  As is common 
  1694.          with shareware documentation, you need to register in order to 
  1695.          find out all the details of operation. 
  1696.          
  1697.          *** PROCESS A REPORT ***
  1698.          To process a report, first make sure that SSQL has been installed   
  1699.  
  1700.          properly.  The tables you reference MUST be in the current
  1701.          directory. Select The Report Option from SQL Menu in SSQL by 
  1702.          pressing ALT-Q and then R.
  1703.          A file menu will be displayed with the .FRM files which contain the
  1704.          report files.  The files RPT1.FRM to RPT7.FRM have been included on 
  1705.          the distribution disk.  Make sure that RPT1.FRM is in your 
  1706.          current directory along with the DBF/SQD files.  Select RPT1.FRM
  1707.          and the result will be displayed on the screen after a few moments  
  1708.          (quite a few moments if you are using a floppy disk).
  1709.          It can also be run directly from DOS by typing: SSQLRPT RPT1.FRM
  1710.  
  1711.          
  1712.          ***SYNTAX OF REPORT FILES***
  1713.          All lines begin with a command.  All commands are two 
  1714.          characters long.  The first character after the command 
  1715.          corresponds to column one on the report. The following example 
  1716.          shows you a basic report. 
  1717.  
  1718.          -- RPT1.FRM (With 3 tables and no indexing, it is slow on an
  1719.          --           old PC)
  1720.          HT                      SALES REPORT
  1721.          HT
  1722.          HT      DATE      CUST NAME        PROD DESCRIP     QTY
  1723.          HT      ********  ***************  ***************  ***
  1724.          DT      XXXXXXXX  XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX  999
  1725.          ET**** END OF REPORT ****
  1726.          DC date, name, descrip, qty
  1727.          TA sales,cust,prod
  1728.          WH cc = cust.code and
  1729.          WH pc = prod.code
  1730.          
  1731.          ** COMMENTS (--) 
  1732.          Begin a line with two dashes if you do not want it processed by 
  1733.          the program. 
  1734.          
  1735.          ** PRINTER (PR)
  1736.          If you want the report sent to the printer, the command is PR, 
  1737.          and the number after it refers to the printer number.  If you 
  1738.          do not know the printer number, it is probably 1.  
  1739.          
  1740.                                    ENV-14
  1741.          
  1742.          It advances to the next page after 59 lines.  I made it 59 
  1743.          instead of 66 for those of us with Laserjets. If you do not have 
  1744.          the PR command, the report will be displayed on the screen. 
  1745.          
  1746.          ** HEADING (HT) (optional)
  1747.          The heading is produced at the top of every page or screen.    
  1748.          Headings can contain text, the current date (in both U.S. and 
  1749.          international formats), a page number, and you can skip a pre-
  1750.          determined number of lines (good for setting margins).  For 
  1751.          information on HOW to do all this, register and get the full 
  1752.          documentation. 
  1753.          
  1754.          ** DETAIL TEXT(DT)
  1755.          The detail represents the placement of information for each row 
  1756.          retrieved from the table.  The information can be placed on 
  1757.          more than one line.  It corresponds to the rows requested in 
  1758.          the Detail Columns (DC) line. It may contain text, but Xs and 
  1759.          9s have special meaning. 
  1760.          
  1761.          X
  1762.          This is used for character data.  It will only display the 
  1763.          number of characters represented by the number of Xs you have 
  1764.          for the column.  This is useful when a column may be defined as 
  1765.          having 30 columns but you just want to see the first 15.  You 
  1766.          would simply put only 15 Xs in the position you wanted the 
  1767.          column information to be displayed. 
  1768.          
  1769.          9
  1770.          The 9 represents numeric data.  You need to make sure that you 
  1771.          have enough 9s for the largest number in the column, otherwise 
  1772.          asterisks (*) will be displayed instead.  Numeric data can be 
  1773.          specially formatted (more on that in the full documentation). 
  1774.          
  1775.          ** FOOTER (FT) (optional)
  1776.          The footer is placed at the bottom of every page sent to the 
  1777.          printer.  It is skipped over if the report is sent to the 
  1778.          screen.  The options for the footer are the same as the ones 
  1779.          for the heading. 
  1780.          
  1781.          ** END OF REPORT (ET) (optional)
  1782.          This information appears just once, at the end of the report.  
  1783.          The options are that same as the ones for the heading. 
  1784.          
  1785.          ** DETAIL COLUMNS (DC)
  1786.          There must be a column for every format described under DT.  
  1787.          The format follows the syntax for the select statement in SSQL.  
  1788.          This means that you can have calculated columns, aggregate 
  1789.          functions - anything that you can put between the word SELECT 
  1790.          and the word FROM in a select statement.  There can be only one 
  1791.          DC line. 
  1792.          
  1793.          ** SUBTOTALS (optional) **
  1794.          Actually, subtotal is a misnomer because you can use any 
  1795.          aggregate function (count, avg, min, max, sum) - not just sum.  
  1796.          There are three parts to creating subtotals.  The first part is 
  1797.          the format (1T) which has the same rules as the ones for detail 
  1798.          
  1799.                                    ENV-15
  1800.          
  1801.  
  1802.          text (DT).  The second part determines the group you want 
  1803.          subtotaled (1B).  The third part (1C) contains the aggregate 
  1804.          function(s) that you want in the subtotal.  The command sent to 
  1805.          SSQL is: 
  1806.          SELECT Break Column(s), Subtotals 
  1807.          FROM   (TA line) 
  1808.          WHERE  (WH line(s)) GROUP BY Break Column(s); 
  1809.          
  1810.          For example:
  1811.          
  1812.          --RPT2.FRM
  1813.          HT SALES REPORT
  1814.          HTCust  Prod  QTY 
  1815.          HT----  ----  ---
  1816.          DTXX    XX    999
  1817.          1T           ----
  1818.          1T Cust Tot  9999
  1819.          1T
  1820.          DC cc, pc, qty
  1821.          1B cc
  1822.          1C sum(qty)
  1823.          TA sales
  1824.          
  1825.          ** SUBTOTAL TEXT (1T)
  1826.          This is where you describe the format of the subtotal.  The 
  1827.          rules are the same as the ones for detail text (DT) except that 
  1828.          you can only use numeric data (9s). 
  1829.          
  1830.          ** BREAK COLUMN(S) (1B)
  1831.          This refers to the column(s) that you want the aggregate 
  1832.          functions grouped by.  The column(s) must also exist in the 
  1833.          Detail Columns (DT).  It functions just like the GROUP BY 
  1834.          clause.  See the manual under the section GROUP BY in chapter 
  1835.          10 for more information on what can be put here. 
  1836.          
  1837.          ** SUBTOTALS (1C)
  1838.          This is where you put the aggregate functions that you want 
  1839.          displayed in the format you described in the Subtotal Text 
  1840.          (1T).  You need one aggregate function for every numeric format 
  1841.          in 1T. 
  1842.          
  1843.          ** GRAND TOTAL (optional)
  1844.          Again a misnomer.  You can do more than total a column (sum), 
  1845.          you can use any aggregate function.  The grand total is 
  1846.          displayed once at the end of the report.  There are two parts: 
  1847.          grand total text (GT) describes the format, and grand total 
  1848.          columns (GC) describes the aggregate function that you want 
  1849.          displayed.  The command sent to SSQL is: 
  1850.          SELECT Grand Total Column(s) 
  1851.          FROM   (TA line) 
  1852.          WHERE  (WH line(s)) 
  1853.          
  1854.  
  1855.  
  1856.  
  1857.          
  1858.                                    ENV-16
  1859.          
  1860.  
  1861.          For example:
  1862.          
  1863.          --RPT3.FRM
  1864.          HT SALES REPORT
  1865.          HTCust  Prod  QTY 
  1866.          HT----  ----  ---
  1867.          DTXX    XX    999
  1868.          GT           ----
  1869.          GT Grand Tot 9999
  1870.          DC cc, pc, qty
  1871.          GC sum(qty)
  1872.          TA sales
  1873.          
  1874.          
  1875.          ** GRAND TOTAL TEXT (GT)
  1876.          This is where you describe the format of the grand total.  The 
  1877.          rules are the same as the ones for detail text (DT) except that 
  1878.          you can only use numeric data (9s). 
  1879.          
  1880.          ** GRAND TOTAL COLUMN(S) (GC)
  1881.          List the aggregate function(s) that you want in the grand 
  1882.          total.  There must be one aggregate function for every numeric 
  1883.          format described in GT. 
  1884.          
  1885.          
  1886.          ** TABLE(S) (TA)
  1887.          This correponds to one or more tables that you want selected.  
  1888.          There can be only on TA line in a report.  Anything valid after 
  1889.          the first FROM in SQL is valid here.  See the manual in chapter 
  1890.          10 and 11 for more information on what can be put here. 
  1891.          
  1892.          ** THE WHERE CLAUSE (WH) (optional)
  1893.          Anything valid after the first FROM in SQL is valid here 
  1894.          including subqueries with the following exception:  You cannot 
  1895.          have a GROUP BY or an ORDER BY if you have also specified 
  1896.          subtotals.  Make sure that the DC, TA, and WH lines go together 
  1897.          to form a valid statement as in: 
  1898.          SELECT (DC line) 
  1899.          FROM   (TA line) 
  1900.          WHERE  (WH line(s)); 
  1901.          
  1902.          
  1903.                                    ENV-17
  1904.          
  1905.  
  1906.          Some more extensive report formats and hints.
  1907.          
  1908.          --RPT4.FRM (Be patient when running this one.  It actually 
  1909.          --          contains three queries)
  1910.          --
  1911.          HT        SALES REPORT
  1912.          HT    MONOLITH MANUFACTURING 
  1913.          HTCustomer Name    Product     QTY 
  1914.          HT---------------  ----------  ---
  1915.          DTXXXXXXXXXXXXXXX  XXXXXXXXXX  999
  1916.          1T                          ------
  1917.          1T The customer total is    99,999
  1918.          1T
  1919.          GT                         -------
  1920.          GT GRAND TOTAL ............999,999
  1921.          ET
  1922.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1923.          ET                 END OF REPORT
  1924.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1925.          DC name, descrip, qty
  1926.          1B name
  1927.          1C sum(qty)
  1928.          GC sum(qty)
  1929.          TA sales,prod,cust
  1930.          WH sales.cc = cust.code and
  1931.          WH sales.pc = prod.code
  1932.          
  1933.          
  1934.          --RPT4A.FRM - another way of writing RPT4.FRM
  1935.          HT        SALES REPORT
  1936.          HT    MONOLITH MANUFACTURING 
  1937.          HTCustomer Name    Product     QTY 
  1938.          HT---------------  ----------  ---
  1939.          DTXXXXXXXXXXXXXXX  XXXXXXXXXX  999
  1940.          DC name, descrip, qty
  1941.          1T                          ------
  1942.          1T The customer total is    99,999
  1943.          1T
  1944.          1B name
  1945.          1C sum(qty)
  1946.          GT                         -------
  1947.          GT GRAND TOTAL ............999,999
  1948.          GC sum(qty)
  1949.          ET
  1950.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1951.          ET        END OF REPORT
  1952.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1953.          TA sales,prod,cust
  1954.          WH sales.cc = cust.code and
  1955.          WH sales.pc = prod.code
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.          
  1962.                                    ENV-18
  1963.          
  1964.  
  1965.          --RPT5.FRM - Multiple line detail
  1966.          HT                      SALES REPORT
  1967.          HT
  1968.          HT      DATE      CUST CODE        PROD CODE        QTY
  1969.          HT                CUST NAME        PROD DESCRIP     
  1970.          HT      ********  ***************  ***************  ***
  1971.          DT      XXXXXXXX  XX               XX               999
  1972.          DT                XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX
  1973.          DT
  1974.          DC date, cc, pc, qty, name, descrip
  1975.          TA sales,cust,prod
  1976.          WH cc = cust.code and
  1977.          WH pc = prod.code
  1978.          
  1979.          
  1980.          --RPT6.FRM - Projections
  1981.          -- Minor quirk - when you repeat column names as in the DC line
  1982.          -- below, "rename" them by putting a unique identifier after each
  1983.          -- one.
  1984.          HT          THE EFFECT OF A PERCENTAGE INCREASE 
  1985.          HT                 IN PERCENT OF DEFECTS
  1986.          HT
  1987.          HT                MANU   PROD     ** DEFECTS **
  1988.          HT      DATE      STATE  CODE     0% 10% 20% 30% 
  1989.          HT      --------  --     --      --  --  --  --   
  1990.          DT      XXXXXXXX  XX     XX      99  99  99  99 
  1991.          DC date,mst,code,defects,defects*1.1 d2,defects*1.2 d3,defects*1.3 d4
  1992.          TA prod
  1993.          
  1994.          --RPT7.FRM Multiple subtotals
  1995.          -- Minor quirk - when you repeat column names as in the 1C line
  1996.          -- below, "rename" them by putting a unique identifier after each
  1997.          -- one.
  1998.          HT      Code Defects Qty
  1999.          DT      XX   99      99
  2000.          1T      Average      99
  2001.          1T      Total        99
  2002.          1T      Maximum      99
  2003.          1T      Minimum      99
  2004.          DC code, defects,qty
  2005.          TA prod
  2006.          1B code
  2007.          1C avg(defects), sum(defects) c2, max(defects) c3, min(defects) c4
  2008.          
  2009.  
  2010.   
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.                                    ENV-19
  2022.  
  2023.          *** THE EXAMPLE DATABASE - MONOLITH MANUFACTURING ***
  2024.          Monolith Manufacturing manufactures and markets high tech
  2025.          products in the western states.  There are manufacturing
  2026.          facilities in various states.  Products are manufactured in
  2027.          batches at each facility.  When the batch is finished an entry is
  2028.          made in the table with the date the batch was finished, the
  2029.          product code, the state where the product was manufactured, the
  2030.          quantity of saleable items and the percent of defects in the
  2031.          batch if that is tracked.  Related to the manufacturing table is
  2032.          the product table which simply has the description that
  2033.          corresponds to each product code.
  2034.          The sales portion of the business has as its core the sales
  2035.          table.  When a sale is made, an entry is made which contains the
  2036.          date of sale, branch code, customer code, salesperson number,
  2037.          product code and quantity purchased.  Related to the sales table
  2038.          are the branch table, the employee table and the customer table. 
  2039.          The branch table contains the branch code, the state the branch
  2040.          is in and the employee number of the manager.  The employee table
  2041.          contains the employee number, the name of the employee, and the
  2042.          employee's manager number if the employee has a manager (the
  2043.          president does not have a manager).  All manager numbers have an
  2044.          entry in the employee number column since all managers are also
  2045.          employees.  The customer table contains the customer code, the
  2046.          customer name and the state where the customer resides.
  2047.          The following contains details on the Monolith's six tables.  The
  2048.          data types correspond to the types used in the create table
  2049.          command.  When a column in one table corresponds to a column
  2050.          (especially a primary key) in another table, it is in the form of
  2051.          TABLE.column. This information will be useful when we need to
  2052.          retrieve information that spans tables. 
  2053.          
  2054.          SALES TABLE
  2055.          column  data                               Common columns in 
  2056.          name    type       description             other tables
  2057.          date    date       Date of sale 
  2058.          bc      char(2)    Branch code             BRANCH.code
  2059.          cc      char(2)    Customer code           CUST.code
  2060.          sn      numeric(2) Salesperson number      EMP.code,
  2061.                                                  EMP.mgrnum, BRANCH.mgrnum
  2062.          pc      char(2)    product code            PROD.code, MANU.code
  2063.          qty     numeric(3) quantity
  2064.  
  2065.          Key is date, bc, cc, sn, pc
  2066.  
  2067.  
  2068.  
  2069.          
  2070.          
  2071.          
  2072.          
  2073.          
  2074.  
  2075.          
  2076.                                DATABASE-1
  2077.  
  2078.  
  2079.  
  2080.  
  2081.          BRANCH TABLE
  2082.          column   data                              Common columns in 
  2083.          name     type       description            other tables
  2084.          code     char(2)    branch code            SALES.bc
  2085.          st       char(2)    state code             CUST.st, MANU.mst
  2086.          mgrnum   numeric(2) manager number         EMP.enum,
  2087.                                                     EMP.mgrnum, SALES.sn
  2088.          KEY is code
  2089.          
  2090.          EMP TABLE
  2091.          column   data                              Common columns in 
  2092.          name     type       description            other tables
  2093.          enum     numeric(2) employee number        EMP.mgrnum, BRANCH.mgrnum, 
  2094.                                                     SALES.sn 
  2095.          name     char(20)   employee name
  2096.          mgrnum   numeric(2) manager number         EMP.enum,
  2097.                                                     BRANCH.mgrnum, SALES.sn
  2098.          KEY is enum
  2099.  
  2100.          PROD TABLE
  2101.          column   data                              Common columns in 
  2102.          name     type       description            other tables
  2103.          code     char(2)    product code           MANU.code, SALES.pc
  2104.          descrip  char(15)   product description
  2105.          KEY is code        
  2106.  
  2107.          MANU TABLE
  2108.          column   data                               Common columns in 
  2109.          name     type       description             other tables
  2110.          date     date       date of manufacture
  2111.          code     char(2)    product code            PROD.code
  2112.          mst      char(2)    state where product     BRANCH.st, PROD.st
  2113.                              is manufactured
  2114.          defects  numeric(3) percent defects in batch  
  2115.          qty      numeric(3) quantity manufactured
  2116.          KEY is date, code, ms
  2117.  
  2118.          CUST TABLE
  2119.          column   data                               Common columns in 
  2120.          name     type       description             other tables
  2121.          code     char(2)    Unique customer code    SALES.cc
  2122.          name     char(15)
  2123.          st       char(2)    State                   MANU.mst, PROD.st
  2124.          rating   numeric(2)  
  2125.          KEY is code
  2126.          
  2127.          
  2128.  
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.          
  2135.                                DATABASE-2
  2136.  
  2137.          
  2138.          
  2139.          
  2140.          USING YOUR WORD PROCESSOR
  2141.  
  2142.          You can modify any SSQL command by calling your text editor or
  2143.          word processor from within SSQL.  You must first specify which
  2144.          text editor or word processor you are using in a file called
  2145.          EDIT.FIL.  The file must contain the path and name of the text
  2146.          editor or word processor.  YOU CANNOT CALL A BATCH FILE.  The
  2147.          distribution disk contains an EDIT.FIL with the following:
  2148.          C:\PCWRITE\ED
  2149.          The command to start PC-Write is ED and it resides on drive C: in
  2150.          the PCWRITE directory.  You can replace the above line with the
  2151.          path and program name of your own. The EDIT.FIL must reside on
  2152.          your default directory. The syntax is:
  2153.          EDIT [file_name];
  2154.          The file_name is any valid name which will be accepted by your
  2155.          word processor on the command line.  For example, if you want to
  2156.          edit MGRORG, you would type:
  2157.          edit mgrorg;
  2158.          As is true with all commands except calling a script file, you
  2159.          must end this one with a ';'.  When you exit your word
  2160.          processor/text editor, you will return to SSQL.  Refer to the
  2161.          next section to find out how to process the file you just
  2162.          created.
  2163.          There is a special case when you want to edit the last SSQL
  2164.          command that you typed.  Every command you type except edit and
  2165.          exit is stored in a file called RUN.  To edit the last command
  2166.          entered type:
  2167.          edit run;
  2168.          If your word processor does not allow you to type the file name
  2169.          that you want to edit after the word processor command in
  2170.          EDIT.FIL simply type:
  2171.          edit;
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.          
  2194.                                    WORD-1
  2195.  
  2196.          
  2197.          
  2198.          
  2199.          CALL SCRIPT FILE 
  2200.  
  2201.          A script file is a text file which contains an SSQL command.  The
  2202.          script file can then be called from SSQL or SSQLENG and the command it
  2203.          contains will be processed.
  2204.          
  2205.          SYNTAX:
  2206.          @file_name 
  2207.          
  2208.          The file_name can be any valid DOS file name.
  2209.          The file must be text only, no special word processing formats
  2210.          allowed. PC-Write or Qedit are excellent choices because they
  2211.          always produce standard text. 
  2212.          
  2213.          COMMENTS IN THE SCRIPT FILE (OR ANY OTHER FILE)
  2214.          You can precede a comment by two dashes and from that point to
  2215.          the end of the line will not be processed by SSQL. The comment
  2216.          can be at any place in the line, not necessarily at the beginning
  2217.          as the following example shows.
  2218.          You can create a file called MGRORG which contains:
  2219.          --This query finds the names of managers who have
  2220.          -- salespeople who have sold products to Organomice
  2221.          select name      --Get names
  2222.          from emp      
  2223.          where enum in 
  2224.            (select mgrn   --of managers
  2225.             from emp 
  2226.             where enum in   
  2227.               (select sn        -- who have salespeople
  2228.                from sales,cust  -- who have sold products to
  2229.                where cc = code  -- Organomice
  2230.                and name = 'Organomice')); 
  2231.          
  2232.          From SSQL or SSQLENG, you would simply type:
  2233.          @MGRORG 
  2234.  
  2235.          Or, from SSQL, use F3 to open MGRORG, and then press F4 to process it.
  2236.  
  2237.          If you do this from SSQLENG, the file you created would be displayed
  2238.          on the screen and then the result would appear.
  2239.  
  2240.          The script file can contain multiple commands.
  2241.          
  2242.          DOS COMMANDS FROM SSQLENG
  2243.          From within SSQLENG, you can type DOS, press ENTER and be in a DOS
  2244.          shell.  You can then type EXIT and return to SSQLENG.  You can take
  2245.          a short-cut and, for example call Qedit from within SSQLENG to edit
  2246.          MGRORG with the command from within SSQLENG:
  2247.          DOS Q MGRORG
  2248.          At this point you will be in Qedit and be able to edit your query.
  2249.          When you exit Qedit, you will be in SSQLENG and be able to call
  2250.          the script file MGRORG.
  2251.  
  2252.  
  2253.                             SCRIPT-1
  2254.  
  2255.  
  2256.          QUERIES - SINGLE TABLE
  2257.  
  2258.          OVERVIEW
  2259.  
  2260.          The select command is used to retrieve data from tables. The
  2261.          select command is covered in three later chapters too.  Although
  2262.          the use of select on a single table is relatively easy,
  2263.          translating from English to SQL (or most other query languages)
  2264.          can be rather tricky.  The approach of this chapter is not merely
  2265.          to explain the syntax of the command, it should give you a more
  2266.          critical approach to applying SQL.
  2267.  
  2268.          SYNTAX: 
  2269.  
  2270.          SELECT [DISTINCT] column_name [,column_name ...] 
  2271.          FROM table_name [,table_name ...] 
  2272.          [WHERE search_expression] 
  2273.          [REDIRECTTO file_name]         <--Non-Standard 
  2274.          [GROUP BY column_name [,column_name ...] 
  2275.          [HAVING criteria]]
  2276.          [ORDER BY column_name [,column_name ...] 
  2277.  
  2278.          If you want all the columns in a table you can replace the list
  2279.          of column names with an asterisk (*). 
  2280.          The select command produces a report in the form of a table.
  2281.          Although the basic use of the select takes a short time to learn,
  2282.          the more advanced uses take a long time to master. 
  2283.          The tables used in this chapter are on the distribution disk so
  2284.          you can try entering the commands yourself.
  2285.          All the examples have each clause starting on a new line for
  2286.          purposes of clarity. It is not necessary to have them on separate
  2287.          lines. It is sometimes advantageous to keep the query on one line
  2288.          because if you need to make a correction, you cannot backup a
  2289.          line. However, to make correcting command much easier, you can
  2290.          call your word processor from within SSQL. This is described in
  2291.          the full documentation.
  2292.  
  2293.          THE BASICS OF SELECT 
  2294.  
  2295.          Find all the data in the manu table.
  2296.          
  2297.          select *  
  2298.          from manu; 
  2299.          date     code mst defects qty  
  2300.          -------- ---- --- ------- ---  
  2301.          02/07/87 GC   ID       12  15 
  2302.          02/01/87 GC   ID        0  55  
  2303.          02/02/87 NM   CA       17  93  
  2304.          02/02/87 DD   ID           25  
  2305.          02/03/87 DD   WA       22  46  
  2306.          02/02/87 NM   WA       15  25  
  2307.          02/04/87 DD   AZ       12  25  
  2308.          02/04/87 DD   CA       15  25  
  2309.          02/06/87 GC   AZ        4  43  
  2310.           
  2311.  
  2312.                                 SELECT-1
  2313.  
  2314.          
  2315.          Instead of typing all the column names, you can use the * to
  2316.          denote all column names. On the next line, the "from manu" tells
  2317.          us that we want the data to come from the table named "manu". All
  2318.          select commands must end with a semi-colon (;). If we just wanted
  2319.          the product code and the state that it was manufactured in you
  2320.          would type:
  2321.          
  2322.           select code, mst 
  2323.           from manu; 
  2324.           code mst  
  2325.           ---- ---  
  2326.           GC   ID   
  2327.           GC   ID   
  2328.           NM   CA   
  2329.           DD   ID   
  2330.           DD   WA   
  2331.           NM   WA   
  2332.           DD   AZ   
  2333.           DD   CA   
  2334.           GC   AZ   
  2335.            
  2336.           9 rows selected 
  2337.          
  2338.          DISTINCT 
  2339.          The optional statement "distinct" will eliminate all rows with
  2340.          duplicate columns that you have specified. If you wanted to find
  2341.          out all the different states where products are manufactured, you
  2342.          would type:
  2343.          
  2344.          select distinct mst 
  2345.          from manu; 
  2346.           
  2347.          mst  
  2348.          ---  
  2349.          ID   
  2350.          CA   
  2351.          WA   
  2352.          AZ   
  2353.           
  2354.          4 rows selected 
  2355.          
  2356.          As you can see only 4 rows were found. This is because  there can
  2357.          be more than one row with the same state. Only the first
  2358.          occurrence of each data element is retained. 
  2359.          The "distinct" modifier can be used with one or more column names
  2360.          in a select statement. However, the "distinct" modifier operates
  2361.          on the whole row, not just a single column. In the manu table,
  2362.          the primary key is date, code, mst. What if we wanted not only
  2363.          the state where the products are manufactured but the product
  2364.          codes too. Notice what happens with the following:
  2365.  
  2366.  
  2367.  
  2368.  
  2369.          
  2370.          
  2371.                                 SELECT-2
  2372.  
  2373.          
  2374.          select mst, code 
  2375.          from manu; 
  2376.           
  2377.          mst code  
  2378.          --- ----  
  2379.          ID  GC    
  2380.          ID  GC    
  2381.          CA  NM    
  2382.          ID  DD    
  2383.          WA  DD    
  2384.          WA  NM    
  2385.          AZ  DD    
  2386.          CA  DD    
  2387.          AZ  GC    
  2388.          
  2389.          Some of the rows are duplicated because it is possible to
  2390.          manufacture the same product in the same state but on a different
  2391.          date. To get rid of the duplication of rows we can add the
  2392.          "distinct" modifier.
  2393.          
  2394.          WHERE 
  2395.          
  2396.          The where clause of the select command is the most powerful and
  2397.          complex part of SQL. Our discussion will be broken down into
  2398.          various phases. The first phase is using the where with a single
  2399.          table name. Later you will see it used with multiple tables and
  2400.          you will find that you can even put another select command within
  2401.          the where clause. 
  2402.          
  2403.          search_expression 
  2404.          
  2405.          The simplest search expression relates a column name to a
  2406.          constant. A constant is an exact value that you enter. All column
  2407.          values are tested against the constant value. If the column name
  2408.          that you are testing is defined as a character, the constant must
  2409.          be enclosed in single quotes or double quotes. The constant you
  2410.          are searching for may not contain a quote. If the column name you
  2411.          are testing for is defined as numeric, the constant is a number
  2412.          without any quotes. 
  2413.          You can relate the column name to the constant in the following
  2414.          ways: 
  2415.          
  2416.          = equal 
  2417.          <> not equal 
  2418.          != not equal 
  2419.          ~= not equal
  2420.          >  greater than 
  2421.            less than 
  2422.          >= greater than or equal 
  2423.          <=  less than or equal 
  2424.          IS NULL   column value is null
  2425.          IS NOT NULL   column value is not null
  2426.          LIKE  like a pattern
  2427.          NOT LIKE  not like a pattern
  2428.  
  2429.  
  2430.                                 SELECT-3
  2431.  
  2432.  
  2433.  
  2434.          
  2435.          Find out which customers are in Arizona. 
  2436.          
  2437.          select * 
  2438.          from cust 
  2439.          where st = 'AZ'; 
  2440.           
  2441.          code name            st rating  
  2442.          ---- --------------- -- ------  
  2443.          ZZ   Organomice      AZ     34  
  2444.          DD   QuarkCo         AZ     10  
  2445.           
  2446.          2 rows selected 
  2447.          
  2448.          Spaces are optional on either side of the "=". You could have
  2449.          typed: 
  2450.          
  2451.          where st='AZ'; 
  2452.          
  2453.          Instead of single quotes you could have used double quotes: 
  2454.          
  2455.          where st="AZ"; 
  2456.          
  2457.          In the manu table, the column name "defects" is the percent of
  2458.          defects when the product was manufactured. The statement below
  2459.          shows you how to get a list of all the information for those
  2460.          products with defects over 10%.
  2461.          
  2462.          select * 
  2463.          from manu 
  2464.          where defects > 10; 
  2465.           
  2466.          date     code mst defects qty  
  2467.          -------- ---- --- ------- ---  
  2468.          02/07/87 GC   ID       12  15  
  2469.          02/02/87 NM   CA       17  93  
  2470.          02/03/87 DD   WA       22  46  
  2471.          02/02/87 NM   WA       15  25  
  2472.          02/04/87 DD   AZ       12  25  
  2473.          02/04/87 DD   CA       15  25  
  2474.           
  2475.          6 rows selected 
  2476.          
  2477.          The symbols such as <, >, =, <=, >=, and != can be used with
  2478.          character data too. For example, the statement below shows how to
  2479.          retrieve all customer names alphabetically greater than Machado.
  2480.  
  2481.  
  2482.  
  2483.  
  2484.  
  2485.  
  2486.  
  2487.  
  2488.          
  2489.                                 SELECT-4
  2490.  
  2491.  
  2492.  
  2493.  
  2494.  
  2495.          
  2496.          select name 
  2497.          from cust 
  2498.          where name > "Machado"; 
  2499.           
  2500.          name             
  2501.          ---------------  
  2502.          Technoharps      
  2503.          Organomice       
  2504.          QuarkCo          
  2505.          Marswarp         
  2506.          Multicrud        
  2507.           
  2508.          5 rows selected 
  2509.          
  2510.          SPECIAL SEARCH EXPRESSION - IS NULL, IS NOT NULL 
  2511.          
  2512.          Nulls are an important concept in relational databases. For
  2513.          example, let's look at the percentage of defects in Idaho:
  2514.          
  2515.          select *
  2516.          from manu
  2517.          where mst = 'ID'; 
  2518.            
  2519.          date     code mst defects qty  
  2520.          ------   ---- --- ------- ---  
  2521.          02/07/87 GC   ID       12  15  
  2522.          02/01/87 GC   ID        0  55  
  2523.          02/02/87 DD   ID           25  
  2524.           
  2525.          3 rows selected 
  2526.           
  2527.          In Idaho, the batch of MW on 02/07/87 has 12% defects, GC on 02/01/87
  2528.          has 0% defects but there is nothing listed for DD. This "nothing"
  2529.          is called a null. It does not mean zero or blank. A null means
  2530.          that the data element is unknown. In our case, product DD in
  2531.          Idaho is not tracked for defects like other products. This isn't
  2532.          to say that there are zero defects. It just means we have no
  2533.          entry. 
  2534.          If we wanted to look at all products for which we do not track
  2535.          defects, we would use the following:
  2536.          
  2537.          select * 
  2538.          from manu 
  2539.          where defects is null; 
  2540.          date     code mst defects qty  
  2541.          -------- ---- --- ------- ---  
  2542.          02/02/87 DD   ID           25  
  2543.           
  2544.          1 row selected 
  2545.          
  2546.  
  2547.  
  2548.                                 SELECT-5
  2549.  
  2550.  
  2551.          
  2552.          We could find all the products for which we track defects by
  2553.          using is not null:
  2554.          
  2555.          select * 
  2556.          from manu 
  2557.          where defects is not null; 
  2558.          date     code mst defects qty  
  2559.          -------- ---- --- ------- ---  
  2560.          02/07/87 GC   ID       12  15  
  2561.          02/01/87 GC   ID        0  55  
  2562.          02/02/87 NM   CA       17  93  
  2563.          02/03/87 DD   WA       22  46  
  2564.          02/02/87 NM   WA       15  25  
  2565.          02/04/87 DD   AZ       12  25  
  2566.          02/04/87 DD   CA       15  25  
  2567.          02/06/87 GC   AZ        4  43  
  2568.           
  2569.          8 rows selected 
  2570.          
  2571.          Null values can be used with both numeric columns and character
  2572.          columns.
  2573.          
  2574.          SPECIAL SEARCH EXPRESSION - LIKE, NOT LIKE 
  2575.          
  2576.          An expression with like is used when we want to retrieve data
  2577.          that is similar in some way to another set of characters. The
  2578.          like expression can only be used with character data. This is
  2579.          explained in the full documentation.
  2580.          
  2581.          AND, OR, NOT 
  2582.          
  2583.          We can link expressions with ands and ors. If you use an and,
  2584.          both expressions have to be true in order to add the row to the
  2585.          table we are producing. If you use an or, only one of the
  2586.          expressions has to be true. You can use as many ands and ors as
  2587.          you want in a query. The expressions are evaluated from left to
  2588.          right. All the ands are done first, then evaluation starts again
  2589.          from the left and the ors are processed. 
  2590.          If you want to reference different column names in a search, the
  2591.          logic as stated in English is typically the same as it is in SQL.
  2592.          Usually there is no confusion between the and and the or unless
  2593.          you are dealing with the same column name. 
  2594.          Find all products in Washington with defects over 16%:
  2595.          
  2596.          select distinct code 
  2597.          from manu 
  2598.          where mst = 'WA' and defects > 16; 
  2599.           
  2600.          code  
  2601.          ----  
  2602.          DD    
  2603.           
  2604.          1 row selected 
  2605.  
  2606.  
  2607.                                 SELECT-6
  2608.  
  2609.          
  2610.          Generally, the use of a single and or an or in a where clause is
  2611.          more or less common sense unless you are referring to the same
  2612.          column name. What if you wanted to find all the products
  2613.          manufactured in Washington and Idaho? This has two meanings in
  2614.          everyday English: 
  2615.          
  2616.          1. Out of all the products we manufacture, I want a list of the
  2617.          ones we manufacture in Washington and a list of the ones we
  2618.          manufacture in Idaho. 
  2619.          
  2620.          2. Out of all the products we manufacture, I want a list of each
  2621.          one we manufacture in BOTH Washington and Idaho. 
  2622.          
  2623.          We will accept the first meaning in this example. It is
  2624.          interesting to note that we could have also said, "List all the
  2625.          products manufactured in Washington or Idaho." It seems odd that
  2626.          in English "or" can have the same meaning as "and". Even if you
  2627.          don't agree with my interpretation, beware that others do and
  2628.          that it may cause communication problems. 
  2629.          In the English version you use the "and" so there is a tendency
  2630.          to always use the "and" in the SQL version too. This would be
  2631.          wrong. The English perspective and the SQL perspective are
  2632.          different. The English perspective often looks at the whole group
  2633.          of items. The SQL perspective looks at one item at a time. To
  2634.          translate Washington and Idaho into SQL we look at it from its
  2635.          perspective. We will look at each item. If that item is
  2636.          manufactured in Washington or Idaho, we will add it to our table.
  2637.          Notice that when we look at it from the SQL perspective, we use
  2638.          the "or" instead of the "and". This not just a quirk of SQL
  2639.          logic, it is common to virtually all data bases. The statement
  2640.          below shows how to find out all the products manufactured in
  2641.          Washington and Idaho: 
  2642.          
  2643.          select distinct code 
  2644.          from manu 
  2645.          where mst='WA' or mst='ID'; 
  2646.           
  2647.          code  
  2648.          ----  
  2649.          GC    
  2650.          DD    
  2651.          NM    
  2652.           
  2653.          3 rows selected 
  2654.          
  2655.          It is interesting to note what the meaning would be if we used an
  2656.          and instead of an or. Would it tell us every product manufactured
  2657.          in BOTH Washington and Idaho? No, it wouldn't as you can see in
  2658.          the following:
  2659.          
  2660.          select distinct code 
  2661.          from manu 
  2662.          where mst='WA' and mst='ID'; 
  2663.           
  2664.          no rows found 
  2665.  
  2666.                                 SELECT-7
  2667.  
  2668.  
  2669.  
  2670.          
  2671.          Actually, it makes no sense. How could a state for an individual
  2672.          item be both WA and ID????? 
  2673.          
  2674.          But how WOULD you find out every product manufactured in both
  2675.          Washington and Idaho? The search strategy is rather complex and
  2676.          the topic is covered under joining tables and again when we
  2677.          discuss subqueries. 
  2678.          
  2679.          In order to express the opposite logic, you use the not but you
  2680.          have to be careful. The statement below shows how to find the
  2681.          products manufactured outside of Washington or Idaho. 
  2682.          
  2683.          select * 
  2684.          from manu 
  2685.          where not (mst='WA' or mst='ID'); 
  2686.          
  2687.          date     code mst defects qty  
  2688.          -------- ---- --- ------- ---  
  2689.          02/02/87 NM   CA       17  93  
  2690.          02/04/87 DD   AZ       12  25  
  2691.          02/04/87 DD   CA       15  25  
  2692.          02/06/87 GC   AZ        4  43  
  2693.           
  2694.          4 rows selected 
  2695.          
  2696.          Again, we must be careful about how we express the query in
  2697.          English. The query above cannot be translated as: Find the
  2698.          products NOT manufactured in Washington or Idaho. The query above
  2699.          retrieved some products that happen to be manufactured in Idaho
  2700.          or Washington. We really did not care. The only constraint we
  2701.          cared about was the state in which it was manufactured. The query
  2702.          could accurately be translated in fractured English: Find the
  2703.          products manufactured not in Washington or Idaho. By changing the
  2704.          statement to one that requests products not manufactured in
  2705.          Washington or Idaho, we change the words modified by "not" from
  2706.          the states to products manufactured. To find the products not
  2707.          manufactured in Washington or Idaho we must first find the set of
  2708.          all products manufactured in Washington or Idaho:
  2709.          
  2710.          select distinct code 
  2711.          from manu 
  2712.          where mst = 'WA' or mst = 'ID'; 
  2713.          
  2714.          code  
  2715.          ----  
  2716.          GC    
  2717.          DD    
  2718.          NM    
  2719.           
  2720.          3 rows selected 
  2721.  
  2722.  
  2723.  
  2724.  
  2725.                                 SELECT-8
  2726.  
  2727.  
  2728.  
  2729.          
  2730.          Then we must find all the products in the other states that are
  2731.          not in the above set of three products. We end up with the
  2732.          following which finds that there are not any products
  2733.          manufactured in the other states that are not manufactured in
  2734.          Washington and Idaho.
  2735.          
  2736.          select distinct code 
  2737.          from manu 
  2738.          where code != 'NM' and code != 'GC' and code != 'DD'; 
  2739.           
  2740.          no rows found
  2741.          
  2742.          Well, you may ask, what IS the select statement to find the
  2743.          products not manufactured in Washington or Idaho? Not so fast.
  2744.          Actually, it would require a subquery that is covered in the
  2745.          section on subqueries. 
  2746.          
  2747.          If I may digress...
  2748.          
  2749.          I know. Just a few pages into the basics of the select statement
  2750.          and you are ready to give up. Remember, the logic of SQL is not
  2751.          all that difficult. The main problem is that you do not think
  2752.          critically about the meaning of sentences. Much has been written
  2753.          about the problems of communication between humans and computers.
  2754.          Most of the problems are actually caused by the end-user and the
  2755.          "computer expert". I put the burden on the "computer expert" who
  2756.          should know enough about English to note the subtlties and
  2757.          ambiguities of what the end-user is requesting. This is just as
  2758.          relevant for interpreting queries as it is building complete
  2759.          application systems.
  2760.          
  2761.          Meanwhile, back at the ranch...
  2762.          
  2763.          Normally, if a condition is true, we add that row to the table.
  2764.          By using the not, if the condition is false, we add the row to
  2765.          the table. On the first row of the select statement to find
  2766.          products manufactured outside Idaho and Washington, the state is
  2767.          CA. It was selected because it is not equal to WA or ID. Just as
  2768.          we can express the same thought in English more than one way, we
  2769.          can do the same with SQL. We could have written:
  2770.          
  2771.          select * 
  2772.          from manu 
  2773.          where mst != 'WA' and mst != 'ID'; 
  2774.          OR 
  2775.          select * 
  2776.          from manu 
  2777.          where not (mst = 'WA') and not (mst = 'ID'); 
  2778.  
  2779.  
  2780.  
  2781.  
  2782.  
  2783.          
  2784.                                 SELECT-9
  2785.  
  2786.  
  2787.  
  2788.          
  2789.          What if we would have written or instead of and in the two
  2790.          examples above? Every row would have been selected. No matter
  2791.          what the state is, it would either be not equal to WA or not
  2792.          equal to ID! 
  2793.          
  2794.          Remember that whenever you use the not, you must enclose the
  2795.          expression in parentheses. 
  2796.          
  2797.          You can have as many ands and ors in a query as you need. What if
  2798.          you wanted all products in Washington and Idaho with defects less
  2799.          than 16%?  
  2800.          
  2801.          select * 
  2802.          from manu 
  2803.          where (mst='WA' or mst='ID') and defects < 16; 
  2804.           
  2805.          date     code mst defects qty  
  2806.          -------- ---- --- ------- ---  
  2807.          02/07/87 GC   ID       12  15  
  2808.          02/01/87 GC   ID        0  55  
  2809.          02/02/87 NM   WA       15  25  
  2810.           
  2811.          3 rows selected 
  2812.          
  2813.          We find that only two products are selected. Notice that
  2814.          parentheses enclose the or expression. This is because without
  2815.          the parentheses the and expression would have been executed
  2816.          first. Remember that without parentheses the ands are processed
  2817.          first and the ors are processed next. Not inserting the
  2818.          parentheses would have changed the meaning considerably:
  2819.          
  2820.          select * 
  2821.          from manu 
  2822.          where mst='WA' or mst='ID' and defects < 16; 
  2823.           
  2824.          date     code mst defects qty  
  2825.          -------- ---- --- ------- ---  
  2826.          02/07/87 GC   ID       12  15  
  2827.          02/01/87 GC   ID        0  55  
  2828.          02/03/87 DD   WA       22  46  
  2829.          02/02/87 NM   WA       15  25  
  2830.           
  2831.          4 rows selected 
  2832.          
  2833.          The above query is the equivalent of saying, "List all products
  2834.          in Washington and list all products in Idaho with defects under
  2835.          16%."  Another way of stating it is "In looking through all the
  2836.          products, if a product is manufactured in Washington or an Idaho
  2837.          product has under 16% defects, add it to the list." The important
  2838.          concept is that the 16% defects relates to the products
  2839.          manufactured in Idaho, not all products. 
  2840.          
  2841.  
  2842.  
  2843.                                 SELECT-10
  2844.  
  2845.  
  2846.  
  2847.  
  2848.          
  2849.          SETS (used with constants) 
  2850.          
  2851.          Instead of relating items one at a time, we can relate groups of
  2852.          items at once. These groups are called sets. They are enclosed
  2853.          parentheses and the constants are separated by commas. 
  2854.          
  2855.          You saw above how you can use =, !=, <, >, <=, and >=. We can
  2856.          expand that concept to include any and all.  This chapter has a
  2857.          brief introduction to sets.  Sets are the main topic of the
  2858.          chapters on subqueries where instead of using constants in sets,
  2859.          we use select statements.
  2860.          
  2861.          ANY 
  2862.          
  2863.          As you will see, the use of the any with constants is simply a
  2864.          short-hand method of writing a series of comparisons with ors.
  2865.          The any may be combined with any of the comparison operators:
  2866.          =any  equal to any 
  2867.          >any  greater than any 
  2868.          <any  less than any 
  2869.          !=any  not equal to any 
  2870.          >=any  greater than or equal to any 
  2871.          <=any  less than or equal to any 
  2872.          
  2873.          Although there are many possible combinations with any, very few
  2874.          are of any use. With constants, the only one that is typically
  2875.          used is =any. 
  2876.          
  2877.          Let's rewrite the query concerning all products manufactured in
  2878.          Washington or Idaho:
  2879.          
  2880.          select distinct code 
  2881.          from manu 
  2882.          where mst =any ('WA','ID'); 
  2883.          code  
  2884.          ----  
  2885.          GC    
  2886.          DD    
  2887.          NM    
  2888.           
  2889.          3 rows selected 
  2890.          
  2891.          Translated into English it would be: Select the description from
  2892.          the manufacturing table where the state of manufacture is equal
  2893.          to any of the following - WA or ID. 
  2894.          We can use numbers too. What if you wanted to know the basic
  2895.          information on products that have a percentage of defects of 12%
  2896.          or 15%?
  2897.  
  2898.  
  2899.  
  2900.          
  2901.  
  2902.                                  SELECT-11
  2903.  
  2904.  
  2905.  
  2906.          select date, code, mst 
  2907.          from manu 
  2908.          where defects =any (12,15); 
  2909.          date     code mst  
  2910.          -------- ---- ---  
  2911.          02/07/87 GC   ID   
  2912.          02/02/87 NM   WA   
  2913.          02/04/87 DD   AZ   
  2914.          02/04/87 DD   CA   
  2915.           
  2916.          4 rows selected 
  2917.          
  2918.          IN 
  2919.          
  2920.          An equivalent to =any is in. The above query could be translated:
  2921.          
  2922.          select date, code, mst
  2923.          from manu  
  2924.          where defects in (12,15);  
  2925.          
  2926.          The above two queries could be translated into a query without a
  2927.          set:
  2928.          
  2929.          select date, code, mst 
  2930.          from manu 
  2931.          where defects = 12 or defects = 15; 
  2932.          
  2933.          ALL
  2934.          
  2935.          The all can be combined with all the comparison operators just as
  2936.          was seen with the any. The only common use with constants occurs
  2937.          with the !=all which allows us to exclude a set of data. In the
  2938.          following statement we show information on products manufactured
  2939.          outside of Washington and Idaho.
  2940.          
  2941.          select date, code, mst 
  2942.          from manu 
  2943.          where mst !=all('WA','ID'); 
  2944.           
  2945.          date     code mst  
  2946.          -------- ---- ---  
  2947.          02/02/87 NM   CA   
  2948.          02/04/87 DD   AZ   
  2949.          02/04/87 DD   CA   
  2950.          02/06/87 GC   AZ   
  2951.           
  2952.          4 rows selected 
  2953.          
  2954.          Although we translated =any as "equal to any item in the set",
  2955.          !=all is usually translated as "not equal to any item in the
  2956.          set". Another way to state the above query is to find products
  2957.          manufactured in a state that is not any of the following -
  2958.          Washington and Idaho.  Confusing, isn't it??  Just remember that
  2959.  
  2960.  
  2961.                                 SELECT-12
  2962.  
  2963.  
  2964.  
  2965.  
  2966.  
  2967.          
  2968.          !=all is the opposite of =any.  This ambiguity in English is
  2969.          related to a riddle I heard Theo ask on the Cosby show:  "What
  2970.          two coins add up to 30 cents and one of them is not a quarter?" 
  2971.          The answer, of course, is a nickel and a quarter.  The nickel is
  2972.          not a quarter which satisfies the rule.  Theo was applying a
  2973.          !=any to the set when a typical listener would think that !=all
  2974.          was actually meant.
  2975.          
  2976.          BETWEEN constant AND constant 
  2977.          An easy way to select a range of values is use the between/and
  2978.          component of the where clause. The following shows how to find
  2979.          the basic information for products with defects between 12% and
  2980.          16%: 
  2981.          
  2982.          select date, code, mst, defects 
  2983.          from manu 
  2984.          where defects between 12 and 16; 
  2985.           
  2986.          date     code mst defects  
  2987.          -------- ---- --- -------  
  2988.          02/07/87 GC   ID       12  
  2989.          02/02/87 NM   WA       15  
  2990.          02/04/87 DD   AZ       12  
  2991.          02/04/87 DD   CA       15  
  2992.           
  2993.          4 rows selected 
  2994.          
  2995.          Notice that when we use the between, we include the 12 and the
  2996.          16. Unfortunately, the word "between" can have more than one
  2997.          meaning in standard English. If you say "The treasure is located
  2998.          between San Francisco and Walnut Creek", you would assume that
  2999.          the treasure is not IN San Francisco or Walnut Creek. That is,
  3000.          you would not include the end points - San Francisco or Walnut
  3001.          Creek. If you say "This insurance plan is available for those
  3002.          people between the ages of 18 and 65", you would assume that if
  3003.          you are 18 or 65, you are eligible. That is, you would include
  3004.          the end points. In English, when the word between is used with
  3005.          numbers, the end points are included. In other cases, the end
  3006.          points are not included. Silly, isn't it!! Just remember that in
  3007.          SQL, between includes the end points.
  3008.  
  3009.  
  3010.  
  3011.  
  3012.  
  3013.  
  3014.  
  3015.  
  3016.  
  3017.  
  3018.  
  3019.          
  3020.                                   SELECT-13
  3021.  
  3022.  
  3023.  
  3024.  
  3025.          IMPORTANCE OF THE NULL (UNKNOWN VALUE)
  3026.          Now that you understand the basics of how the where clause works,
  3027.          we need to complicate it a bit with considering the unknown value
  3028.          (the null).  If the where clause evaluates to TRUE, then the row
  3029.          is displayed, otherwise, it is not displayed.  In most non-SQL
  3030.          databases, the where clause only evaluates to TRUE and FALSE.  In
  3031.          SQL we have TRUE, FALSE, and UNKNOWN.  We can use truth tables to
  3032.          show the relationship.
  3033.          
  3034.           AND  |TRUE  FALSE  UNK     OR   |TRUE  FALSE  UNK
  3035.           ----------------------     -----------------------
  3036.           TRUE |TRUE  FALSE  UNK     TRUE |TRUE  TRUE   TRUE
  3037.           FALSE|FALSE FALSE  FALSE   FALSE|TRUE  FALSE  UNK   
  3038.           UNK  |UNK   FALSE  UNK     UNK  |TRUE  UNK    UNK
  3039.          
  3040.           NOT
  3041.           ------------
  3042.           TRUE  |FALSE
  3043.           FALSE |TRUE
  3044.           UNK   |UNK
  3045.          
  3046.          This can cause some confusing results until you are used to it.
  3047.          For example, there are nine rows in the manu table.  However, the
  3048.          two queries below result in a total of only eight rows.  
  3049.          
  3050.          select *
  3051.          from manu
  3052.          where defects = 15;
  3053.          
  3054.          date     code mst defects qty 
  3055.          -------- ---- --- ------- --- 
  3056.          02/02/87 NM   WA       15  25 
  3057.          02/04/87 DD   CA       15  25 
  3058.          
  3059.          2 rows selected
  3060.          
  3061.          select *
  3062.          from manu
  3063.          where defects != 15;
  3064.          
  3065.          date     code mst defects qty 
  3066.          -------- ---- --- ------- --- 
  3067.          02/07/87 GC   ID       12  15 
  3068.          02/01/87 GC   ID        0  55 
  3069.          02/02/87 NM   CA       17  93 
  3070.          02/03/87 DD   WA       22  46 
  3071.          02/04/87 DD   AZ       12  25 
  3072.          02/06/87 GC   AZ        4  43 
  3073.          
  3074.          6 rows selected
  3075.          
  3076.  
  3077.  
  3078.  
  3079.                                   SELECT-14
  3080.  
  3081.  
  3082.          At first glance you would think that defects are either equal to
  3083.          fifteen or not equal to fifteen.  However there is one instance
  3084.          where defects has an unknown value so it is not displayed.
  3085.          Intuitively you may be saying to yourself that since an unknown
  3086.          value is not equal to fifteen, it should be displayed, but that
  3087.          is not the way it works.  If you look at both the AND truth table
  3088.          and OR truth table, there is no difference between the false and
  3089.          the unknown since a result of FALSE or UNKNOWN will prevent a row
  3090.          from being displayed.  However, in the truth table for NOT, there
  3091.          is a difference. The opposite of unknown is still unknown.
  3092.          
  3093.          AGGREGATE FUNCTIONS - AVG, MIN, MAX, SUM, COUNT 
  3094.          
  3095.          All the aggregate functions operate on numeric columns except
  3096.          count which will operate on any columns. 
  3097.          
  3098.          SYNTAX: 
  3099.          
  3100.          AVG([DISTINCT] column_name) 
  3101.          MIN(column_name) 
  3102.          MAX(column_name) 
  3103.          SUM([DISTINCT]column_name) 
  3104.          COUNT([DISTINCT]column_name) 
  3105.          
  3106.          SUMMARY 
  3107.          
  3108.          AVG - without DISTINCT: The average of all values in a column
  3109.          AVG - with DISTINCT: The average of non-duplicate values 
  3110.          MIN - The minimum value in the column 
  3111.          MAX - The maximum value in the column 
  3112.          SUM - without DISTINCT: The sum of all values in the column 
  3113.          SUM - with DISTINCT: The sum of non-duplicate values 
  3114.          COUNT - without DISTINCT: the number of column values 
  3115.          COUNT - with DISTINCT: the number of non-duplicate column values 
  3116.          
  3117.          Notice that some of the functions have the distinct option. This
  3118.          will exclude all rows in the table which have duplicate column
  3119.          values. The follwing two examples show the difference. 
  3120.          select count(mst) 
  3121.          from manu; 
  3122.          CNT(mst)  
  3123.          --------  
  3124.                 9  
  3125.           
  3126.          9 rows selected 
  3127.          
  3128.          The above query can be translated as: 
  3129.          How many rows have values in the mst column? There are a total of
  3130.          9. This is not very useful because every row is going to have an
  3131.          entry for mst. The answer will always be the total number of rows
  3132.          in the table since the mst column was created with the "not null"
  3133.          constraint. 
  3134.          The following query is typically more useful. It answers the
  3135.          question: "How many different states manufacture products?"
  3136.  
  3137.  
  3138.                                   SELECT-15
  3139.  
  3140.  
  3141.          select count(distinct mst) 
  3142.          from manu; 
  3143.          CNT(mst)  
  3144.          --------  
  3145.                 4  
  3146.           
  3147.          Typically, when you are using a column name with count, you will
  3148.          modify it with distinct. When querying a table with a multiple
  3149.          part key, all columns will have the possibility of having
  3150.          duplicate entries. This is the case with the manu table with a
  3151.          key of date, code, mst. With such tables, you will modify the
  3152.          column name with distinct. When you query a table that has a key
  3153.          based on a single column, the only column you can be assured of
  3154.          not having duplicates is the key column. If you want to count all
  3155.          the rows in the table, there is a short-cut:
  3156.          
  3157.          select count(*) 
  3158.          from manu; 
  3159.          
  3160.          CNT(*)  
  3161.          ------  
  3162.               9  
  3163.           
  3164.          At this point, you may ask what use the column name is without
  3165.          the distinct modifier? It is that wonderful concept of the null
  3166.          that has been touched on before and will be discussed in more
  3167.          detail about the other functions. The count function will not
  3168.          count rows that have a null value in the column you are counting.
  3169.          Actually, all the functions skip rows with nulls. In the employee
  3170.          table, not everyone has a corresponding manager number. The entry
  3171.          for employees without a manager is a null (actually only the
  3172.          president doesn't have a manager in this table). The following
  3173.          tells us how many employees have managers.
  3174.          
  3175.          select count(mgrnum) 
  3176.          from emp; 
  3177.          CNT(mgrnum)  
  3178.          -----------  
  3179.                   14  
  3180.           
  3181.          15 rows selected 
  3182.          
  3183.          This can be contrasted with the following query. Notice how
  3184.          simply adding the word "distinct" completely changes the meaning
  3185.          of the query to finding how many managers there are in the
  3186.          company.
  3187.          
  3188.          select count(distinct mgrnum) 
  3189.          from emp; 
  3190.          
  3191.          CNT(mgrnum)  
  3192.          -----------  
  3193.                    7  
  3194.  
  3195.  
  3196.           
  3197.                                 SELECT-16
  3198.  
  3199.  
  3200.  
  3201.          
  3202.          The functions also can be used with the where clause. The
  3203.          following shows how many batches of product DD are in the table.
  3204.          
  3205.          select count(*) 
  3206.          from manu 
  3207.          where code = 'DD'; 
  3208.          
  3209.          CNT(*)  
  3210.          ------  
  3211.               4  
  3212.           
  3213.          4 rows selected 
  3214.           
  3215.          Before we discuss the other functions, let's look at the
  3216.          percentage of defects in Idaho:
  3217.          
  3218.          select * 
  3219.          from manu 
  3220.          where mst = 'ID'; 
  3221.          date     code mst defects qty  
  3222.          -------- ---- --- ------- ---  
  3223.          02/07/87 GC   ID       12  15  
  3224.          02/01/87 GC   ID        0  55  
  3225.          02/02/87 DD   ID           25  
  3226.           
  3227.          3 rows selected 
  3228.          
  3229.          In Idaho, the are three entries: 12, 0, and "nothing". This
  3230.          "nothing" is called a null. Nulls are important in relational
  3231.          data bases. It does not mean zero or blank. A null means that the
  3232.          data element is not relevant in the row. In our case, product DD
  3233.          is not tracked for defects like other products. This isn't to say
  3234.          that there are zero defects. It just means we have no entry.
  3235.          Because there are only two valid entries for defects in Idaho,
  3236.          the functions will not take into account the null data element.
  3237.          The average percentage of defects will be 12/2 = 6, not 12/3 = 4:
  3238.          
  3239.          select avg(defects) 
  3240.          from manu 
  3241.          where mst='ID'; 
  3242.          AVG(defects)  
  3243.          ------------  
  3244.                     6  
  3245.           
  3246.          3 rows selected 
  3247.          
  3248.          
  3249.          The sum function totals the numeric values in the column(s)
  3250.          requested. Below we sum the number of product DD that has been
  3251.          manufactured.
  3252.  
  3253.  
  3254.  
  3255.  
  3256.                                 SELECT-17
  3257.  
  3258.          
  3259.          select sum(qty) 
  3260.          from manu 
  3261.          where code = 'DD'; 
  3262.          
  3263.          SUM(qty)  
  3264.          --------  
  3265.               121  
  3266.           
  3267.          4 rows selected 
  3268.          
  3269.          More than one function can be used in a query: 
  3270.          
  3271.          
  3272.          select avg(qty), sum(qty) 
  3273.          from manu 
  3274.          where code = 'DD'; 
  3275.           
  3276.          AVG(qty) SUM(qty)  
  3277.          -------- --------  
  3278.                30      121  
  3279.           
  3280.          4 rows selected 
  3281.          
  3282.          
  3283.          Except for the group by clause, you cannot retrieve column values
  3284.          and functions based on column values in the same query. 
  3285.          
  3286.          COLUMN CALCULATIONS (*, /, +, -)
  3287.          
  3288.          Numeric columns can be used in calculations.  The following
  3289.          symbols are used:
  3290.          
  3291.          * = multiplication
  3292.          / = division
  3293.          + = addition
  3294.          - = subtraction
  3295.          The following shows the values if the defects doubled.
  3296.          
  3297.          select code, defects*2
  3298.          from manu;
  3299.          
  3300.          code      defects 
  3301.          ---- ------------ 
  3302.          GC             24 
  3303.          GC              0 
  3304.          NM             34 
  3305.          DD                
  3306.          DD             44 
  3307.          NM             30 
  3308.          DD             24 
  3309.          DD             30 
  3310.          GC              8 
  3311.          
  3312.          9 rows selected
  3313.  
  3314.  
  3315.                                 SELECT-18
  3316.  
  3317.  
  3318.  
  3319.          In a calculation, multiplication and division are processed
  3320.          first, then the addition and subtraction.  The order of
  3321.          processing can be changed by adding parentheses.  Calculations
  3322.          within parentheses are calculated first.  More than one column
  3323.          can be used in a calulation and even the same column can be
  3324.          referenced more than once.  The column name for the calculation
  3325.          is taken from the first column name however you can rename the
  3326.          column (as shown in the full documentation).  The data type for
  3327.          the calculation is based on the first column in the calculation.
  3328.  
  3329.          Calculations can occur also in the where clause but THERE ARE
  3330.          LIMITATIONS.  First, expressions are evaluated from left to right
  3331.          (mult & divide not necessarily first). Second, you cannot use
  3332.          parentheses.
  3333.          
  3334.          GROUP BY, HAVING
  3335.  
  3336.          The group by clause is normally used with aggregate functions. 
  3337.          It has two operations:
  3338.          
  3339.          1.  It sorts by the column name.
  3340.          
  3341.          2.  The aggregate functions only operate based on the rows that
  3342.          have the same column name.  The functions in essence create
  3343.          sub-totals based on the column name.
  3344.          
  3345.          What if we wanted to know the average defects in each state:
  3346.          
  3347.          select mst, avg(defects) 
  3348.          from manu 
  3349.          group by mst; 
  3350.           
  3351.          mst AVG(defects)  
  3352.          --- ------------  
  3353.          AZ             8  
  3354.          CA            16  
  3355.          ID             6  
  3356.          WA            18  
  3357.           
  3358.          4 rows selected 
  3359.          
  3360.          
  3361.          The column name after the group by must exist on the line with
  3362.          the select.  After the select you can only have column names in
  3363.          the group by and aggregate functions - nothing else.
  3364.          You can restrict the group by clause with the having component.
  3365.          In SSQL you are allowed one simple selection that includes the =.
  3366.          <, <=, >, >=, and !=.  As with the group by, the column name
  3367.          in the having component must exist in the select clause.
  3368.          If you only wanted a list of states with defects over 10% you
  3369.          would use the following:
  3370.          
  3371.  
  3372.  
  3373.  
  3374.                                 SELECT-19
  3375.  
  3376.  
  3377.  
  3378.          
  3379.          select mst, avg(defects) 
  3380.          from manu 
  3381.          group by mst having avg(defects) > 10; 
  3382.           
  3383.          mst AVG(defects)  
  3384.          --- ------------  
  3385.          CA            16  
  3386.          WA            18  
  3387.           
  3388.          2 rows selected 
  3389.          
  3390.          The group by clause can have up to two fields.  This is when you
  3391.          want a group within a group.  
  3392.          The group by clause can be used with the where clause.  When we
  3393.          found the average defects in each state we could have modified it
  3394.          by excluding product DD. 
  3395.          The where clause and the having clause are related.  However, the
  3396.          where clause operates on the whole table without regard for the
  3397.          grouping.  The having clause operates on the data that has
  3398.          already been grouped.  Because of the way the having clause
  3399.          works, most of the time the having clause contains an operation
  3400.          with an aggregate function because that is the basic purpose of
  3401.          the group by clause.
  3402.          
  3403.          ORDER BY
  3404.          
  3405.          The order by clause sorts the output of the table based on the
  3406.          column name(s) listed.  The original table is not changed. As
  3407.          with the group by clause, the order by clause can be used with
  3408.          the where clause.  For example, the following query will produce
  3409.          a list of manufacturing information sorted by defects for those
  3410.          items manufactured in Idaho.
  3411.          
  3412.          select date, code, defects 
  3413.          from manu 
  3414.          where mst = 'ID' 
  3415.          order by defects; 
  3416.           
  3417.          date     code defects  
  3418.          -------- ---- -------  
  3419.          02/02/87 DD            
  3420.          02/01/87 GC         0  
  3421.          02/07/87 GC        12  
  3422.           
  3423.          3 rows selected 
  3424.          
  3425.          You also can produce a sort within a sort.  The following query
  3426.          will produce an ordered list of dates and within each date the
  3427.          state will be sorted.
  3428.  
  3429.  
  3430.  
  3431.  
  3432.          
  3433.                                 SELECT-20
  3434.  
  3435.  
  3436.  
  3437.  
  3438.          
  3439.          select date, mst, code, qty 
  3440.          from manu 
  3441.          order by date, mst; 
  3442.           
  3443.          date     mst code qty  
  3444.          -------- --- ---- ---  
  3445.          02/01/87 ID  GC    55  
  3446.          02/02/87 CA  NM    93  
  3447.          02/02/87 ID  DD    25  
  3448.          02/02/87 WA  NM    25  
  3449.          02/03/87 WA  DD    46  
  3450.          02/04/87 AZ  DD    25  
  3451.          02/04/87 CA  DD    25  
  3452.          02/06/87 AZ  GC    43  
  3453.          02/07/87 ID  GC    15  
  3454.           
  3455.          9 rows selected 
  3456.          
  3457.          The order by clause assumes ascending order.  You can reverse the
  3458.          order by finding out in the full documentation
  3459.          
  3460.          When there are two columns to sort, one can be in ascending order
  3461.          while the other is in descending order. 
  3462.          In the current version of SSQL, you are limited to two sort
  3463.          columns.
  3464.          
  3465.          REDIRECTTO file_name
  3466.          
  3467.          Normally the report that is produced by the select command is
  3468.          displayed on the monitor.  However, with the redirectto clause,
  3469.          the report can be redirected to a text file or to the printer. 
  3470.          The text file is in standard ASCII text.  That is, there are no
  3471.          special characters in it.  The file name you give it must not
  3472.          have an extension because SSQL adds the extension ".txt".  
  3473.          If you wanted all the Arizona products stored in a file called
  3474.          ARIZ.TXT and you wanted the report sorted by product code, you
  3475.          would use the following query.
  3476.          
  3477.          select * 
  3478.          from  manu 
  3479.          where mst='AZ' 
  3480.          redirectto ariz 
  3481.          order by code; 
  3482.          
  3483.          The output would be stored in a file called ARIZ.TXT on your
  3484.          default drive.  To display the file on the monitor, you would
  3485.          exit SSQL and the following at the DOS prompt:
  3486.          
  3487.          type ariz.txt
  3488.  
  3489.  
  3490.  
  3491.          
  3492.                                 SELECT-21
  3493.  
  3494.  
  3495.  
  3496.  
  3497.  
  3498.          
  3499.          You also can have the report sent to the printer.  Replace the
  3500.          file name with prn or lpt1.  Make sure the printer is on when you
  3501.          use this option.  
  3502.          
  3503.          UNION
  3504.          The union command allows you to combine output from two or 
  3505.          three different select statements.  The two select statements 
  3506.          must have exactly the same output format - the same number of 
  3507.          columns and each column in the corresponding tables must be of 
  3508.          the same data type.  For the following example, I assume that 
  3509.          there are two tables with the same structure as SALES called 
  3510.          SALESCA and SALESAZ however it is not necessary that the tables 
  3511.          have the same structure, just the output columns.  The 
  3512.          following statement will get the customer code and quantity 
  3513.          from SALESCA for those customers who have purchased over 50 of 
  3514.          a particular item.  The output will also contain all the 
  3515.          customers and corresponding quanitities from the SALESAZ table. 
  3516.          The output is sorted by customer code.
  3517.  
  3518.          select cc,qty
  3519.          from salesca
  3520.          where qty > 50
  3521.          union
  3522.          select cc,qty
  3523.          from salesaz
  3524.          order by cc;
  3525.          
  3526.  
  3527.  
  3528.  
  3529.  
  3530.  
  3531.  
  3532.  
  3533.  
  3534.  
  3535.  
  3536.  
  3537.  
  3538.  
  3539.  
  3540.  
  3541.  
  3542.  
  3543.  
  3544.  
  3545.  
  3546.  
  3547.  
  3548.  
  3549.  
  3550.  
  3551.                                   SELECT-22
  3552.  
  3553.  
  3554.  
  3555.          JOINING TABLES
  3556.          
  3557.          
  3558.          Often, the data we need exists in more than one table. In order
  3559.          to extract the data, we need to select the appropriate columns
  3560.          and join the tables. The type of join primarily discussed is
  3561.          called a natural join but for the sake of brevity I will just use
  3562.          the word "join" alone. 
  3563.          Whenever we join tables, there must normally be a common column.
  3564.          Hear the common column contains the customer code. It is not
  3565.          important that the column names are different. The important
  3566.          aspect is that they describe precisely the same thing - i.e.,
  3567.          they have a common domain.
  3568.          In SQL, the join operation normally has three basic components:
  3569.          
  3570.          SELECT column names - for duplicate names, precede the column
  3571.          name with the table name and a period as in prod.code
  3572.          
  3573.          FROM table names separated by commas
  3574.          
  3575.          WHERE common columns are set to be equal
  3576.          
  3577.          The following shows how to combine the sales information from 
  3578.          the sales table and the customer name from cust:
  3579.          
  3580.          select name, pc, qty 
  3581.          from sales, cust
  3582.          where cc=code;
  3583.  
  3584.          name            pc qty 
  3585.          --------------- -- --- 
  3586.          Organomice      MW  23 
  3587.          Organomice      DD  34 
  3588.          QuarkCo         AB   2 
  3589.          Marswarp        MW  81 
  3590.          Technoharps     NM   3 
  3591.          Technoharps     MW  41 
  3592.          Technoharps     GC  33 
  3593.          Compugorp       MW 125 
  3594.          Compugorp       MW 947 
  3595.          Compugorp       DD 452 
  3596.          Compugorp       NM  32 
  3597.          
  3598.          name            pc qty 
  3599.          --------------- -- --- 
  3600.          QuarkCo         GC 845 
  3601.          Organomice      NM  45 
  3602.          Organomice      MW  73 
  3603.          Compugorp       GC 127 
  3604.          Compugorp       DD  32 
  3605.          
  3606.          16 rows selected
  3607.  
  3608.          MUCH MORE IN THE FULL DOCUMENTATION
  3609.  
  3610.                                 JOIN-1
  3611.  
  3612.  
  3613.  
  3614.  
  3615.          SIMPLE SUBQUERIES
  3616.          
  3617.          OVERVIEW
  3618.          
  3619.          In previous chapters, any comparison operation was done with
  3620.          actual values or a column name. Whenever we used the =, !=, <,
  3621.          > , <=, >=, in, all, or any there was a column name on one
  3622.          side and either a column name, an actual value or for the in,
  3623.          all, or any, a set of actual values on the other side. The
  3624.          problem is that often the values on right side of the comparison
  3625.          operation are not known until the query is made. In such cases
  3626.          the right side of the comparison is stated as a separate select
  3627.          command that is called a subquery. The result of the subquery is
  3628.          obtained first and the data is passed back to the previous query.
  3629.          Many queries can be linked together in this way.
  3630.          Another way of looking at it is that often, we need to break down
  3631.          our problem into more than one query. Each query will have its
  3632.          own select. We need subqueries when, in analyzing a problem, we
  3633.          find that we need information from one query before we can
  3634.          process another query. Sometimes we can either join tables or use
  3635.          a subquery. Typically, subqueries retrieve information more
  3636.          rapidly than joining tables.
  3637.          
  3638.          SUBQUERIES WITH =, !=, <, >, <=, AND >=
  3639.          
  3640.          These operators can be used with any subquery that produces a
  3641.          single value. It is commonly used with subqueries that contain  
  3642.          an aggregate function. The following shows the customer codes and
  3643.          quantity for those customers who have purchased more of product
  3644.          MW in a single purchase than the total purchased by customer BB.
  3645.          
  3646.          select  cc, qty
  3647.          from  sales
  3648.          where  pc = 'MW'
  3649.          and  qty >
  3650.            (select  sum(qty)
  3651.            from  sales
  3652.            where  cc = 'BB'
  3653.            and  pc = 'MW');
  3654.          
  3655.          cc qty
  3656.          -- ---
  3657.          EE  81
  3658.          AA 125
  3659.          AA 947
  3660.          ZZ  73
  3661.  
  3662.          4 rows selected
  3663.          
  3664.          This method also can be used with any column where we know it can
  3665.          only return a single value.
  3666.  
  3667.  
  3668.  
  3669.                                      SUB-1
  3670.  
  3671.  
  3672.  
  3673.  
  3674.  
  3675.          SET EXPRESSIONS - IN
  3676.          
  3677.          In the section on simple select statements, we used any and in
  3678.          with constants (actual values). The true power of the set
  3679.          expressions involves its use in subqueries. 
  3680.          
  3681.          We will first compare a join with a subquery. If we wanted to get
  3682.          product codes for all products purchased by customers in
  3683.          California, we could join the SALES table and the customer table.
  3684.          
  3685.          select  distinct pc
  3686.          from  sales, cust
  3687.          where  cc = code
  3688.          and  st = 'CA';
  3689.          
  3690.          pc 
  3691.          -- 
  3692.          MW 
  3693.          
  3694.          1 row selected
  3695.          
  3696.          
  3697.          Another approach would be to break it down into two queries. We
  3698.          know that we need a list of product codes from the SALES table.
  3699.          The SALES table has customer codes but it doesn't have
  3700.          information on states. Another way to state the problem is that
  3701.          we want all product codes from the SALES table where the customer
  3702.          code is in the set of customers that are in California. find the
  3703.          customers in California.
  3704.          
  3705.          
  3706.          select  distinct pc
  3707.          from  sales
  3708.          where  cc in
  3709.            (select  code
  3710.            from  cust
  3711.            where  st='CA');
  3712.          
  3713.          pc 
  3714.          -- 
  3715.          MW 
  3716.          
  3717.          1 row selected
  3718.          
  3719.          THERE IS MUCH MORE IN THE FULL DOCUMENTATION
  3720.         
  3721.         
  3722.         
  3723.         
  3724.         
  3725.         
  3726.         
  3727.          
  3728.                                      SUB-3
  3729.  
  3730.  
  3731.          CORRELATED SUBQUERIES 
  3732.           
  3733.          OVERVIEW 
  3734.           
  3735.          A correlated subquery is a subquery that refers to a table 
  3736.          OUTSIDE the subquery. This causes correlated subqueries to be 
  3737.          processed differently from simple subqueries. Simple subqueries 
  3738.          are processed once, and the result is passed back to the main 
  3739.          query. The correlated subquery is processed for every row in the
  3740.          
  3741.          main query. This allows us to process a subquery based on a 
  3742.          particular column value in each row of the main query. It can get
  3743.          even more complex with multiple levels of subqueries. With simple
  3744.          subqueries, each subquery is isolated and passes back a single 
  3745.          set of values to the previous level. With correlated subqueries,
  3746.          each subquery can depend on more than one of the previous levels.
  3747.          
  3748.          If we are working on the SALES table we can have a subquery that
  3749.          is processed for each customer or product, not the whole table. 
  3750.          Another way of looking at it is that for each row in the main 
  3751.          query we can take a value, say customer code, and process a 
  3752.          subquery based on the customer. The value it produces for the 
  3753.          customer can be compared to another value in the same row in the
  3754.          main query. We will do that in the next section when we want to 
  3755.          display the complete row in a SALES table for every customer 
  3756.          purchase where the quantity purchased is above average FOR THAT 
  3757.          CUSTOMER. With a simple subquery we could only compare customer 
  3758.          purchases with average SALES for the whole table or a SINGLE 
  3759.          customer. Using the correlated subquery is sometimes like being 
  3760.          able to compare values to a subquery with a GROUP BY.  
  3761.           
  3762.          Often, we just want to test for the existence of a row in a 
  3763.          subquery based on a value in the first level of the query so SQL
  3764.          has the exists predicate. By using the correlated subquery we can
  3765.          create some rather complex tests on sets of data.  
  3766.           
  3767.          AGGREGATE FUNCTIONS 
  3768.           
  3769.          Let's contrast the correlated subquery with a simple subquery 
  3770.          that is produced below: 
  3771.           
  3772.          select * 
  3773.          from sales  
  3774.          where qty > 
  3775.           (select avg(qty) 
  3776.           from sales); 
  3777.           
  3778.          date     bc cc sn pc qty  
  3779.          -------- -- -- -- -- ---  
  3780.          04/01/87 1A AA 10 MW 947  
  3781.          04/08/87 1A AA 10 DD 452  
  3782.          04/08/87 1B DD 11 GC 845  
  3783.           
  3784.          3 rows selected 
  3785.  
  3786.  
  3787.                                      COR-1
  3788.  
  3789.  
  3790.  
  3791.  
  3792.           
  3793.          It solves the problem for displaying the complete row in the 
  3794.          SALES table for every customer purchase where the quantity 
  3795.          purchased is above average (based on the complete SALES table). 
  3796.           
  3797.          The way a simple subquery works is that it is processed ONCE, and
  3798.          
  3799.          the result is passed back to the main query. 
  3800.          Now let's change the query a little bit. Now we want to display 
  3801.          the complete row in a SALES table for every customer purchase 
  3802.          where the quantity purchased is above average FOR THAT CUSTOMER.
  3803.          
  3804.          Without knowing much else, it should at least be obvious that we
  3805.          need to process the subquery more than once since the query 
  3806.          states that we need an average for EACH customer to compare the 
  3807.          quantity to. Actually, the correlated subquery is more 
  3808.          inefficient than this. Usually, for EVERY row in the first table,
  3809.          
  3810.          the subquery is processed: 
  3811.           
  3812.          select * 
  3813.          from sales sales1 
  3814.          where qty > 
  3815.           (select avg(qty) 
  3816.           from sales 
  3817.           where cc = sales1.cc); 
  3818.           
  3819.          date     bc cc sn pc qty  
  3820.          -------- -- -- -- -- ---  
  3821.          04/12/87 1B BB 27 MW  41  
  3822.          04/15/87 2A BB 33 GC  33  
  3823.          04/01/87 1A AA 10 MW 947  
  3824.          04/08/87 1A AA 10 DD 452  
  3825.          04/08/87 1B DD 11 GC 845  
  3826.          04/01/87 2A ZZ 12 NM  45  
  3827.          04/08/87 2A ZZ 12 MW  73  
  3828.           
  3829.          7 rows selected 
  3830.           
  3831.           
  3832.          On the last line of the subquery we set cc = sales1.cc. This 
  3833.          means that for every row in the first table, it will take the 
  3834.          customer code (sales1.cc), and compute the average quantity for 
  3835.          that customer. It will then determine whether the quantity is 
  3836.          greater than the average quantity. We had to use the alias sales1
  3837.          to distinguish one SALES table from the other. Following any 
  3838.          table name we can rename it to avoid confusion or ambiguity. The
  3839.          alias can be any name that would be a valid table name. 
  3840.           
  3841.  
  3842.  
  3843.  
  3844.  
  3845.  
  3846.                                      COR-2
  3847.  
  3848.  
  3849.  
  3850.  
  3851.  
  3852.          EXISTS/NOT EXISTS  
  3853.           
  3854.          The exists predicate tests for the existence of a row selected in
  3855.          a subquery BASED ON DATA IN THE CURRENT QUERY (and possibly outer
  3856.          queries). We do not care what data selected happens to be in the
  3857.          subquery, we just want to know that it exists. The following 
  3858.          shows how to find the names of products in the SALES table. 
  3859.           
  3860.          select descrip 
  3861.          from prod 
  3862.          where exists 
  3863.           (select * 
  3864.           from sales 
  3865.           where code = pc); 
  3866.           
  3867.          descrip          
  3868.          ---------------  
  3869.          Megawamp         
  3870.          Gigasnarf        
  3871.          Nanomouse        
  3872.          Dynamic Disk     
  3873.           
  3874.          4 rows selected 
  3875.           
  3876.          THE TRUE VALUE OF THE CORRELATED SUBQUERY CAN BE SEEN IN THE FULL
  3877.          DOCUMENTATION - ABOUT 40 PAGES!!! 
  3878.  
  3879.  
  3880.  
  3881.  
  3882.  
  3883.  
  3884.  
  3885.  
  3886.  
  3887.  
  3888.  
  3889.  
  3890.  
  3891.  
  3892.  
  3893.  
  3894.  
  3895.  
  3896.  
  3897.  
  3898.  
  3899.  
  3900.  
  3901.  
  3902.  
  3903.  
  3904.  
  3905.                                      COR-3
  3906.  
  3907.  
  3908.  
  3909.  
  3910.  
  3911.  
  3912.          CONVERTING ENGLISH TO SQL
  3913.          
  3914.          OVERVIEW
  3915.          The previous chapters emphasized the point of view of SQL and
  3916.          understanding all the parts.  This chapter is more concerned with
  3917.          the point of view of a person wanting to solve queries using SQL. 
  3918.          There are three pieces of important information - understanding
  3919.          the database, understanding the query, and finally writing the
  3920.          SQL statement.
  3921.          
  3922.          ***************************************************************
  3923.          **  THE FULL DOCUMENTATION HAS THE SQL STATEMENTS AND        **
  3924.          **  EXPLANATIONS.  THE FOLLOWING WILL GIVE YOU A GOOD        **
  3925.          **  IDEA OF THE FLEXIBILITY OF SQL AND THE VARIETY OF        **
  3926.          **  PROBLEMS THAT IT CAN SOLVE  (IT SHOULD ALSO CONVINCE     **
  3927.          **  YOU TO REGISTER SO YOU CAN GET THE FULL DOCUMENTATION!)  **
  3928.          ***************************************************************
  3929.          
  3930.          UNDERSTANDING THE DATABASE
  3931.          In the chapter on the example databases, when a column was
  3932.          described, any other common columns in other tables was mentioned
  3933.          also.  This commonality allows us to work with information that
  3934.          spans tables.  There are other ways to group this information
  3935.          that may be useful.  The first way is to group them by the common
  3936.          domains.  The Monolith Manufacturing database has four basic
  3937.          domains (I do not count date): state, product code, employee
  3938.          number, and branch code. We can group them as follows:
  3939.          
  3940.          
  3941.          
  3942.          NAME TABLES COLUMN NAME
  3943.          STATE CUST st
  3944.           MANU mst
  3945.           BRANCH st
  3946.          
  3947.          PRODUCT MANU code
  3948.          CODE PROD code
  3949.           SALES pc
  3950.          
  3951.          EMPLOYEE SALES sn
  3952.          NUMBER EMP enum
  3953.           EMP mgrnum
  3954.           BRANCH mgrnum
  3955.          
  3956.          BRANCH BRANCH code
  3957.          CODE SALES bc
  3958.          
  3959.          Whenever we join tables or use a subquery, we must use common
  3960.          domains so the above information is essential.
  3961.          
  3962.          
  3963.          
  3964.                                     ENG-1
  3965.  
  3966.  
  3967.  
  3968.          GENERAL APPROACH TO SOLVING THE QUERY
  3969.          (This section is only in the full documentation)
  3970.  
  3971.          The Numbering Scheme For Queries
  3972.          Each key word or words is given a number from 1 to 4.  The number
  3973.          is further divided into numbers to the right of a decimal as to
  3974.          how the keywords are used.  Each example is  followed by a
  3975.          letter.  Query 3.2B corresponds to the second example(B) for the
  3976.          second use(2) of keyword 3.  Queries preceded by an 'N' means
  3977.          that it is a negated version of the query.  There may be more
  3978.          than one way to negate it so the version is in the right-most
  3979.          position as in: N3.2B.2.
  3980.          
  3981.          1 - AND
  3982.          The and is used to refer to information in columns.  In its
  3983.          simplest form, it is used to select which columns to display.  It
  3984.          is commonly used to determine which rows to display.  As you will
  3985.          see from the examples, there are many ways to translate and into
  3986.          SQL.
  3987.          1.1 Used to describe which columns to display.
  3988.          1.1A List the date of manufacture, product code, and quantity.
  3989.          
  3990.          1.2 Used when you need or:
  3991.          1.2A List the rating for Organomice and Compugorp.
  3992.          1.2B List the dates of purchase for products MW and NM.
  3993.          
  3994.          1.3 Ambiguous 
  3995.          Solution could be an or a more complex construction.  Usually the
  3996.          ambiguity arises because we are referring to sets of values
  3997.          instead of single values.  The answer could be a union of the two
  3998.          sets (or) or an intersection (subquery). Query 1.2A asks for
  3999.          rating which will be a single value for each customer.  In query
  4000.          1.3A, we are concerned with two sets of values - products
  4001.          manufactured in Washington and products manufactured in Idaho. 
  4002.          Just because it refers to sets of values does not necessarily
  4003.          mean that it is ambiguous.  Query 1.2B refers to sets but (in my
  4004.          opinion) it is not ambiguous.  Contrast 1.2B with 1.3B which I
  4005.          think is ambiguous.
  4006.          1.3A  List the products manufactured in Washington and Idaho.
  4007.          1.3B  List the dates when products MW and NM were purchased.
  4008.          
  4009.          1.4. Not Ambiguous
  4010.          However the and translates into a more complex SQL statement. 
  4011.          What we are after here is often an intersection of the sets as
  4012.          you can see in 1.4A and 1.4B.  That is, we want values common to
  4013.          all sets. An intersection can normally be achieved through a
  4014.          series of subqueries.
  4015.          1.4A  List the products manufactured in both Washington and
  4016.          Idaho.
  4017.          1.4B  List the dates when all of the following products were
  4018.          purchased: MW, NM, and GC.
  4019.          1.4C  List the customers who are from Arizona and have purchased
  4020.          product MW.  
  4021.  
  4022.  
  4023.                                     ENG-2
  4024.  
  4025.  
  4026.  
  4027.  
  4028.  
  4029.          1.5 Definitely need an and in the where clause.  
  4030.          The and is used when we want to test two or more different
  4031.          columns in a row within a single table.
  4032.          1.5A  List the customers who are from Arizona and have a rating
  4033.          over 15.
  4034.          
  4035.          1.6 Implied and
  4036.          1.6A  List the Arizona customers who have a rating over 15.
  4037.          Same as 1.5A
  4038.          1.6B  List the Arizona customers who have purchased product MW.
  4039.          Same as 1.4C
  4040.          
  4041.          OR
  4042.          2.1  Normally translates to or:
  4043.          2.1A  List the customer codes for customers who have purchased MW
  4044.          or NM.
  4045.          2.1B  List the customers names for customers who have purchased
  4046.          MW or NM.
  4047.          2.1C  List the customer names for customers who are from
  4048.          California or have a rating less than 10.
  4049.          
  4050.          2.2 Comparison operators <= and >=.
  4051.          2.2A  List customers who have a rating greater than or equal to
  4052.          15.
  4053.          
  4054.          3 - WHICH, WHO, WHOSE, THAT, WITH, WHERE
  4055.          The above words have various meanings but I will emphasize their
  4056.          use in clauses.  Clauses connect parts of sentences.  Sometimes
  4057.          is makes a query clearer when it is converted to a form that has
  4058.          a clause.  The first six queries ask the same thing in different
  4059.          ways.  
  4060.          3.1A  Which customers have a rating over 10?
  4061.          3.1B  Who has a rating over 10?
  4062.          3.1C  List customers who have ratings over 10.
  4063.          3.1D  List customers with ratings over 10.
  4064.          3.1E  List every customer whose rating is over 10.
  4065.          3.1F  List every customer where the rating is over 10.
  4066.          Queries 3.1C through 3.1D are the most straightforward because
  4067.          the first part describes what is to be displayed and the second
  4068.          part describes the logic to determine which rows we want.  Many
  4069.          of the following can be rewritten in a similar manner but I will
  4070.          usually present only one approach.
  4071.          
  4072.          3.2  Link tables  
  4073.          3.2A  List the customer's states for customers who have purchased
  4074.          products  that are manufactured in Idaho.
  4075.          3.2B  Get branch codes for branches that sell products that are
  4076.          sold by branches that sell product DD. This is a rather
  4077.          convoluted one but shows the power of recognizing clauses. 
  4078.          Logically we want to find all the branches that sell product DD.
  4079.  
  4080.  
  4081.  
  4082.                                     ENG-3
  4083.  
  4084.  
  4085.  
  4086.  
  4087.          Then we want to take those branches and find all the products
  4088.          they sell.  With that set of products, we want to find all the
  4089.          branches that sell those products.  Notice how in 3.2B each
  4090.          subquery corresponds to each clause.  Be careful doing the
  4091.          conversion.  Sometimes the "that" does not have to be included. 
  4092.          Get branch codes for branches that sell products sold by branches
  4093.          that sell product DD.  In the previous sentence the "that" is
  4094.          missing between "products" and "sold".
  4095.          3.2C  List the names of employees who have sold products to
  4096.          Compugorp.
  4097.          3.2D. Get customer names and product names for customers who have
  4098.          purchased items that are manufactured outside their own state.
  4099.          3.2E  Get branch code, customer names and product names for those
  4100.          who purchase products that are manufactured in their own state
  4101.          from a branch in their own state.
  4102.          3.2F  Get branch numbers for branches that sell to both AA and
  4103.          BB.
  4104.          3.2G Who are the managers?
  4105.          3.2H  What are the names of managers who actually sold something?
  4106.          3.2I  What are the names of managers whose salespeople have sold
  4107.          products to Organomice?
  4108.          3.2J  What are the names of managers whose salespeople have sold
  4109.          products ONLY to Organomice? We have to add something to exclude
  4110.          salespeople who sold to a customer other than Organomice.
  4111.          3.2K List the names of Xero Xanadu's subordinates who sold
  4112.          Technowidgits and what were the quantities sold?
  4113.          3.2L  Get branch codes for branches that sell to a Washington or
  4114.          Arizona customer a product manufactured in Oregon.
  4115.          3.2M Get all pairs of state values where the branch in the first
  4116.          state sells to a customer in a second state.
  4117.          
  4118.          3.4  Implied that or who  
  4119.          The words in parentheses in the following three queries are
  4120.          optional.
  4121.          3.4A  Get product codes for products (that were) sold to any
  4122.          customer in California.
  4123.          3.4B  Get product numbers and corresponding customer names for
  4124.          products (that are) manufactured in the same state as a customer.
  4125.          3.4C  Get customer numbers for customers (who were) sold at least
  4126.          one product from a branch in the same state.
  4127.          
  4128.          4 - ANY/ALL
  4129.          Translating any and all cause the same sort of problems that were
  4130.          caused by and.  This is because the way we use a word in English
  4131.          may be different from the way we translate it to SQL.
  4132.          First of all, I will skip over any queries in which the any or
  4133.          all serve no purpose.  Often they are added for emphasis and CAN
  4134.          mean the same thing as in queries 4.1A and 4.1B.  Normally, the
  4135.          word any serves no purpose unless it is used in a comparison.
  4136.  
  4137.  
  4138.  
  4139.  
  4140.  
  4141.                                     ENG-4
  4142.  
  4143.  
  4144.  
  4145.          4.1  All and any not needed.  
  4146.          4.1A  Get all product codes for all products purchased by
  4147.          customers in California. 
  4148.          Same as 3.4A
  4149.          4.1B  Get any product code for any products purchased by any
  4150.          customer in California. 
  4151.          Same as 3.4A
  4152.          4.1C  Get product codes for products purchased by customers in
  4153.          California. 
  4154.          Same as 3.4A
  4155.          
  4156.          4.2  All used as part of a FORALL quantifier
  4157.          This was discussed in detail in the chapter on correlated
  4158.          subqueries. It is used when we want to compare one set of values
  4159.          to another set of values instead of comparing a single value to a
  4160.          set of values or comparing a single value to a single value.
  4161.          4.2A  Get product codes for products purchased by all customers.
  4162.          4.2B  Get product codes for products purchased by all customers
  4163.          in California.
  4164.          4.2C  Get product codes for products where the minimum defects is
  4165.          greater than 10 and has been purchased by all customers.
  4166.          4.2C  Get customer names for customers who have purchased all
  4167.          products.
  4168.          4.2D  States that manufacture all products sold by a branch in
  4169.          the same state.
  4170.          4.2E  Branches that have sold all the products manufactured in
  4171.          their own state.
  4172.          4.2F Branches that have sold all the products to all the
  4173.          customers.
  4174.          
  4175.          4.3 Using any and all in comparisons
  4176.          These are very deceiving.  The use of any normally translates to
  4177.          all in SQL or an equivalent construction.  This idea is discussed
  4178.          in the chapter on subqueries.
  4179.          4.3A  List products that have defects greater than any defect for
  4180.          products manufactured in Idaho.  Compare this to 4.3B.
  4181.          4.3B  List products that have defects greater than all defects
  4182.          for products manufactured in Idaho.
  4183.          Query 4.3A is normally interpreted as the same as 4.3B although
  4184.          logically it means something different.  If you think that they
  4185.          are different in common English then I guess SQL has distorted
  4186.          your thinking a bit.  When a tennis player says that he is better
  4187.          than ANY tennis player, we assume that he means that he is better
  4188.          than ALL tennis players.  The confusion is caused by the fact
  4189.          that in most contexts any and all are very different as in the
  4190.          following two sentences.  Give me any book.  Give me all the
  4191.          books.
  4192.          4.3C  List products that have defects less than any (all) defects
  4193.          for products manufactured in Idaho.
  4194.          4.3D  List products that have defects the same as any defects for
  4195.          products manufactured in Idaho.  In this case we cannot replace
  4196.          the "any" with an "all" and retain the same meaning.
  4197.          
  4198.  
  4199.  
  4200.                                     ENG-5
  4201.  
  4202.  
  4203.  
  4204.  
  4205.  
  4206.          N - NEGATION
  4207.          In this section I will take many of the above queries and negate
  4208.          them.
  4209.          N1.2a List the rating for all except Organomice and Compugorp.
  4210.          N1.3A.1  List the products manufactured in neither Washington nor
  4211.          Idaho.
  4212.          N1.3A.2 List the products not manufactured in both Washington and
  4213.          Idaho.
  4214.          N1.3B.1  List the dates that products MW or NM were not
  4215.          purchased.
  4216.          N1.3B.2  List the dates that products other than MW or NM were
  4217.          purchased.
  4218.          N1.4B.1  List the dates when all of the following products were
  4219.          not purchased: MW, NM, and GC.
  4220.          N1.4B.2  List the dates when none of the following products were
  4221.          purchased: MW, NM, and GC.
  4222.          N1.4B.3  List the dates when something other than the following
  4223.          products were purchased: MW, NM, and GC.
  4224.          N1.4C.1  List the customers who are from Arizona and have not
  4225.          purchased product MW.  
  4226.          N2.1B.1  List the customers names for customers who have not
  4227.          purchased MW or NM.
  4228.          N2.1B.2  List the customers names for customers have purchased
  4229.          products other than MW and NM.
  4230.          The difference between N2.1B.1 and N2.1B.2 is that the first one
  4231.          would exclude any customer who has purchased MW or NM.  The
  4232.          second one would include the customer as long as the customer
  4233.          purchased another product.
  4234.          N3.2A.1  List the customer's states for those customers who have
  4235.          not purchased products manufactured in Idaho.
  4236.          N3.2A.2  List the customer's states for those customers who have 
  4237.          purchased products not manufactured in Idaho.
  4238.          N3.2A.2  List the customers' states for those customers who have 
  4239.          purchased products manufactured outside (not in) Idaho.
  4240.          The above differs from the previous query in that it will include
  4241.          any product manufactured in Idaho IF it is manufactured outside
  4242.          Idaho.
  4243.          N3.2B.1  Get branch codes for branches that do not sell products
  4244.          that are sold by branches that sell product DD.
  4245.          N3.2B.2  Get branch codes for branches that sell products that
  4246.          are not sold by branches that sell product DD.
  4247.          N3.2B.3  Get branch codes for branches that sell products that
  4248.          are sold by branches that do not sell product DD.
  4249.          The negation is on selling product DD.  This is different from
  4250.          saying: "...branches that sell products other than DD".  
  4251.          N3.2C.1  List the names of employees who have not sold products
  4252.          to Compugorp.
  4253.          N3.2D.1 Get customer names and product names for customers who
  4254.          have purchased items not manufactured outside their own state.
  4255.          N3.2D.2 Get customer names for customers who have not purchased
  4256.          items manufactured outside their own state.  
  4257.  
  4258.  
  4259.                                     ENG-6
  4260.  
  4261.  
  4262.  
  4263.  
  4264.  
  4265.          N3.2D.3 Get customer names for customers who have only purchased
  4266.          items in their own state. 
  4267.          At first glance N3.2D.3 seems the same as N3.2D.2 but N3.2D.2
  4268.          would display customers who have not purchased anything.  We have
  4269.          to add some more to make sure that the customer actually made a
  4270.          purchase.
  4271.          N3.2F.1  Get branch codes for branches that do not sell to both
  4272.          AA and BB.
  4273.          N3.2F.1  Get branch codes for branches that sell to neither AA
  4274.          nor BB.
  4275.          N3.2F.1  Get branch codes for branches that do not sell to AA or
  4276.          BB.
  4277.          N3.2G.1  What are the names of managers who did not sell
  4278.          anything?
  4279.          N3.2I.1  What are the names of managers whose salespeople have
  4280.          not sold products to Organomice?
  4281.          N3.2I.2  What are the names of managers whose salespeople have 
  4282.          sold products to everyone except Organomice?  This becomes a
  4283.          tough one because the opposite of "only Organomice" in 3.2I is
  4284.          "everyone except Organomice" and "everyone" corresponds to the
  4285.          FORALL quantifier covered in the chapter on correlated
  4286.          subqueries.  The core of this query has to do with "salespeople
  4287.          selling to ALL customers (except Organomice)".
  4288.          N3.2L.1  Get branch codes for branches that sell to a Washington
  4289.          or Arizona customer a product not manufactured in Oregon.
  4290.          N3.2L.2  Get branch codes for branches that sell to a Washington
  4291.          or Arizona customer a product manufactured outside of Oregon.
  4292.          N4.2A.1  Get product codes for products not purchased by all
  4293.          customers.
  4294.          N4.2A.2  Get product codes for products purchased by none of the
  4295.          customers.
  4296.          N4.2D.1  States that do not manufacture all products sold by a
  4297.          branch in the same state.
  4298.          N4.2D.2  States that manufacture no products sold by a branch in
  4299.          the same state.
  4300.          N4.2E.1  Branches that have not sold all the products
  4301.          manufactured in their own state.
  4302.          N4.2E.2  Branches that have sold none of the products
  4303.          manufactured in their own state.
  4304.          N4.2E.3  Branches that have sold all the products not
  4305.          manufactured in their own state.
  4306.          N4.2E.4  Branches that have sold all the products manufactured
  4307.          outside their own state.
  4308.  
  4309.          
  4310.          
  4311.          
  4312.          
  4313.          
  4314.          
  4315.          
  4316.          
  4317.          
  4318.                                     ENG-7
  4319.  
  4320.  
  4321.  
  4322.  
  4323.  
  4324.          CREATE  TABLE
  4325.          
  4326.          OVERVIEW
  4327.          The CREATE TABLE command is used to create a table file. You can
  4328.          control the contents of the columns in two ways.  You can prevent
  4329.          a column from being null and you can make sure that a column or
  4330.          set of columns never contain duplicate values.  These
  4331.          restrictions allow us to maintain valid primary keys when we use
  4332.          the INSERT command to add rows to a table or the UPDATE command
  4333.          to modify values in a table.
  4334.          
  4335.          SYNTAX
  4336.          
  4337.          CREATE TABLE table_name (
  4338.            column_definition [, column_definition...]
  4339.            [, uniqueness_constraint]
  4340.          );
  4341.  
  4342.          EXAMPLE:
  4343.          The statement below shows how to create the cust table that  the
  4344.          documentation uses. 
  4345.          
  4346.          create table cust ( 
  4347.          code char(2) not null unique,   
  4348.          name char(15) not null,
  4349.          st char(2) not null,
  4350.          rating numeric(2)
  4351.          );
  4352.          
  4353.          You can define up to 27 columns in a table.
  4354.          As with all SQL commands, they can be in uppercase or lowercase. 
  4355.          After the name of the table, parentheses enclose the column
  4356.          definitions. The column definitions are put on separate lines to
  4357.          aid in readability.  SQL never requires anything to be put on
  4358.          separate lines or have specific spacing. Note that all column
  4359.          definitions except the last one has a comma after it.
  4360.          The above command states that there will be four columns in the
  4361.          cust table:
  4362.          
  4363.          code can have up to two characters.  It is the primary key so
  4364.          nulls will not be allowed and each code will be unique.
  4365.          
  4366.          name can have up to fifteen characters. It must contain a value.
  4367.          
  4368.          st   is the state postal code and it can have up to 2 characters.
  4369.          It must contain a value.
  4370.          
  4371.          rating is the customer rating.  Since NOT NULL is not specified,
  4372.          the customer rating is optional - it does not have to be entered.
  4373.  
  4374.  
  4375.  
  4376.  
  4377.                                       CREATE-1
  4378.  
  4379.  
  4380.  
  4381.  
  4382.  
  4383.          COMPONENTS OF CREATE TABLE
  4384.          
  4385.          table_name
  4386.          A table name can be from 1 to 10 characters.  The first eight
  4387.          characters must be unique.  The first character must be a letter 
  4388.          of the alphabet.  The rest can be letters or digits, _
  4389.          (underscore), or #.  The table files are created on the disk as in
  4390.          table_name.DBF and table_name.SQD.  For example, the cust 
  4391.          table, created above would be stored as CUST.DBF and CUST.SQD 
  4392.          on your disk.  However, from SSQL, you would always refer to it 
  4393.          as CUST.  Since SSQL adds the extension of DBF and SQD to all 
  4394.          table files, you must not use a period in your table name.   
  4395.          
  4396.          column_definition
  4397.          
  4398.          column_name data_type [NOT NULL [UNIQUE]]
  4399.          
  4400.          column_name
  4401.          A column name can be from 1 to 10 characters.  The first
  4402.          character must be a letter of the alphabet.  The rest can be
  4403.          letters or digits.  In creating column names remember that when
  4404.          you display a table, the full column name is displayed too as the
  4405.          heading.  Long column names tend to fill the screen (or printer)
  4406.          very rapidly when you want to display many columns.  The column
  4407.          name is displayed exactly as you type it, retaining the uppercase
  4408.          and lowercase letters.  The create statement above created the
  4409.          column names and hence the headings in lowercase letters.  We
  4410.          could have written it as shown below.
  4411.          
  4412.          create table cust ( 
  4413.          CODE char(2) not null unique,   
  4414.          NAME char(15) not null,
  4415.          ST char(2) not null,
  4416.          RATING numeric(2)
  4417.          );
  4418.          
  4419.          With the statement above, whenever we display data from the
  4420.          table, the headings would in uppercase.
  4421.  
  4422.          data_type
  4423.          The data types in SQL fall into two broad categories - numbers
  4424.          and characters.  Within the numbers category, there are exact
  4425.          numeric types and approximate numeric types.  However, all 
  4426.          numeric data types are converted to the dBase numeric data 
  4427.          type.  The other types are included to maintain compatibility 
  4428.          with ANSI SQL.  Two non-standard data have been added to 
  4429.          maintain compatibility with dBase - Date and Logical.
  4430.  
  4431.  
  4432.  
  4433.  
  4434.          
  4435.  
  4436.                                 CREATE-2
  4437.  
  4438.  
  4439.          Numeric
  4440.          
  4441.           (Exact Numeric)
  4442.           NUMERIC[(length [,decimal_places])]
  4443.           DECIMAL[(length [,decimal_places])]
  4444.           DEC[(length [,decimal_places])]
  4445.           INTEGER
  4446.           INT
  4447.           SMALLINT
  4448.           (Approximate Numeric covered in the full documentation)
  4449.  
  4450.          When you use numeric data types the values are always
  4451.          right-justified (values are pushed to the right so all the
  4452.          decimal lines up).  Data must be of this type in order to use any
  4453.          of the numeric functions such as avg, max, min, and sum.  
  4454.          The first three data types in the exact numeric category -
  4455.          NUMERIC, DECIMAL, and DEC, can be used interchangeably.  It
  4456.          sounds a little odd that a data type called DECIMAL does not
  4457.          necessarily mean that the contents of the column will contain a
  4458.          decimal place since the modifier decimal_places is optional. 
  4459.          
  4460.          length (optional)
  4461.          The length of the column be up to 12 digits (including the
  4462.          decimal place. Without this specification, the length is 1.
  4463.          
  4464.          decimal_places (optional)
  4465.          The number of places to the right of the decimal.  For Example:  
  4466.          
  4467.          cost  numeric(5,2)
  4468.          
  4469.          This would allow a maximum of 99.99 to be stored in the column.
  4470.          
  4471.          This also could be defined as:
  4472.          
  4473.           cost decimal(5,2)
  4474.          or 
  4475.           cost dec(5,2)
  4476.          
  4477.          It is allowable to use whole numbers even though you define it as
  4478.          having decimals.  For example, you want to enter grades that are
  4479.          from  0 to 100 but when you calculate grade averages, you want it
  4480.          calculated to one tenth of a grade point.  You would define the
  4481.          column as:
  4482.          
  4483.           grade  numeric(5,1)
  4484.          
  4485.          You would enter the grades as whole numbers but when the average
  4486.          is calculated, the decimal would be included. 
  4487.          With NUMERIC, DECIMAL and DEC, if you omit the dec_places
  4488.          modifier, the result is a whole number.
  4489.          
  4490.          Data types such as INTEGER (which is the same as INT) and
  4491.          SMALLINT have lengths associated with them.  The ANSI standards
  4492.          state that the lengths shall be determined by the implementor so
  4493.          I have picked lengths of ten and five respectively.
  4494.  
  4495.                                CREATE-3
  4496.  
  4497.          
  4498.          INTEGER is the same as numeric(10) or decimal(10) or dec(10)
  4499.          SMALLINT is the same as numeric(5) or decimal(5) or dec(5)
  4500.          
  4501.          You can see that although there are six exact numeric data types,
  4502.          you only need one.
  4503.          
  4504.          Characters
  4505.          
  4506.           CHARACTER[(length)]
  4507.           CHAR[(length)]
  4508.  
  4509.          These data types can be used for any column that you do not use
  4510.          in a calculation.  Although the data is usually a combination of
  4511.          alphabetic and numeric data, it is alright if the column just
  4512.          contains digits.  The characters are left-justified.  The maximum
  4513.          column width is 80.  CHARACTER and CHAR can be used
  4514.          interchangeably.  If you omit the length modifier, then the
  4515.          length of the column is one.
  4516.          
  4517.          Date
  4518.            DATE
  4519.            The DATE data type creates a column of 8 characters.  Dates 
  4520.          are entered as mm/dd/yy.  
  4521.          
  4522.          Logical
  4523.            LOGICAL - A one character column that can store Y, y, N, n, 
  4524.          T, t, F, f, or ? 
  4525.  
  4526.          NOT NULL
  4527.          This modifier to a column definition will ensure that there is
  4528.          always a value for this column. It prevents the INSERT and UPDATE
  4529.          commands from allowing a column to contain a null value. When
  4530.          used with the UNIQUE modifier, it is used to specify a primary
  4531.          key.  There are special commands to retrieve rows that only have
  4532.          null values in a particular column or to exclude rows that have
  4533.          null values in a particular column.
  4534.          
  4535.          UNIQUE
  4536.          This ensures that each value in the column is unique.  If an
  4537.          attempt is made to enter a duplicate value when inserting a new
  4538.          row, an error results and the row is not added to the table.  If
  4539.          you use unique, it must be used with NOT NULL. This is typically
  4540.          used when a single column is a primary key. 
  4541.          
  4542.          Uniqueness_constraint (covered in full documentation)
  4543.          
  4544.          CHECKING THE STRUCTURE OF A TABLE
  4545.          In order to display the create command used to create the table,
  4546.          type STRUCT followed by the table name.
  4547.          
  4548.  
  4549.  
  4550.  
  4551.  
  4552.  
  4553.  
  4554.                                      CREATE-4
  4555.  
  4556.  
  4557.          CREATE A VIEW
  4558.  
  4559.          OVERVIEW
  4560.          A view is a derived table.  A view describes an alternative
  4561.          access to columns and tables that already exist.  The create
  4562.          table command allowed us to create a physical table that is also
  4563.          called a base table.  With a base table every column corresponds
  4564.          to the source of data.  We can always use the insert, update, and
  4565.          delete commands on base tables.  Views never exist as permanent
  4566.          tables.  Whenever you query a view, it creates a temporary table
  4567.          based on your view.  When the query is complete, the temporary
  4568.          table is erased.
  4569.          The view can reference an existing column under a different name
  4570.          or keep the original name. The view can be based on more than one
  4571.          table.  You can reference as few or as many columns you want in
  4572.          the tables you select.
  4573.          
  4574.          SYNTAX
  4575.          
  4576.          CREATE VIEW view_name [(column_name [,column_name...])] AS
  4577.          select_statement
  4578.          
  4579.          EXAMPLES:
  4580.          Create a view which only shows less than the full number of
  4581.          columns in a table.  In a commercial database system, this can be
  4582.          used to prevent users from seeing sensitive data.  For example,
  4583.          The employee table may include salary information.  A view of
  4584.          that table could have everything except the salary information.
  4585.          It is necessary to have some background in the use of the select
  4586.          command before attempting to create a view of a table.  The
  4587.          statement below shows the creation of a view called custa.   It
  4588.          is based on the cust table that has four columns:  code, name,
  4589.          st, and rating.
  4590.          
  4591.          create view custa as
  4592.          select code, name, st
  4593.          from cust;
  4594.          
  4595.          The column names are the same as they are in the original table. 
  4596.          The only difference is that rating is not part of the view.
  4597.          When you use view custa in a query, it acts just like a regular
  4598.          table.  When it displays all the columns as shown below, you only
  4599.          see the three columns specified in the view.  
  4600.          
  4601.          select * 
  4602.          from custa; 
  4603.          
  4604.          code name            st  
  4605.          ---- --------------- --  
  4606.          AA   Compugorp       WA  
  4607.          BB   Technoharps     OR  
  4608.          ZZ   Organomice      AZ  
  4609.          DD   QuarkCo         AZ  
  4610.          EE   Marswarp        CA  
  4611.          FF   Multicrud       NV  
  4612.          
  4613.                                      VIEW-1
  4614.  
  4615.  
  4616.  
  4617.  
  4618.  
  4619.          We have no access to the rating column when using the custa view.
  4620.          
  4621.          RULES FOR USING VIEWS
  4622.          
  4623.          It is used in a select statement, NOT an insert, update, or
  4624.          delete statement. To erase the view, use the drop view statement.
  4625.          
  4626.          Can contain any select statement except one that has redirectto. 
  4627.          
  4628.          Views should only be used when they are absolutely needed.  They
  4629.          can add much confusion to a database design because it is easy to
  4630.          forget which are tables and which are views.
  4631.          
  4632.          (MORE IN THE FULL DOCUMENTATION)
  4633.  
  4634.  
  4635.  
  4636.  
  4637.  
  4638.  
  4639.  
  4640.  
  4641.  
  4642.  
  4643.  
  4644.  
  4645.  
  4646.  
  4647.  
  4648.  
  4649.  
  4650.  
  4651.  
  4652.  
  4653.  
  4654.  
  4655.  
  4656.  
  4657.  
  4658.  
  4659.  
  4660.  
  4661.  
  4662.  
  4663.  
  4664.  
  4665.  
  4666.  
  4667.  
  4668.  
  4669.  
  4670.  
  4671.  
  4672.                                       VIEW-2
  4673.  
  4674.  
  4675.  
  4676.  
  4677.  
  4678.  
  4679.          DROP TABLE/VIEW
  4680.  
  4681.          OVERVIEW
  4682.          The drop command is used to erase a table or view from the disk.
  4683.          The drop table command is used to erase the description of the
  4684.          table from the disk.  The drop view command erases the view but
  4685.          can never alter data or tables since the view simply describes
  4686.          access to a table or tables.
  4687.          
  4688.          SYNTAX
  4689.          
  4690.          DROP TABLE table_name;
  4691.          or
  4692.          DROP VIEW view_name;
  4693.          
  4694.          EXAMPLES:
  4695.          
  4696.          drop table cust;
  4697.          drop view custa; 
  4698.          
  4699.          COMPONENTS OF THE DROP COMMAND
  4700.          
  4701.          table_name
  4702.          The table_name must be a valid table name which contains NO data. 
  4703.          In order to delete all the data you can first type:
  4704.          
  4705.          delete from table_name;
  4706.          
  4707.          The delete command erases the data and the drop table command 
  4708.          erases the column descriptions. 
  4709.          
  4710.          view_name
  4711.          The view_name must be a valid view name.  Since no data is
  4712.          affected it can be used at any time.
  4713.  
  4714.  
  4715.  
  4716.  
  4717.  
  4718.  
  4719.  
  4720.  
  4721.  
  4722.  
  4723.  
  4724.  
  4725.  
  4726.  
  4727.  
  4728.  
  4729.  
  4730.  
  4731.                                 DROP-1
  4732.  
  4733.  
  4734.           INSERT DATA INTO A TABLE
  4735.  
  4736.           OVERVIEW
  4737.           The ANSI standards define two ways to add data to a table. One
  4738.           requires us to type each value for each column in each row. The
  4739.           other copies data from one table and puts it in another. It is
  4740.           very useful for making corrections to tables. I only describe the
  4741.           first type in this documentation.  Registered users find out
  4742.           about the more advanced use of the insert command and a special,
  4743.           non-ANSI command which makes inserting data MUCH easier.
  4744.           
  4745.           Both insert commands maintain the restrictions we may have
  4746.           imposed through the CREATE TABLE command.  We could have
  4747.           specified NOT NULL and UNIQUE to prevent certain types of data
  4748.           errors in our tables.
  4749.           
  4750.           INSERT VALUES - SYNTAX
  4751.           
  4752.           INSERT INTO table_name [column_name [,column_name...]]
  4753.           VALUES (column_value [,column_value]);
  4754.           
  4755.           EXAMPLES:
  4756.           
  4757.           The first example shows the typical way to use the insert command
  4758.           since it does not reference any column names.  The insert command
  4759.           assumes that you will enter the data in the order that they were
  4760.           created in the table.  The second example shows that column names
  4761.           can be referenced. The third example shows that the data entry
  4762.           does not have to be in the order in which the columns were
  4763.           created.  Compare the order of the values to the first example.
  4764.           insert into cust values('AA','Compugorp','WA',20);
  4765.           or
  4766.           insert into cust code, name, st, rating
  4767.           values('AA','Compugorp','WA',20);
  4768.           or
  4769.           insert into cust name, code, rating, st 
  4770.           values('Compugorp','AA',20,'WA');
  4771.           
  4772.           MORE DETAIL ON THE COMPONENTS OF THE SYNTAX
  4773.           
  4774.           table_name
  4775.           
  4776.           Any existing table in your database.
  4777.           
  4778.           column_name
  4779.           
  4780.           Normally column names are not used with the insert command.  This
  4781.           is because the insert command assumes that we are going to insert
  4782.           values for all the columns in the table in the order in which
  4783.           they were created. You saw this in the first example.  It has the
  4784.           same effect as the second example.  In the third example we want
  4785.           to insert data in a different order.  This could be useful if the
  4786.           source document that we are getting the data from presents the
  4787.           data in a different order from which the table was designed.  
  4788.  
  4789.  
  4790.                                    INSERT-1
  4791.  
  4792.  
  4793.  
  4794.           column_value
  4795.           There are three types of column values: character, numeric and
  4796.           null.
  4797.           
  4798.           Character data
  4799.           Character data must be enclosed in quotes or apostrophes. The
  4800.           first example above could have been entered as:
  4801.           
  4802.           insert into cust values("AA","Compugorp","WA",20);
  4803.           
  4804.           Although you can enter SQL commands in uppercase or lowercase,
  4805.           care must be taken when entering character data since everything
  4806.           is stored exactly as entered.  This can create problems when we
  4807.           are retrieving data.  Let's assume that Compugorp purchased some
  4808.           Megawamps so we insert the following information into the sales
  4809.           table: date, branch code, customer code, employee number, product
  4810.           code and quantity.
  4811.           
  4812.           insert into sales values('04/01/87','1A','aa',10,'MW',947);
  4813.           
  4814.           Although SQL would accept the above insert command, we made a
  4815.           critical logical error.  The correct customer code for Compugorp
  4816.           is 'AA', NOT 'aa'.  This is an error that is rather difficult to
  4817.           detect.  Any time we relate the sales table with the cust table,
  4818.           the above row in the sales table will not be retrieved.  This
  4819.           could cause reports to disagree.  The section on correlated
  4820.           subqueries explains how to detect this problem.  Technically, it
  4821.           is called referential integrity.
  4822.           Another mistake that is easily made involves accidentally having
  4823.           leading or trailing spaces in a character value.  If we inserted
  4824.           the data for Compugorp with the following:
  4825.           
  4826.           insert into cust values('AA','Compugorp ','WA',20);
  4827.           
  4828.           You will notice the space after the last 'p' in Compugorp.  There
  4829.           will not be a problem with it until you try a query that includes
  4830.           the name of the company as shown below.
  4831.           select *
  4832.           from cust 
  4833.           where name = 'Compugorp';
  4834.           
  4835.           The row with Compugorp will not be found because the query did
  4836.           not contain the space after the last p!  This is a very
  4837.           frustrating error as I have seen quite a few students accuse me
  4838.           of having a serious malfunction in my program with the look on
  4839.           their faces suggesting what I could do with SSQL.
  4840.           All columns have a particular length associated with them.  This
  4841.           denotes the maximum number of characters that the column can
  4842.           contain.  When we created the cust table, the name column was
  4843.           defined as char(15) which means that the maximum number of
  4844.           characters it can contain is 15.  If more than 15 characters are
  4845.           in the name, only the first 15 are saved.
  4846.  
  4847.  
  4848.  
  4849.                                    INSERT-2
  4850.  
  4851.  
  4852.  
  4853.  
  4854.           
  4855.           insert into cust values('GG','Bizzlesnarf & Sons','CA',10);
  4856.           
  4857.           Although the above command would be allowed, when we retrieve the
  4858.           customer name, only "Bizzlesnarf & " would be displayed.
  4859.           
  4860.           Numeric Data
  4861.           The basic rule to remember with numeric data is not to enclose
  4862.           them in quotes as we can see above with the entry for rating. 
  4863.           The length of the column as specified in the create table command
  4864.           must not be exceeded.  Rating was created as numeric(2) which
  4865.           means that the maximum number of digits is 2.  The following
  4866.           insert command would cause an error because the entry for rating
  4867.           contains 3 digits instead of 2:
  4868.           
  4869.           insert into cust values('AA','Compugorp','WA',123);
  4870.           
  4871.           Because of the error, none of the values would be added to the
  4872.           table.
  4873.           
  4874.           Null data
  4875.           I really should not write "null data" or "null value" because the
  4876.           concept of the null means absence of a value.  If we add a
  4877.           customer that has no rating we would do something like:
  4878.           
  4879.           insert into cust values('HH','MagnaMice','AZ',null);
  4880.           
  4881.           There are NO QUOTES enclosing the word null.  Like all other
  4882.           keywords in SQL, it can be in uppercase or lowercase letters. 
  4883.           The null is different from a 0 (zero) in that a null means that
  4884.           there is no rating.  A 0 (zero) means that there IS a rating and
  4885.           the rating is 0.  This idea is further developed when discussing
  4886.           data retrieval in the section on the select command.          
  4887.  
  4888.  
  4889.  
  4890.  
  4891.  
  4892.  
  4893.  
  4894.  
  4895.  
  4896.  
  4897.  
  4898.  
  4899.  
  4900.  
  4901.  
  4902.  
  4903.  
  4904.  
  4905.  
  4906.  
  4907.  
  4908.                                    INSERT-3
  4909.  
  4910.  
  4911.  
  4912.  
  4913.          UPDATE DATA IN A TABLE
  4914.          
  4915.          OVERVIEW
  4916.          The update command is used to change column values in existing
  4917.          rows based on criteria in a search_condition that is accomplished
  4918.          through a where clause. If there where any constraints put on the
  4919.          column values when the table was created such as not null or not
  4920.          null unique, the update command makes sure that the constraints
  4921.          are maintained before the columns are actually updated.  Next to
  4922.          the insert command, it is the most cumbersome to use.  To make
  4923.          updates a bit easier, I made a non-ANSI command which is fully
  4924.          explained in the full documentation.
  4925.          
  4926.          UPDATE SYNTAX
  4927.          
  4928.          UPDATE table_name
  4929.          SET column_name = value [, column_name = value...]
  4930.          WHERE search_condition
  4931.  
  4932.          The value can be a mathmatical expression.
  4933.  
  4934.          EXAMPLES:
  4935.          In the first example, the rating for customer AA is increased 
  4936.          by 5.
  4937.          
  4938.          update cust
  4939.          set rating = rating + 5
  4940.          where code = 'AA';
  4941.          
  4942.          In the example below, the rating for customer CC is changed to a
  4943.          null.
  4944.          
  4945.          update cust
  4946.          set rating = null
  4947.          where code = 'CC';
  4948.          
  4949.          
  4950.          The example below shows how to change the percent defects to 10
  4951.          for a batch of product GC that occurred on July 21, 1987 in
  4952.          Idaho.
  4953.          
  4954.          update manu
  4955.          set defects = 10
  4956.          where date = '07/21/87' and code = 'GC' and mst = 'ID';
  4957.          
  4958.          In the following, two columns are changed.  For customer BB,
  4959.          rating is changed to 14 and the state column is changed to Idaho.
  4960.          
  4961.          update cust
  4962.          set rating = 14, st = 'ID'
  4963.          where code = 'BB';
  4964.  
  4965.          
  4966.  
  4967.                                       UPDATE-1
  4968.  
  4969.  
  4970.  
  4971.  
  4972.  
  4973.          The example below is a bit more involved.  We want to change the
  4974.          rating of branch 2A customers who have purchased above average
  4975.          quantities (overall) to 30. The use of the select command in a
  4976.          where clause is covered in a later section.  
  4977.  
  4978.          update cust
  4979.          set rating = 30 
  4980.          where code in 
  4981.           (select distinct code 
  4982.            from cust, sales
  4983.            where cc=code  
  4984.              and bc='2A'  
  4985.              and qty >
  4986.             (select avg(qty) 
  4987.              from sales)); 
  4988.          
  4989.          COMPONENTS OF UPDATE
  4990.          
  4991.          table_name
  4992.          Any existing table in your database.
  4993.          
  4994.          column_name
  4995.          Any valid column name in the table name accessed, including the
  4996.          column(s) which form the key. 
  4997.           
  4998.          value
  4999.          The value is anything appropriate for the data type of the
  5000.          column.  Any value that would work with the insert command will
  5001.          work with the update command.  As with the insert command, the
  5002.          word null is valid to show that there is no value for the column. 
  5003.          Remember to put quotes around character values.
  5004.          
  5005.          
  5006.          search_condition
  5007.          The section on the select command has more detail on the
  5008.          search_condition that is accomplished with the use of a where
  5009.          clause.  Typically, you want to update a single row.  To update a
  5010.          single row the search_condition must test for the primary key so
  5011.          that will be explained here.  The primary key allows you to
  5012.          uniquely define each row.  The cust table, which is used in most
  5013.          of the examples above, has a primary key based on code.  That is,
  5014.          based on the value in code, we know only a single row will be
  5015.          updated.  
  5016.          
  5017.          Compound keys are discussed in the full documentation.
  5018.  
  5019.  
  5020.  
  5021.  
  5022.  
  5023.  
  5024.  
  5025.  
  5026.                                 UPDATE-2
  5027.  
  5028.  
  5029.          DELETE ROWS FROM A TABLE
  5030.          
  5031.          OVERVIEW
  5032.          The delete command deletes rows based on the criteria in the
  5033.          where clause.  Without a where clause, all the data in the table
  5034.          is deleted.
  5035.          Refer to the section on the select statement for details on the
  5036.          where clause.  The basic difference between the select * and the
  5037.          delete is that with select, the rows are displayed, and with
  5038.          delete, the rows are deleted.  Because of this, it is a smart
  5039.          idea to use the select before you use the delete so you can see
  5040.          the rows that you are going to delete.
  5041.          You can only delete complete rows.  If you want to delete column
  5042.          data within a row, use the update command.
  5043.          
  5044.          SYNTAX
  5045.          
  5046.          DELETE FROM table_name
  5047.          [WHERE search_expression]
  5048.          
  5049.          EXAMPLES:
  5050.          
  5051.          The following shows how to delete all the data from the cust
  5052.          table.
  5053.          
  5054.          delete from cust;
  5055.          
  5056.          The following shows how to delete customers from the cust table
  5057.          whose rating is less than 10.
  5058.          
  5059.          delete from cust
  5060.          where rating < 10;
  5061.          
  5062.          COMPONENTS OF DELETE
  5063.          
  5064.          table_name
  5065.          Any existing table in your database.
  5066.          
  5067.          search_condition
  5068.          The section on the select statement has more detail on the
  5069.          search_condition that is accomplished with a where clause. A
  5070.          simple example is shown above. The command to delete customers
  5071.          whose rating is less than 10 could create some problems if there
  5072.          were customers in the sales table with customer codes that you
  5073.          deleted.  Whenever you delete a row that contains a primary key
  5074.          that is used in another table, problems can arise.  In order to
  5075.          determine whether there are any of those customers in the sales
  5076.          table, you could use the select command as shown below.
  5077.          
  5078.          select *
  5079.          from   sales
  5080.          where  cc in (
  5081.            select code
  5082.            from   cust
  5083.            where  rating < 10);
  5084.  
  5085.                                      DELETE-1
  5086.  
  5087.  
  5088.  
  5089.  
  5090.          
  5091.          To delete all the corresponding rows in the sales table for
  5092.          customers whose rating is less than 10, you would simply take the
  5093.          statement above and replace the "select * from" with "delete" as
  5094.          shown below.
  5095.          
  5096.          delete from sales
  5097.          where  cc in (
  5098.            select code
  5099.            from   cust
  5100.            where  rating < 10);
  5101.          
  5102.          The delete command below shows how to delete all rows in the
  5103.          sales table that do not have a corresponding customer code in the
  5104.          cust table. The not exists is covered in the full documentation.
  5105.          
  5106.          delete from sales
  5107.          where not exists
  5108.              (select *
  5109.               from cust
  5110.               where code = cc);
  5111.          
  5112.          HOW TO RESTORE DATA YOU HAVE JUST DELETED
  5113.          
  5114.          Type it in again!!! (Therefore be very careful when you use this
  5115.          command).
  5116.          
  5117.  
  5118.  
  5119.  
  5120.  
  5121.  
  5122.  
  5123.  
  5124.  
  5125.  
  5126.  
  5127.  
  5128.  
  5129.  
  5130.  
  5131.  
  5132.  
  5133.  
  5134.  
  5135.  
  5136.  
  5137.  
  5138.  
  5139.  
  5140.  
  5141.  
  5142.  
  5143.  
  5144.                                 DELETE-2
  5145.  
  5146.  
  5147.          TUTORIAL
  5148.          
  5149.          OVERVIEW
  5150.          This tutorial will take you through the basics creating a tables,
  5151.          inserting data into a table, updating rows in a table, deleting
  5152.          rows from a table, retrieving data from a table, creating a view
  5153.          of a table and dropping a table.  It is designed to give you a
  5154.      feel for the types of things that SSQL can do.  The detailed 
  5155.      description of the syntax is in the main section of the 
  5156.      documentation.
  5157.          
  5158.          START SSQL
  5159.          From the DOS prompt type SSQL and press ENTER.
  5160.      There will be a Title screen.  The next key you press will be part 
  5161.      of the next action so let's first explore the help screens.
  5162.      The shareware version has full help for the editor.  The version
  5163.      for registered users adds full help for SQL syntax and examples.
  5164.      
  5165.  
  5166.          CREATE THE TABLE
  5167.          Before we can put any data into the table, you need to describe
  5168.          the type(s) of data that it is going to contain.  Often the table
  5169.          we create is referred to as a base table.  Base tables contain
  5170.          actual data which is distinguished from a derived table (see
  5171.          Create a View). It is assumed that you have already read the
  5172.          introduction which contains the basic definitions of columns,
  5173.          rows, and tables. You will create a shortened version of a
  5174.          payroll table.  For each employee you will store the last name,
  5175.          the first name, salary, and city.  The key will be
  5176.          a combination of last name and first name. Type the following:
  5177.          
  5178.          create table pay (
  5179.      code char(2) not null unique,
  5180.          last char(15) not null,
  5181.          first char(15) not null,
  5182.          salary numeric(5),
  5183.          city char(12),
  5184.      tot_sold numeric(7,2)
  5185.          );
  5186.          The employee code is up to three characters long and is the primary
  5187.      key so we are telling it that there must be a value for this column
  5188.      (not null) and each one must be unique. Last name and first name can
  5189.      each be up to fifteen characters in length.  The values for city are 
  5190.      limited to twelve characters.  Salary is limited to five digits 
  5191.      which means up to $99,999. Total items sold is limited to $99,999.99.
  5192.          You can edit the above description until it is correct.  Once it
  5193.          is correct press:
  5194.          F4
  5195.          This will process the command.  If you have correct syntax, there
  5196.          will be a message box in the middle of the screen stating
  5197.          Table Created
  5198.          Otherwise an error message will appear and you can go back and
  5199.      correct it.
  5200.  
  5201.                                 TUTORIAL-1
  5202.  
  5203.  
  5204.          At this point we can either save what we just typed or delete it
  5205.      and type something new.  Let's delete this one and save the next one.
  5206.  
  5207.      What we want to do is quickly delete all the text on the screen.
  5208.      In editor terms, we want to delete a block of text. Let's find out
  5209.      how to do it by looking it up in the help screens.
  5210.  
  5211.          TO ACCESS THE ON-LINE HELP
  5212.      Let's assume that you want to find help on marking and deleting 
  5213.      blocks of text.
  5214.  
  5215.          Press F1.  
  5216.  
  5217.      You will notice the following list with the first item highlighted:
  5218.      Quick Start for SQL Commands
  5219.      Text Editor
  5220.      File
  5221.      Edit
  5222.      Search
  5223.      Window
  5224.      SQL
  5225.  
  5226.      Since marking and deleting text refers to the text editor, press
  5227.      the TAB key once to highlight "Text Editor" and press the ENTER
  5228.      key.
  5229.      The next screen gives some basic information but we want information
  5230.      specifically for blocks.  It is the first item so it is already
  5231.      highlighted.  Press ENTER.
  5232.      It starts with a description of how to mark a block:
  5233.      "Move the cursor to one end of the block.  While holding down the 
  5234.      shift key, use the arrow keys to move to the other end of the block.
  5235.      Now it is marked"
  5236.  
  5237.      In about the middle of the screen, it gives you more than one way to
  5238.      delete a block: Ctrl-KY, Ctrl-Del, or Del.  The reason there is more
  5239.      than one way is to cover a variety of standards.
  5240.  
  5241.      Press ESC to get out of the help screen.
  5242.      
  5243.  
  5244.          Assuming that the cursor is at the end of the command, you can
  5245.          delete the command to create the table by pressing:
  5246.          Shift-PgUp, Del
  5247.          (What it actually does is mark all the text and then you delete the
  5248.          block you marked)
  5249.  
  5250.  
  5251.      Let's create another table which will be need to make joins and 
  5252.      subqueries. Type:
  5253.  
  5254.      create table trans (
  5255.      trans_code numeric(3) not null unique,
  5256.      sales_prsn char(2) not null,
  5257.      prod char(2) not null,
  5258.      qty numeric(3) not null,
  5259.      cost numeric(5,2) not null
  5260.      );
  5261.  
  5262.                                 TUTORIAL-2
  5263.  
  5264.  
  5265.      Notice that the window is untitled (top center of the window).  Let's
  5266.      save it under the file name TRANS.CR.  Do the following:
  5267.      ALT F <-this means hold down the ALT key and press F
  5268.  
  5269.      Now you are looking at the File screen. The 'a' in "Save As" is
  5270.      highlighted so press the A key.
  5271.  
  5272.      A file name box appears. Type the following and press ENTER:
  5273.      TRANS.CR
  5274.      
  5275.          Now at the top of the window, the name of the file is TRANS.CR. We
  5276.      want to start a new window.  To keep our "desk-top" clean, we should
  5277.      close the existing window before openning a new one. 
  5278.      Press ALT F3 to close the window.
  5279.  
  5280.      To open a new window press ALT-F to get to the file menu and press
  5281.      N to open a new file.
  5282.  
  5283.      Now it is time to put data into the table.  I offer two ways to do
  5284.      it.  Adding data to a table in standard SQL is very cumbersome so
  5285.      I have created a "Form" program to make it easier.  For the PAY table
  5286.      I will use standard SQL.  For the TRANS table, I will use SSQLFORM.
  5287.  
  5288.          INSERT DATA INTO THE TABLE
  5289.          The following has has the commands to insert four rows of data 
  5290.          into the table. When you type them, be careful to include all the
  5291.          appropriate quotes and commas.  And yes, include the incorrect
  5292.          spelling of Scottsdale.  You will correct it in the next section.
  5293.          Note that Hippity Hopper has a null in place of his
  5294.          salary.  This means that the salary is not available which is
  5295.          different from putting a salary of zero.
  5296.  
  5297.          With long commands, it is a better idea to press the F5 to make
  5298.          the text editing full-screen. You can type all of the lines before
  5299.          pressing F4.  However, if there is an error on one of the lines,
  5300.          it will be more difficult to determine which line it is on.
  5301.          
  5302.          insert into pay values('AA','Everski','Willy',45000,'Scottsdale',0);
  5303.          (press F4)
  5304.          (Shift-PgUp,Del)
  5305.          insert into pay values('BB','Everski','Wilshe',60000,'Scootsdale',0);
  5306.          (press F4)
  5307.          (Shift-PgUp,Del)
  5308.          insert into pay values('CC','Hopper','Hippity',null,'Phoenix',0);
  5309.          (press F4)
  5310.          (Shift-PgUp,Del)
  5311.          insert into pay values('DD','Nosebleed','Harvey',20000,'Peoria',0);
  5312.          (press F4)
  5313.          (Shift-PgUp,Del)
  5314.  
  5315.                                 TUTORIAL-3
  5316.  
  5317.  
  5318.          Although the above is SQL syntax, SSQL allows a much easier way
  5319.          to fill the tables with data.  Let's add data to the TRANS table
  5320.      the easy way.  Press
  5321.          ALT-Q
  5322.      to access the SQL menu.  Press
  5323.      F
  5324.          to access the FORMS program which allows flexible data entry.
  5325.          Type TRANS.DBF for the file name.  After you have entered all the
  5326.          data, press ESC to exit the FORMS program.
  5327.  
  5328.          1 
  5329.      AA         
  5330.      r4     
  5331.      3  
  5332.      3.45 
  5333.  
  5334.          2 
  5335.      AA         
  5336.      r6     
  5337.      1  
  5338.      2.50 
  5339.  
  5340.          3 
  5341.      BB         
  5342.      r4     
  5343.      5  
  5344.      6.78 
  5345.  
  5346.  
  5347.          If you made any mistakes, you should be able to correct them
  5348.          after going through the next section on updates.
  5349.          
  5350.          UPDATE DATA IN A TABLE
  5351.          You need to make some changes to the table.  
  5352.      Scootsdale needs to be changed to Scottsdale 
  5353.  
  5354.  
  5355.  
  5356.          update pay
  5357.          set city = 'Scottsdale'
  5358.          where city = 'Scootsdale';
  5359.          
  5360.          (press F4)
  5361.          (Shift-PgUp,Del)
  5362.  
  5363.      The salary for everyone needs to be increased by 10%.
  5364.  
  5365.          update pay
  5366.          set salary = salary * 1.1;
  5367.          
  5368.          (press F4)
  5369.          (Shift-PgUp,Del)
  5370.  
  5371.                                 TUTORIAL-4
  5372.  
  5373.  
  5374.          Let's do something a bit more complex.  Total the sales (cost*qty)
  5375.      by Willy Everski ('AA') from the TRANS table and update the 
  5376.      TOT_SOLD column in the pay table.  To do that type the following:
  5377.          
  5378.      update pay
  5379.      set tot_sold =
  5380.          (select sum(cost*qty) from trans
  5381.           where sales_prsn = 'AA')
  5382.      where code = 'AA';
  5383.  
  5384.          (press F4)
  5385.          (Shift-PgUp,Del)
  5386.  
  5387.          DELETE A ROW FROM A TABLE
  5388.          Harvey Nosebleed has been fired so you want to delete the row
  5389.          that contains the data for Harv.  
  5390.  
  5391.      delete from pay
  5392.          where  last='Nosebleed';
  5393.  
  5394.          (press F4)
  5395.          (Shift-PgUp,Del)
  5396.          
  5397.  
  5398.      In a manner similar to the update, the where clause describes which
  5399.          row should be deleted.
  5400.  
  5401.          
  5402.          RETRIEVE DATA FROM A TABLE
  5403.          The select command is used to retrieve data from a table.  The
  5404.          following select command retrieves all the data in the table. 
  5405.          
  5406.          select *
  5407.          from pay;
  5408.  
  5409.          (press F4)
  5410.          
  5411.      CODE LAST            FIRST           SALARY CITY         TOT_SOLD 
  5412.      ---- --------------- --------------- ------ ------------ -------- 
  5413.      AA   Everski         Willy            49500 Scottsdale      12.85 
  5414.      BB   Everski         Wilshe           66000 Scottsdale       0.00 
  5415.      CC   Hopper          Hippity                Phoenix          0.00 
  5416.  
  5417.  
  5418.  
  5419.          The above is in a text window on the right of the screen.  Notice
  5420.          that the screen is cut off at the salary amount.  The best solution
  5421.          is to press F5 to make the text window full-screen.  (Remember to
  5422.          press F5 again to shrink it).  Otherwise you can move the cursor
  5423.          to the heading line and press the END key to scroll to the end of
  5424.          the line.
  5425.  
  5426.                                 TUTORIAL-5
  5427.  
  5428.  
  5429.          Let's do two optional operations:
  5430.          Edit it.  
  5431.         Add the heading "THE COMPLETE PAY TABLE" at the top of the
  5432.         window.
  5433.          Print it. Press ALT-F,P to print the contents of a text window.
  5434.  
  5435.  
  5436.          Now erase it. Press ALT-F3 to close the window. It will ask whether
  5437.      you want to save the changes.  Press N.
  5438.  
  5439.          (press Shift-PgUp,Del)
  5440.  
  5441.      WHAT HAPPENS WHEN YOU FORGET THE NAMES OF THE COLUMNS
  5442.      Before we go any further, it may be useful to find out the names
  5443.      of the columns in a table.  The struct command will do that for us.
  5444.      Type:
  5445.  
  5446.      struct pay;
  5447.  
  5448.                             COLUMN DECIMAL NOT
  5449.      COLUMN NAME  DATA TYPE  WIDTH  PLACES  NULL  UNIQUE  INDEX
  5450.      CODE         character      3           X       X    
  5451.      LAST         character     15           X            
  5452.      FIRST        character     15           X            
  5453.      SALARY       numeric        5     0                  
  5454.      CITY         character     12                        
  5455.      TOT_SOLD     numeric        7     2                  
  5456.  
  5457.  
  5458.          (press F4)
  5459.      (You won't be able to see all of it in the narrow window so press
  5460.      F5 to zoom the window).
  5461.          (press ALT-F3 to close the window)
  5462.          (Shift-PgUp,Del)
  5463.          
  5464.          The following shows how to retrieve the first name and last name for
  5465.          all the rows.
  5466.          
  5467.          select first, last
  5468.          from pay;
  5469.  
  5470.          (press F4)
  5471.          
  5472.          first           last            
  5473.          --------------- --------------- 
  5474.          Willy           Everski         
  5475.          Wilshe          Everski         
  5476.          Hippity         Hopper          
  5477.          
  5478.  
  5479.          (press ALT-F3 to close the window)
  5480.          (press Shift-PgUp,Del)
  5481.  
  5482.                                 TUTORIAL-6
  5483.  
  5484.  
  5485.          The following displays the rows of employees which
  5486.          have salaries of less than $58,000.  Notice that the row for
  5487.          Hippity Hopper is not displayed because all nulls are excluded. 
  5488.          
  5489.          select *
  5490.          from pay
  5491.          where salary < 58000;
  5492.  
  5493.          (press F4)
  5494.          
  5495.          last          first         salary city         
  5496.          ------------- ------------- ------ ------------ 
  5497.          Everski       Willy          49500 Scottsdale   
  5498.          
  5499.  
  5500.          (press ALT-F3 to close the window)
  5501.          (press Shift-PgUp,Del)
  5502.  
  5503.     *****************************************************************
  5504.     * FROM NOW ON I WILL ASSUME THAT YOU KNOW ABOUT THE F4, ALT-F3, *
  5505.     * AND SHIFT-PGUP, DEL
  5506.     *****************************************************************
  5507.  
  5508.      ELIMINATING DUPLICATE ROWS
  5509.      We can eliminate duplicate rows with the DISTINCT modifier.  The
  5510.      TRANS table has three rows but two of them have duplicate product
  5511.      codes.  We can display a list of all the DIFFERENT product codes
  5512.      with:
  5513.  
  5514.      select distinct prod
  5515.      from trans;
  5516.     
  5517.      prod 
  5518.      ---- 
  5519.      r4   
  5520.      r6   
  5521.     
  5522.      AGGREGATE FUNCTIONS (AVG, MIN, MAX,SUM,COUNT)
  5523.      
  5524.      These functions operate on the whole table so they cannot normally
  5525.      exist with simple column names.
  5526.  
  5527.      The SUM function adds items in a numeric column as in:
  5528.  
  5529.      select sum(salary) 
  5530.      from pay;
  5531.  
  5532.      SUM(salary) 
  5533.      ----------- 
  5534.           115500 
  5535.  
  5536.                                 TUTORIAL-7
  5537.  
  5538.  
  5539.          The COUNT function simply counts the number of rows that have
  5540.      values.  The ones that contain NULL are not counted.  You can
  5541.      use a shortcut with the '*' to count all rows:
  5542.  
  5543.      select count(*)
  5544.      from pay;
  5545.  
  5546.      CNT(*) 
  5547.      ------ 
  5548.           3 
  5549.  
  5550.      However when we count the number of rows with salary, only two 
  5551.      are listed because Hippity Hopper doesn't have a salary.
  5552.  
  5553.      select count(salary)
  5554.      from pay;
  5555.  
  5556.      CNT(salary) 
  5557.      ----------- 
  5558.                2 
  5559.  
  5560.      When you calculate the average salary, it will correctly divide by
  5561.      two, not three:
  5562.  
  5563.      select avg(salary)
  5564.      from pay;
  5565.  
  5566.      AVG(salary) 
  5567.      ----------- 
  5568.            57750 
  5569.  
  5570.  
  5571.  
  5572.          CALCULATIONS
  5573.          You can do calculations but you have to be careful.  It will take
  5574.          us three times to get it the way we want.
  5575.          Let's assume we want to display the qty, cost and the extended cost
  5576.          for each item.  Type:
  5577.  
  5578.  
  5579.          select qty, cost, qty*cost 
  5580.          from trans;
  5581.  
  5582.          qty  cost      qty 
  5583.          --- ----- -------- 
  5584.            3  3.45       10 
  5585.            1  2.50        2 
  5586.            5  6.78       34 
  5587.  
  5588.          Hey! That's not right!  3 * 3.45 is not just 10, it is 10.35. Well,
  5589.          by putting qty first in the list of calculations, it based the 
  5590.          calculation on the data type of qty which is a whole number so
  5591.          the decimal portion did not appear.
  5592.  
  5593.                                 TUTORIAL-8
  5594.  
  5595.  
  5596.          Let's try it again. Type:
  5597.  
  5598.          select qty, cost, cost*qty
  5599.          from trans;
  5600.  
  5601.      qty  cost       cost 
  5602.      --- ----- ---------- 
  5603.        3  3.45      10.35 
  5604.        1  2.50       2.50 
  5605.        5  6.78      33.90 
  5606.  
  5607.      That is better but we need a better heading.  If you don't change
  5608.      the heading, it will be the first column name in the calculation.
  5609.      Let's change the heading to ExtCost:
  5610.  
  5611.          select qty, cost, cost*qty ExtCost
  5612.          from trans;
  5613.  
  5614.      qty  cost      ExtCost 
  5615.      --- ----- ------------ 
  5616.        3  3.45        10.35 
  5617.        1  2.50         2.50 
  5618.        5  6.78        33.90 
  5619.  
  5620.      SORTING THE ROWS
  5621.      Let's print all the rows in the TRANS table but sort them by 
  5622.      product code:
  5623.  
  5624.      select *
  5625.      from trans
  5626.      order by prod;
  5627.  
  5628.      TRANS_CODE SALES_PRSN PROD QTY  COST 
  5629.      ---------- ---------- ---- --- ----- 
  5630.               1 AA         r4     3  3.45 
  5631.               3 BB         r4     5  6.78 
  5632.               2 AA         r6     1  2.50 
  5633.  
  5634.     
  5635.      GROUP INFORMATION
  5636.      Now for something a bit more advanced let's count how many sales
  5637.      EACH salesperson made.  We can see above that AA made two sales
  5638.      and BB made one sale.  We are going to group (re-arrange) the 
  5639.      information by SALES_PRSN and count the number of rows based on
  5640.      each value in the group:
  5641.  
  5642.      select sales_prsn, count(sales_prsn)
  5643.      from trans
  5644.      group by sales_prsn;
  5645.  
  5646.      sales_prsn CNT(sales_prsn) 
  5647.      ---------- --------------- 
  5648.              AA               2 
  5649.              BB               1 
  5650.  
  5651.                                 TUTORIAL-9
  5652.  
  5653.  
  5654.      THE JOIN
  5655.      The power of SQL is seen through the more advanced commands. The
  5656.      join allows us to merge tables.  We need to specify a common column
  5657.      between the tables.  In our example, the column CODE in the pay
  5658.      table describes the same information as SALES_PRSN in the trans
  5659.      table.
  5660.      
  5661.      Let's display the name of the salesperson along with the product
  5662.      code and the quantity sold.
  5663.  
  5664.      select first, last, prod, qty
  5665.      from pay, trans
  5666.      where code=sales_prsn; 
  5667.  
  5668.      first           last            prod qty 
  5669.      --------------- --------------- ---- --- 
  5670.      Willy           Everski         r4     3 
  5671.      Willy           Everski         r6     1 
  5672.      Wilshe          Everski         r4     5 
  5673.  
  5674.     
  5675.      THE SUBQUERY
  5676.      The typical subquery starts processing at the last subquery and 
  5677.      works its way back up. Let's display the names of all salespersons
  5678.      who have sold product which cost less than $5.  The bottom query
  5679.      gathers the codes of persons who have sold products less than $5.
  5680.      That information is passed back to the top query which finds the
  5681.      names based on the codes.
  5682.  
  5683.      select first, last
  5684.      from pay
  5685.      where code in
  5686.         (select sales_prsn
  5687.         from trans
  5688.         where cost < 5);
  5689.  
  5690.  
  5691.  
  5692.      first           last            
  5693.      --------------- --------------- 
  5694.      Willy           Everski         
  5695.  
  5696.          
  5697.  
  5698.          CREATE A VIEW
  5699.          A view is an alternate way of looking at a table or even a
  5700.          combination of tables.  It is also known as a derived table
  5701.          because a view is derived from one or more base tables. We will
  5702.          create a view which excludes salary information.  The view is
  5703.          based on a select statement. The view accesses data in the base
  5704.          table every time it is used in a select statement.  There is
  5705.          never any data contained in the view.  The following creates the view:
  5706.  
  5707.          create view pay1 as
  5708.          select code, last, first, city
  5709.          from pay;
  5710.          (press F4)
  5711.  
  5712.                                 TUTORIAL-10
  5713.  
  5714.  
  5715.          Now let's see what happens when all the columns of the view are
  5716.          selected.
  5717.          
  5718.          select *
  5719.          from pay1;
  5720.          (press F4)
  5721.  
  5722.  
  5723.      CODE LAST            FIRST           CITY         
  5724.      ---- --------------- --------------- ------------ 
  5725.      AA   Everski         Willy           Scottsdale   
  5726.      BB   Everski         Wilshe          Scottsdale   
  5727.      CC   Hopper          Hippity         Phoenix      
  5728.          
  5729.          (press ALT-F3 to close the window)
  5730.          (press Shift-PgUp,Del)
  5731.  
  5732.          DROP A TABLE
  5733.          When a table is dropped, the description of the table is erased
  5734.          from the disk.  The drop command will only work on an empty
  5735.          table.  The delete command is needed to delete all the data in 
  5736.          the table then the drop command erases the table from the disk.  
  5737.          
  5738.          delete from pay;
  5739.          (press F4)
  5740.          (press Shift-PgUp,Del)
  5741.  
  5742.          drop table pay;
  5743.          (press F4)
  5744.          (press Shift-PgUp,Del)
  5745.  
  5746.          drop view pay1;
  5747.          (press F4)
  5748.          (press Shift-PgUp,Del)
  5749.  
  5750.          EXIT SSQL
  5751.          Press ALT-X to exit.  When it asks you whether you want to save
  5752.          the untitled file, press N.
  5753.  
  5754.  
  5755.                                 TUTORIAL-11
  5756.  
  5757.