home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / ssql23.zip / SSQL.DOC < prev    next >
Text File  |  1990-11-11  |  175KB  |  4,510 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.                                      SSQL
  20.                                   VERSION 2.3
  21.  
  22.  
  23.               COPYRIGHT (C) 1987, 1988, 1989, 1990 BY STEVE SILVA
  24.     
  25.                             SILVAWARE
  26.                             3902 NORTH 87TH STREET
  27.                             SCOTTSDALE, AZ 85251
  28.  
  29.  
  30.  
  31.                      Special thanks to the hard-working 
  32.                      students in CIS425 at the DeVry 
  33.                      Institute of Technology, Phoenix, Arizona.
  34.  
  35.  
  36.  
  37.  
  38.          ***************************************************************
  39.          ** READ THIS!!!!!!!                                          **
  40.          ** IF YOU HAVE A PREVIOUS VERSION OF SSQL, YOU NEED CONVERT  **
  41.          ** YOUR FILES.  FROM YOUR DIRECTORY (or disk) THAT CONTAINS  **
  42.          ** THE .SQD FILES, TYPE A:CONVERT (assuming that the new     **
  43.          ** new SSQL disk is in drive A).                             **
  44.          ***************************************************************
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.          dBase is a trademark of Ashton-Tate.
  60.  
  61.  
  62.                                  TABLE OF CONTENTS
  63.          
  64.          
  65.          INTRODUCTION  . . . . . . . . . . . . . . . . . . . . . . . INTRO-1
  66. ***********WHAT'S NEW WITH SSQL *** READ!!!  . . . . . . . . . . . . INTRO-1
  67.            Key Words Needed to Understand the Documentation  . . . . INTRO-2
  68.            What Is SQL And Why Is It So Important To Know? . . . . . INTRO-3
  69.            Differences In The Registered Version . . . . . . . . . . INTRO-3
  70.            Install SSQL  . . . . . . . . . . . . . . . . . . . . . . INTRO-4
  71.            Permission to copy  . . . . . . . . . . . . . . . . . . . INTRO-4
  72.            Changes from 1.4 to 2.3 . . . . . . . . . . . . . . . . . INTRO-5
  73.            Syntax Diagrams . . . . . . . . . . . . . . . . . . . . . INTRO-5
  74.            SSQL Specifications . . . . . . . . . . . . . . . . . . . INTRO-7
  75.            Contents of Disk  . . . . . . . . . . . . . . . . . . . . INTRO-7
  76.            dBase Compatibility . . . . . . . . . . . . . . . . . . . INTRO-8
  77.         
  78.          THE EXAMPLE DATABASE  . . . . . . . . . . . . . . . . . . . DATABASE-1
  79.          USING YOUR OWN WORD PROCESSOR FROM WITHIN SSQL  . . . . . . WORD-1
  80.          USING SCRIPT FILES  . . . . . . . . . . . . . . . . . . . . SCRIPT-1
  81.          
  82.          EXTRACTING DATA FROM A SINGLE TABLE . . . . . . . . . . . . SELECT-1
  83.            Distinct  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-2
  84.            Where . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-3
  85.            search_expression . . . . . . . . . . . . . . . . . . . . SELECT-3
  86.            Special Search Expression - is null, is not null  . . . . SELECT-5
  87.            Special Search Expression - like, not like  . . . . . . . SELECT-6
  88.            And, Or, Not  . . . . . . . . . . . . . . . . . . . . . . SELECT-7
  89.            Any . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-11
  90.            In. . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-12
  91.            All . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-12
  92.            Between constant AND constant . . . . . . . . . . . . . . SELECT-13
  93.            Importance of the NULL (Unknown value)  . . . . . . . . . SELECT-13
  94.            Mathmatical Functions Avg, Min, Max, Sum, Count . . . . . SELECT-15
  95.            Column Calculations (+, -, *, /)  . . . . . . . . . . . . SELECT-18
  96.            Group by, Having  . . . . . . . . . . . . . . . . . . . . SELECT-19
  97.            Order by  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-20
  98.            Redirectto  . . . . . . . . . . . . . . . . . . . . . . . SELECT-21
  99.       
  100.          JOINING . . . . . . . . . . . . . . . . . . . . . . . . . . JOIN-1
  101.          SIMPLE SUBQUERIES . . . . . . . . . . . . . . . . . . . . . SUB-1
  102.          CORRELATED SUBQUERIES . . . . . . . . . . . . . . . . . . . COR-1
  103.          CONVERTING ENGLISH TO SQL . . . . . . . . . . . . . . . . . ENG-1
  104.          CREATE A TABLE  . . . . . . . . . . . . . . . . . . . . . . CREATE-1
  105.          CREATE A VIEW . . . . . . . . . . . . . . . . . . . . . . . VIEW-1
  106.          DROP A TABLE/VIEW . . . . . . . . . . . . . . . . . . . . . DROP-1
  107.          INSERT DATA INTO A TABLE  . . . . . . . . . . . . . . . . . INSERT-1
  108.          UPDATE DATA IN A TABLE  . . . . . . . . . . . . . . . . . . UPDATE-1
  109.          DELETE DATA FROM A TABLE  . . . . . . . . . . . . . . . . . DELETE-1
  110.          TUTORIAL  . . . . . . . . . . . . . . . . . . . . . . . . . TUTORIAL-1
  111.          FORM GENERATOR (DATA ENTRY) . . . . . . . . . . . . . . . . FORM-1
  112.          REPORT GENERATOR  . . . . . . . . . . . . . . . . . . . . . REPORT-1
  113.  
  114. ******** ORDER FORM  * * * * * * * * * * * * * * * * * * * * * * * * LAST PAGE
  115.          (for another copy type from DOS: copy order.frm prn)
  116.          
  117.          
  118.          WHAT'S NEW WITH SSQL (besides the bug fixes)
  119.  
  120.          Well, my product has been around for about three years now and 
  121.          I have found that many people want more than just a learning tool - 
  122.          they want to actually USE it!  I have addressed the following 
  123.          problems in the current version:
  124.  
  125.          1.  The major problem with SSQL has been that it is SLOOOOOOW 
  126.          when large tables are joined.  
  127.          Indexes are now available to increase processing speeds 
  128.          dramatically.  Although the shareware version allows you to 
  129.          create indexes, only the registered version with the index 
  130.          option will process indexes.  However, the results are dramatic. 
  131.          I did the following test on an IBM PC AT.  
  132.  
  133.                    TIME IN SECONDS TO DISPLAY VARIOUS JOINS
  134.          Number of Rows in Tables     SSQLPro  Oracle 5.1  dBase IV 1.1
  135.          100 X 100                       5.5      16           38
  136.          100 X 100 X 100                10.0      22           39
  137.          1000 X 1000 (10 displayed)      3.5     123           24
  138.          1000 X 1000 (all displayed)    45.0     157          108
  139.  
  140.          The dBase results could probably be made better by some fine-
  141.          tuning.  My current version implements only the most critical 
  142.          aspects of indexing.  Indexes are used when testing for 
  143.          equality and when the tests are linked with ANDs.  I am working 
  144.          on ORs and ORDER BY.
  145.          Without indexes, (i.e. the shareware version) SSQL would take 
  146.          MANY hours to join two tables with 1000 rows each.  I got tired 
  147.          trying to find out exactly how long.
  148.  
  149.          2.  Data Entry is difficult at best.
  150.          I have included an easy-to-use program that generates data 
  151.          entry screens called FORM.  The screens can be easily modified 
  152.          by any text editor.  Besides the basic add, update, view, and 
  153.          delete, you can create help screens, cross-reference data in 
  154.          another table, test for ranges,  and test for a certain set of 
  155.          data.  You can move back and forth from entry to entry and 
  156.          there is full data editing capabilities.  See the section on 
  157.          the FORM program for more details.
  158.  
  159.          3.  Report formatting is primitive.
  160.          I have included a report generator that does all the basics - 
  161.          subtotals, grand totals, page breaks, page numbering, current 
  162.          date, headings, footers, calculations, etc.  See the section on 
  163.          the REPORT program for more details.
  164.  
  165.          4.  There is no programming interface
  166.          Now you can include SSQL in your favorite programming language 
  167.          (including batch files).  I have created an extensive command 
  168.          line inteface that I use in the FORM and REPORT programs.  The 
  169.          interface allows you to access everything that SSQL can do.  
  170.          You can call it from within C (spawn) or even a batch file.  
  171.          Although I haven't tried it, I assume it will work with Pascal 
  172.          and even some dialects of Basic.  Since you are calling an 
  173.  
  174.                                       INTRO-1
  175.  
  176.  
  177.          executable file, it doesn't matter which brand of compiler you 
  178.          are using.  
  179.  
  180.          I also offer the Turbo C source code for FORM and REPORT to 
  181.          show how the calls to SSQL can be integrated into a program.  I 
  182.          admit that I am not real proud of the code I produced in 
  183.          version 1.0.  I started off with the intention of offering the 
  184.          code so I tried to write it in an easy-to-understand style.  
  185.          Well, as often happens, the problem was a bit more involved 
  186.          than I thought and there are a few kludges here and there. Oh 
  187.          well, it may give you some ideas!!  See the order form for 
  188.          details.
  189.  
  190.          No, I do not offer the source code of SSQL.
  191.  
  192.          KEY WORDS NEEDED TO UNDERSTAND THE DOCUMENTATION
  193.  
  194.          SQL - Structured Query Language. A standard method of 
  195.          interacting with a database.  It is pronounced "SEQUEL"!!  
  196.          However,  over the years I have noticed more and more people 
  197.          saying the letters S-Q-L.
  198.          
  199.          TABLE - A table is typically known as a FILE in other systems.
  200.          You may ask why they don't just call a table a file.  It is 
  201.          because that although normally a table does refer to a specific 
  202.          file, a table can refer to something that spans two or more 
  203.          files.  This can be done by "creating a view" (see 
  204.          documentation). If you read a book on relational databases, they 
  205.          will probably refer to a table as a relation. 
  206.  
  207.          ROW - A row corresponds to a record or a portion of a record in 
  208.          a file.  In relational theory it is called a tuple.
  209.  
  210.          COLUMN - A column is typically known as a field in other 
  211.          systems.  In relational theory it is called an attribute.
  212.  
  213.          The above names were created to give relational databases a 
  214.          consistent and accurate view of data.
  215.  
  216.          EXAMPLE:
  217.          You may have a TABLE named sales which contains COLUMNs called 
  218.          date, custnum, partnum and quantity.  Every time you made a 
  219.          sale, you would add a ROW of data to the TABLE.
  220.  
  221.                       COLUMNS
  222.            -----------------------------
  223.           |         |        |         |
  224.          date    custnum  partnum  quantity
  225.          ------  -------  -------  --------
  226.          880201  8524     AD873         928       <-- ROW
  227.          880203  7687     VF8709         87       <-- ROW
  228.          ----------------------------------
  229.                         ^
  230.                         |
  231.                       TABLE 
  232.  
  233.                                       INTRO-2
  234.  
  235.  
  236.          WHAT IS SQL AND WHY IS IT SO IMPORTANT TO KNOW?
  237.          
  238.          SQL stands for Structured Query Language.  However, it is more 
  239.          than just a query language. You can add update and delete 
  240.          information too.  Virtually every popular database from PCs to 
  241.          mainframes support this language.  
  242.  
  243.  
  244.          DIFFERENCES IN THE REGISTERED VERSION
  245.          
  246.          PROGRAM
  247.          There is no difference in the registered version although if 
  248.          you register, you can get a version (for an extra $10) that 
  249.          processes indexes.  Indexes can make joins (combining data from 
  250.          different files) 40 times or more faster. 
  251.  
  252.          DISK-BASED DOCUMENTATION (UNREGISTERED USERS)
  253.          In order to fit everything on a single disk, there is only 
  254.          enough documentation on some commands to explain the basics. 
  255.          However, it does show all the ANSI SQL commands so you can see 
  256.          for yourself that the commands actually work. You do not get 
  257.          documentation on most of the utilities that make working in 
  258.          this environment a bit easier and allow you, for example, to 
  259.          output any query to a dBase file which gives you the ability to 
  260.          customize reports. 
  261.  
  262.          FULL DOCUMENTATION (REGISTERED USERS)
  263.  
  264.          Actually, it is much more than just documentation.  It is a 
  265.          book with nearly 400 pages.  It was produced with a desk-top 
  266.          publishing package, printed on a laser printer, and bound. 
  267.  
  268.          The name of the book is APPLYING SQL.  Besides explaining all 
  269.          the details of syntax, it shows the common constructs used in 
  270.          SQL to make creating your own queries much easier.  Much of 
  271.          each chapter is centered around translating English into SQL 
  272.          and how to note peculiarities and ambiguities in English that 
  273.          can cause problems in writing queries.
  274.  
  275.          Support is offered by mail or through CompuServe (73177,2771).
  276.  
  277.          One of the most complex (and useful) topics in SQL is the 
  278.          correlated subquery.  Most authors give you a page or two on 
  279.          the topic.  That is not nearly enough for you to be able to use 
  280.          it on your own.  My chapter on correlated subqueries is over 40 
  281.          pages.  One good gauge on the quality of a book on SQL is how 
  282.          well it explains the correlated subquery and how well it 
  283.          conveys the importance of the topic.  It is an essential part 
  284.          of SQL that opens up various categories of queries that cannot 
  285.          be accomplished any other way.
  286.          
  287.          The last chapter on queries is CONVERTING ENGLISH TO SQL.  It 
  288.          gives you over 50 pages on approaches to converting English to 
  289.          SQL and dozens of examples.  It explains how to break down a 
  290.          sentence in English that simplifies its conversion.  Many of 
  291.  
  292.                                       INTRO-3
  293.  
  294.  
  295.          the common words used in queries are analyzed.  As is often the 
  296.          case with English, the same word in different contexts can 
  297.          significantly change the meaning of the sentence and its 
  298.          conversion to SQL.  Much of the chapter explains how to convert 
  299.          sentences with negation which can be VERY tricky.  For 
  300.          example, let's anylze the following query:
  301.          
  302.          List the purchase dates when all of the following products were 
  303.          purchased: MW, NM, GC. 
  304.          
  305.          The query, although a bit complex when solving it SQL, is 
  306.          nonetheless rather straightforward.  When we negate the query 
  307.          it gets even more confusing (and realistic!!): 
  308.  
  309.          List the purchase dates when all of the following products were 
  310.          NOT purchased: MW, NM, GC. 
  311.          (if all the products were purchased, skip the date) 
  312.  
  313.          List the purchase dates when NONE of the following products were 
  314.          purchased: MW, NM, GC.
  315.          (if any of the products were purchased, skip the date)
  316.  
  317.          List the purchase dates when something other than the following 
  318.          products were purchased: MW, NM, GC. 
  319.  
  320.          Once you see the solutions to these queries you will better 
  321.          understand why SQL is "structured" and it should give you a 
  322.          more critical understanding of English queries.
  323.  
  324.          The full chapters on the joins and subqueries have many more 
  325.          examples and detailed explanations.
  326.  
  327.          The power of SQL can be TOTALLY lost if the user does not 
  328.          understand the basics of data normalization. Normalization 
  329.          involves the rules for creating tables.  If the tables are not 
  330.          organized correctly, SQL cannot be used to its full potential.  
  331.          It is important to note that the topic is discussed with the 
  332.          non-technical end-user in mind.  Since there have not been any 
  333.          widely available SQL programs, all the books on normalization 
  334.          tend to be very theoretical and academically oriented.  
  335.  
  336.          You also get full information on utilities to delete columns from 
  337.          tables, modify column names, change the width of columns, create 
  338.          tables which are subsets of a current table, etc. 
  339.  
  340.          INSTALL SSQL
  341.          There is no installation per se.  Normally you just want to 
  342.          copy the contents of the distribution disk into a subdirectory 
  343.          on your hard disk.  If you have a floppy disk system put the  
  344.          SSQL disk in drive A and a blank formatted disk in drive B.  
  345.          With the DOS prompt on drive A type:
  346.          COPY SSQL.EXE B:
  347.          COPY ERROR.DAT B:
  348.          COPY *.DBF B:
  349.          COPY *.SQD B:
  350.  
  351.                                       INTRO-4
  352.  
  353.  
  354.          If you want the form generator and the report generator:
  355.          COPY FORM.EXE B:
  356.          COPY REPORT.EXE B:
  357.  
  358.          The only things you have to remember is that SSQL.EXE and 
  359.          ERROR.DAT have to be in the same directory and the database 
  360.          files must be in your default directory.
  361.          Here is some more information about the files on the 
  362.          distribution disk.. 
  363.          SSQL.EXE - The executable program. 
  364.          ERROR.DAT - contains the error messages
  365.          The other files (tables, views, etc.) must be in your current 
  366.          directory.  In essence, your directory corresponds to your 
  367.          complete database. 
  368.          MEANING OF FILE EXTENSIONS
  369.          DBF - The dbase files.  On the distribution disk, you have 
  370.          BRANCH.DBF, SALES.DBF, PROD.DBF, MANU.DBF, EMP.DBF and 
  371.          CUST.DBF.
  372.          SQD - Definition files.  There is one for every DBF file.
  373.          VIE - The view files. These are text files that contain SELECT 
  374.          statements which form the views.
  375.          TXT - Table output to disk.  These files are created when you 
  376.          use the REDIRECTTO option in the SELECT command to save your 
  377.          report in a disk file instead of sending it to the printer.
  378.  
  379.          EDIT.FIL - This contains the command to start your word 
  380.          processor or text editor from within SSQL.  See the section on 
  381.          USING YOUR OWN WORD PROCESSOR for more details on this. 
  382.          
  383.          You can put SSQL.EXE and ERROR.DAT in a single directory and 
  384.          set a path to it. You can then have various other directories 
  385.          for the tables. 
  386.          
  387.          PERMISSION TO COPY
  388.  
  389.          Please copy this disk and give it to a friend (or anybody else).
  390.          However, the following restrictions apply:
  391.          1.  No changes can be made to the distribution disk, including the 
  392.          documentation.
  393.          2. You cannot copy or reproduce the printed manual.  
  394.          
  395.          Any commercial, educational, governmental and other such 
  396.          organizations are required to purchase a copy of SSQL for every 
  397.          building it is used in. 
  398.  
  399.          Quantity discounts available.
  400.          
  401.          CHANGES FROM 1.4 TO 2.2
  402.  
  403.          Bug fixes too numerous to mention.
  404.          dBase compatible files
  405.          Row width increased from 132 to 500 characters
  406.          Create table, create view, insert, update and delete are ANSI 
  407.          standard 
  408.          Add count(*)
  409.  
  410.                                       INTRO-5
  411.  
  412.  
  413.          Addition of drop table, drop view, and correlated subqueries 
  414.          Ability to use calculations in the select list, in the where 
  415.          clause, and update
  416.          The UNION command
  417.          table size increased from 20K to amount of memory.
  418.          The non-standard 'into' is changed to 'redirectto' to eliminate 
  419.          the conflict with the ANSI 'into'
  420.          Complete revision of documentation for registered users.
  421.  
  422.         CHANGES FROM 2.2 TO 2.2a
  423.         Corrects message of "null pointer assignment" and other potential 
  424.         problems when accessing files created by dBase. 
  425.         Date comparisons in where clause work now.
  426.         
  427.         CHANGES FROM 2.2a to 2.2b
  428.         Correctly handles column names with 10 characters
  429.         CREATE TABLE now allows decimal, integer, smallint, float and 
  430.         double precision.
  431.         Can use LIKE with date column but you must use an underline for a 
  432.         wild card - not the %. 
  433.  
  434.         CHANGES FROM 2.2b TO 2.3
  435.         OOPS!! I lost my list of bug fixes but there were about 10 or so.
  436.  
  437.  
  438.         SYNTAX DIAGRAMS
  439.         Many chapters begin with a syntax diagram that concisely
  440.         describes a command and its options.  It takes some practice to
  441.         read them but there are only four basic components:
  442.  
  443.         lowercase_letters - they describe something that you must supply. 
  444.         The rules for correctly writing this part of the command follow
  445.         in the documentation.  It may represent a single word, such as
  446.         the syntax for table_name or a rather complicated series of words
  447.         that is needed for a column_definition.
  448.  
  449.         [  ] - items between square brackets are optional.
  450.  
  451.         ...   -  three dots mean previous item may be repeated.
  452.  
  453.         UPPER CASE LETTERS and all other symbols such as '(', ')', ';',
  454.         '.', and ','  - these must be typed as shown.  Although when
  455.         typing the command, you can use lower case instead of upper case
  456.         letters.  For example, refer to the syntax diagram for creating 
  457.         tables.
  458.         
  459.         Since CREATE TABLE is in uppercase, it must
  460.         always be typed as shown.  The items in lowercase such as
  461.         table_name, column_definition, and uniqueness_constraint must be
  462.         replaced by information you supply.  The rules for forming the
  463.         phrases required by the words in lowercase will be contained in
  464.         the chapter.  The item called column_definition is repeated.  The
  465.         first occurance is outside of the square brackets which means
  466.         that it is required.  The second occurance of column_definition
  467.         is within square brackets which means that it is optional.  There
  468.  
  469.                                       INTRO-6
  470.  
  471.  
  472.         is a comma after the left square bracket which means that if you
  473.         add another column_definition, there must be a comma before it. 
  474.         There are three dots after the second occurance of
  475.         column_definition which means that column_definitions can be more
  476.         than two column_definitions.  Other symbols, such as '(', ')' and
  477.         ';' must be included at the positions shown in the example.  In
  478.         SQL, spacing and lines make no difference.  You can create a
  479.         table with all the components on a single line or put each word
  480.         on a separate line.
  481.  
  482.  
  483.         SSQL SPECIFICATIONS
  484.  
  485.         Requires only one floppy diskette 
  486.         Program only takes up about 160K
  487.         Minimum Memory 320K
  488.         Maximum number of columns 128
  489.         Maximum length of a row 500 bytes (characters)
  490.         Maximum number of rows 
  491.         It is limited by memory. With a 640K PC, the OUTPUT size of the
  492.         table cannot exceed about 400K.  The tables on the disk can be
  493.         larger, you just could not display all the rows and columns at
  494.         once.  Also, you could join multiple large tables as long as the
  495.         resultant table was not over 400K. This means that the output 
  496.         of a table with rows 100 characters wide could contain about 
  497.         4,000 rows (about 70 pages of report).
  498.         
  499.         Maximum levels of subqueries 14 (reduced by # of indexes in use)
  500.         Maximum number of tables you can join 14 (reduced by # of indexes)
  501.         
  502.         CONTENTS OF DISK
  503.         READ.ME       - Basic information
  504.         SSQL.EXE      - The executable file
  505.         ERROR.DAT     - Contains error messages
  506.         EDIT.FIL      - The text file with the location of your word processor
  507.         BRANCH.DBF, PROD.DBF, EMP.DBF, MANU.DBF, SALES.DBF, CUST.DBF,
  508.         BRANCH.SQD, PROD.SQD, EMP.SQD, MANU.SQD, SALES.SQD, CUST.SQD,
  509.                       - The tables used in the documentation
  510.         SSQL.DOC      - Documentation
  511.         ORDER.FRM     - Order form. type: COPY ORDER.FRM PRN 
  512.                         for a printout
  513.         PRINTDOC.BAT  - The batch file to print the documentation
  514.         CONVERT.EXE   - Converts the older .SQL files to .DBF and .SQD
  515.         RUN           - ALTHOUGH THIS IS NOT ON THE DISK IT WILL BE AFTER
  516.                         THE FIRST TIME YOU RUN SSQL.  IT CONTAINS YOUR
  517.                         LAST COMMAND
  518.         FORM.EXE      - The form program - data entry, update, delete, 
  519.                         view
  520.         REPORT.EXE    - The report generator
  521.         RP1.FRM TO RPT7.FRM - Report formats for REPORT.EXE
  522.  
  523.  
  524.                                       INTRO-7
  525.  
  526.  
  527.          DBASE COMPATIBILITY
  528.  
  529.          SSQL creates dBase III compatible files and it can read any 
  530.          dBase file with the following exceptions:
  531.                 No memo data type
  532.                 No field length over 132
  533.  
  534.          A significant difference between SQL and dBase is the ability 
  535.          of SQL to store a null (see SELECT-13 for more information).  
  536.          Even dBase IV cannot store a null.  The way I have done it is 
  537.          that character columns are filled with blanks, and numeric 
  538.          columns contain -0 (negative zero).  You will never see a -0 
  539.          when using SSQL, the column value will be blank.  This 
  540.          information might be useful if you manipulate files created by 
  541.          SSQL in dBase.
  542.  
  543.          You will notice that every .DBF file created by SSQL has a 
  544.          corresponding .SQD file.  This stores information specific to 
  545.          file creation - Not Null and Unique (see CREATE-2 for more 
  546.          information).  If you access a dBase file not 
  547.          created through SSQL, the .DBF file is created with Not Null set 
  548.          for every column and no uniqueness constraints. 
  549.  
  550.          If you change the number of fields in a dBase file after it has 
  551.          been accessed by SSQL, you should delete the corresponding .SQD 
  552.          file. Otherwise, the Not Null and Uniqueness constraints will 
  553.          not be correct.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.                                 INTRO-8
  560.  
  561.  
  562.  
  563.  
  564.          
  565.          THE EXAMPLE DATABASE - MONOLITH MANUFACTURING
  566.          Monolith Manufacturing manufactures and markets high tech
  567.          products in the western states.  There are manufacturing
  568.          facilities in various states.  Products are manufactured in
  569.          batches at each facility.  When the batch is finished an entry is
  570.          made in the table with the date the batch was finished, the
  571.          product code, the state where the product was manufactured, the
  572.          quantity of saleable items and the percent of defects in the
  573.          batch if that is tracked.  Related to the manufacturing table is
  574.          the product table which simply has the description that
  575.          corresponds to each product code.
  576.          The sales portion of the business has as its core the sales
  577.          table.  When a sale is made, an entry is made which contains the
  578.          date of sale, branch code, customer code, salesperson number,
  579.          product code and quantity purchased.  Related to the sales table
  580.          are the branch table, the employee table and the customer table. 
  581.          The branch table contains the branch code, the state the branch
  582.          is in and the employee number of the manager.  The employee table
  583.          contains the employee number, the name of the employee, and the
  584.          employee's manager number if the employee has a manager (the
  585.          president does not have a manager).  All manager numbers have an
  586.          entry in the employee number column since all managers are also
  587.          employees.  The customer table contains the customer code, the
  588.          customer name and the state where the customer resides.
  589.          The following contains details on the Monolith's six tables.  The
  590.          data types correspond to the types used in the create table
  591.          command.  When a column in one table corresponds to a column
  592.          (especially a primary key) in another table, it is in the form of
  593.          TABLE.column. This information will be useful when we need to
  594.          retrieve information that spans tables. 
  595.          
  596.          SALES TABLE
  597.          column  data                               Common columns in 
  598.          name    type       description             other tables
  599.          date    date       Date of sale 
  600.          bc      char(2)    Branch code             BRANCH.code
  601.          cc      char(2)    Customer code           CUST.code
  602.          sn      numeric(2) Salesperson number      EMP.code,
  603.                                                  EMP.mgrnum, BRANCH.mgrnum
  604.          pc      char(2)    product code            PROD.code, MANU.code
  605.          qty     numeric(3) quantity
  606.  
  607.          Key is date, bc, cc, sn, pc
  608.  
  609.  
  610.  
  611.          
  612.          
  613.          
  614.          
  615.          
  616.  
  617.          
  618.                                DATABASE-1
  619.  
  620.  
  621.  
  622.  
  623.          BRANCH TABLE
  624.          column   data                              Common columns in 
  625.          name     type       description            other tables
  626.          code     char(2)    branch code            SALES.bc
  627.          st       char(2)    state code             CUST.st, MANU.mst
  628.          mgrnum   numeric(2) manager number         EMP.enum,
  629.                                                     EMP.mgrnum, SALES.sn
  630.          KEY is code
  631.          
  632.          EMP TABLE
  633.          column   data                              Common columns in 
  634.          name     type       description            other tables
  635.          enum     numeric(2) employee number        EMP.mgrnum, BRANCH.mgrnum, 
  636.                                                     SALES.sn 
  637.          name     char(20)   employee name
  638.          mgrnum   numeric(2) manager number         EMP.enum,
  639.                                                     BRANCH.mgrnum, SALES.sn
  640.          KEY is enum
  641.  
  642.          PROD TABLE
  643.          column   data                              Common columns in 
  644.          name     type       description            other tables
  645.          code     char(2)    product code           MANU.code, SALES.pc
  646.          descrip  char(15)   product description
  647.          KEY is code        
  648.  
  649.          MANU TABLE
  650.          column   data                               Common columns in 
  651.          name     type       description             other tables
  652.          date     date       date of manufacture
  653.          code     char(2)    product code            PROD.code
  654.          mst      char(2)    state where product     BRANCH.st, PROD.st
  655.                              is manufactured
  656.          defects  numeric(3) percent defects in batch  
  657.          qty      numeric(3) quantity manufactured
  658.          KEY is date, code, ms
  659.  
  660.          CUST TABLE
  661.          column   data                               Common columns in 
  662.          name     type       description             other tables
  663.          code     char(2)    Unique customer code    SALES.cc
  664.          name     char(15)
  665.          st       char(2)    State                   MANU.mst, PROD.st
  666.          rating   numeric(2)  
  667.          KEY is code
  668.          
  669.          
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.          
  677.                                DATABASE-2
  678.  
  679.          
  680.          
  681.          
  682.          USING YOUR WORD PROCESSOR
  683.  
  684.          You can modify any SSQL command by calling your text editor or
  685.          word processor from within SSQL.  You must first specify which
  686.          text editor or word processor you are using in a file called
  687.          EDIT.FIL.  The file must contain the path and name of the text
  688.          editor or word processor.  YOU CANNOT CALL A BATCH FILE.  The
  689.          distribution disk contains an EDIT.FIL with the following:
  690.          C:\PCWRITE\ED
  691.          The command to start PC-Write is ED and it resides on drive C: in
  692.          the PCWRITE directory.  You can replace the above line with the
  693.          path and program name of your own. The EDIT.FIL must reside on
  694.          your default directory. The syntax is:
  695.          EDIT [file_name];
  696.          The file_name is any valid name which will be accepted by your
  697.          word processor on the command line.  For example, if you want to
  698.          edit MGRORG, you would type:
  699.          edit mgrorg;
  700.          As is true with all commands except calling a script file, you
  701.          must end this one with a ';'.  When you exit your word
  702.          processor/text editor, you will return to SSQL.  Refer to the
  703.          next section to find out how to process the file you just
  704.          created.
  705.          There is a special case when you want to edit the last SSQL
  706.          command that you typed.  Every command you type except edit and
  707.          exit is stored in a file called RUN.  To edit the last command
  708.          entered type:
  709.          edit run;
  710.          If your word processor does not allow you to type the file name
  711.          that you want to edit after the word processor command in
  712.          EDIT.FIL simply type:
  713.          edit;
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.          
  736.                                    WORD-1
  737.  
  738.          
  739.          
  740.          
  741.          CALL SCRIPT FILE 
  742.  
  743.          A script file is a text file which contains an SSQL command.  The
  744.          script file can then be called from SSQL and the command it
  745.          contains will be processed.
  746.          
  747.          SYNTAX:
  748.          @file_name 
  749.          
  750.          The file_name can be any valid DOS file name.
  751.          The file must be text only, no special word processing formats
  752.          allowed. PC-Write or Qedit are excellent choices because they
  753.          always produce standard text. If you use WordPerfect, save the
  754.          file using Text Out (Ctrl-F5). If you use Wordstar, I feel sorry
  755.          for you but anyway, use the non-document format. 
  756.          
  757.          COMMENTS IN THE SCRIPT FILE
  758.          You can precede a comment by two dashes and from that point to
  759.          the end of the line will not be processed by SSQL. The comment
  760.          can be at any place in the line, not necessarily at the beginning
  761.          as the following example shows.
  762.          You can create a file called MGRORG which contains:
  763.          --This query finds the names of managers who have
  764.          -- salespeople who have sold products to Organomice
  765.          select name      --Get names
  766.          from emp      
  767.          where enum in 
  768.            (select mgrn   --of managers
  769.             from emp 
  770.             where enum in   
  771.               (select sn        -- who have salespeople
  772.                from sales,cust  -- who have sold products to
  773.                where cc = code  -- Organomice
  774.                and name = 'Organomice')); 
  775.          
  776.          From SSQL, you would simply type: 
  777.          
  778.          @MGRORG 
  779.  
  780.          The file you created would be displayed on the screen and then 
  781.          the result would appear. 
  782.          The script file can contain multiple commands.
  783.          
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.                             SCRIPT-1
  796.  
  797.  
  798.          QUERIES - SINGLE TABLE
  799.  
  800.          OVERVIEW
  801.  
  802.          The select command is used to retrieve data from tables. The
  803.          select command is covered in three later chapters too.  Although
  804.          the use of select on a single table is relatively easy,
  805.          translating from English to SQL (or most other query languages)
  806.          can be rather tricky.  The approach of this chapter is not merely
  807.          to explain the syntax of the command, it should give you a more
  808.          critical approach to applying SQL.
  809.  
  810.          SYNTAX: 
  811.  
  812.          SELECT [DISTINCT] column_name [,column_name ...] 
  813.          FROM table_name [,table_name ...] 
  814.          [WHERE search_expression] 
  815.          [REDIRECTTO file_name]         <--Non-Standard 
  816.          [GROUP BY column_name [,column_name ...] 
  817.          [HAVING criteria]]
  818.          [ORDER BY column_name [,column_name ...] 
  819.  
  820.          If you want all the columns in a table you can replace the list
  821.          of column names with an asterisk (*). 
  822.          The select command produces a report in the form of a table.
  823.          Although the basic use of the select takes a short time to learn,
  824.          the more advanced uses take a long time to master. 
  825.          The tables used in this chapter are on the distribution disk so
  826.          you can try entering the commands yourself.
  827.          All the examples have each clause starting on a new line for
  828.          purposes of clarity. It is not necessary to have them on separate
  829.          lines. It is sometimes advantageous to keep the query on one line
  830.          because if you need to make a correction, you cannot backup a
  831.          line. However, to make correcting command much easier, you can
  832.          call your word processor from within SSQL. This is described in
  833.          the full documentation.
  834.  
  835.          THE BASICS OF SELECT 
  836.  
  837.          Find all the data in the manu table.
  838.          
  839.          select *  
  840.          from manu; 
  841.          date     code mst defects qty  
  842.          -------- ---- --- ------- ---  
  843.          02/07/87 GC   ID       12  15 
  844.          02/01/87 GC   ID        0  55  
  845.          02/02/87 NM   CA       17  93  
  846.          02/02/87 DD   ID           25  
  847.          02/03/87 DD   WA       22  46  
  848.          02/02/87 NM   WA       15  25  
  849.          02/04/87 DD   AZ       12  25  
  850.          02/04/87 DD   CA       15  25  
  851.          02/06/87 GC   AZ        4  43  
  852.           
  853.  
  854.                                 SELECT-1
  855.  
  856.          
  857.          Instead of typing all the column names, you can use the * to
  858.          denote all column names. On the next line, the "from manu" tells
  859.          us that we want the data to come from the table named "manu". All
  860.          select commands must end with a semi-colon (;). If we just wanted
  861.          the product code and the state that it was manufactured in you
  862.          would type:
  863.          
  864.           select code, mst 
  865.           from manu; 
  866.           code mst  
  867.           ---- ---  
  868.           GC   ID   
  869.           GC   ID   
  870.           NM   CA   
  871.           DD   ID   
  872.           DD   WA   
  873.           NM   WA   
  874.           DD   AZ   
  875.           DD   CA   
  876.           GC   AZ   
  877.            
  878.           9 rows selected 
  879.          
  880.          DISTINCT 
  881.          The optional statement "distinct" will eliminate all rows with
  882.          duplicate columns that you have specified. If you wanted to find
  883.          out all the different states where products are manufactured, you
  884.          would type:
  885.          
  886.          select distinct mst 
  887.          from manu; 
  888.           
  889.          mst  
  890.          ---  
  891.          ID   
  892.          CA   
  893.          WA   
  894.          AZ   
  895.           
  896.          4 rows selected 
  897.          
  898.          As you can see only 4 rows were found. This is because  there can
  899.          be more than one row with the same state. Only the first
  900.          occurrence of each data element is retained. 
  901.          The "distinct" modifier can be used with one or more column names
  902.          in a select statement. However, the "distinct" modifier operates
  903.          on the whole row, not just a single column. In the manu table,
  904.          the primary key is date, code, mst. What if we wanted not only
  905.          the state where the products are manufactured but the product
  906.          codes too. Notice what happens with the following:
  907.  
  908.  
  909.  
  910.  
  911.          
  912.          
  913.                                 SELECT-2
  914.  
  915.          
  916.          select mst, code 
  917.          from manu; 
  918.           
  919.          mst code  
  920.          --- ----  
  921.          ID  GC    
  922.          ID  GC    
  923.          CA  NM    
  924.          ID  DD    
  925.          WA  DD    
  926.          WA  NM    
  927.          AZ  DD    
  928.          CA  DD    
  929.          AZ  GC    
  930.          
  931.          Some of the rows are duplicated because it is possible to
  932.          manufacture the same product in the same state but on a different
  933.          date. To get rid of the duplication of rows we can add the
  934.          "distinct" modifier.
  935.          
  936.          WHERE 
  937.          
  938.          The where clause of the select command is the most powerful and
  939.          complex part of SQL. Our discussion will be broken down into
  940.          various phases. The first phase is using the where with a single
  941.          table name. Later you will see it used with multiple tables and
  942.          you will find that you can even put another select command within
  943.          the where clause. 
  944.          
  945.          search_expression 
  946.          
  947.          The simplest search expression relates a column name to a
  948.          constant. A constant is an exact value that you enter. All column
  949.          values are tested against the constant value. If the column name
  950.          that you are testing is defined as a character, the constant must
  951.          be enclosed in single quotes or double quotes. The constant you
  952.          are searching for may not contain a quote. If the column name you
  953.          are testing for is defined as numeric, the constant is a number
  954.          without any quotes. 
  955.          You can relate the column name to the constant in the following
  956.          ways: 
  957.          
  958.          = equal 
  959.          <> not equal 
  960.          != not equal 
  961.          ~= not equal
  962.          >  greater than 
  963.            less than 
  964.          >= greater than or equal 
  965.          <=  less than or equal 
  966.          IS NULL   column value is null
  967.          IS NOT NULL   column value is not null
  968.          LIKE  like a pattern
  969.          NOT LIKE  not like a pattern
  970.  
  971.  
  972.                                 SELECT-3
  973.  
  974.  
  975.  
  976.          
  977.          Find out which customers are in Arizona. 
  978.          
  979.          select * 
  980.          from cust 
  981.          where st = 'AZ'; 
  982.           
  983.          code name            st rating  
  984.          ---- --------------- -- ------  
  985.          ZZ   Organomice      AZ     34  
  986.          DD   QuarkCo         AZ     10  
  987.           
  988.          2 rows selected 
  989.          
  990.          Spaces are optional on either side of the "=". You could have
  991.          typed: 
  992.          
  993.          where st='AZ'; 
  994.          
  995.          Instead of single quotes you could have used double quotes: 
  996.          
  997.          where st="AZ"; 
  998.          
  999.          In the manu table, the column name "defects" is the percent of
  1000.          defects when the product was manufactured. The statement below
  1001.          shows you how to get a list of all the information for those
  1002.          products with defects over 10%.
  1003.          
  1004.          select * 
  1005.          from manu 
  1006.          where defects > 10; 
  1007.           
  1008.          date     code mst defects qty  
  1009.          -------- ---- --- ------- ---  
  1010.          02/07/87 GC   ID       12  15  
  1011.          02/02/87 NM   CA       17  93  
  1012.          02/03/87 DD   WA       22  46  
  1013.          02/02/87 NM   WA       15  25  
  1014.          02/04/87 DD   AZ       12  25  
  1015.          02/04/87 DD   CA       15  25  
  1016.           
  1017.          6 rows selected 
  1018.          
  1019.          The symbols such as <, >, =, <=, >=, and != can be used with
  1020.          character data too. For example, the statement below shows how to
  1021.          retrieve all customer names alphabetically greater than Machado.
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.          
  1031.                                 SELECT-4
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.          
  1038.          select name 
  1039.          from cust 
  1040.          where name > "Machado"; 
  1041.           
  1042.          name             
  1043.          ---------------  
  1044.          Technoharps      
  1045.          Organomice       
  1046.          QuarkCo          
  1047.          Marswarp         
  1048.          Multicrud        
  1049.           
  1050.          5 rows selected 
  1051.          
  1052.          SPECIAL SEARCH EXPRESSION - IS NULL, IS NOT NULL 
  1053.          
  1054.          Nulls are an important concept in relational databases. For
  1055.          example, let's look at the percentage of defects in Idaho:
  1056.          
  1057.          select *
  1058.          from manu
  1059.          where mst = 'ID'; 
  1060.            
  1061.          date     code mst defects qty  
  1062.          ------   ---- --- ------- ---  
  1063.          02/07/87 GC   ID       12  15  
  1064.          02/01/87 GC   ID        0  55  
  1065.          02/02/87 DD   ID           25  
  1066.           
  1067.          3 rows selected 
  1068.           
  1069.          In Idaho, the batch of MW on 02/07/87 has 12% defects, GC on 02/01/87
  1070.          has 0% defects but there is nothing listed for DD. This "nothing"
  1071.          is called a null. It does not mean zero or blank. A null means
  1072.          that the data element is unknown. In our case, product DD in
  1073.          Idaho is not tracked for defects like other products. This isn't
  1074.          to say that there are zero defects. It just means we have no
  1075.          entry. 
  1076.          If we wanted to look at all products for which we do not track
  1077.          defects, we would use the following:
  1078.          
  1079.          select * 
  1080.          from manu 
  1081.          where defects is null; 
  1082.          date     code mst defects qty  
  1083.          -------- ---- --- ------- ---  
  1084.          02/02/87 DD   ID           25  
  1085.           
  1086.          1 row selected 
  1087.          
  1088.  
  1089.  
  1090.                                 SELECT-5
  1091.  
  1092.  
  1093.          
  1094.          We could find all the products for which we track defects by
  1095.          using is not null:
  1096.          
  1097.          select * 
  1098.          from manu 
  1099.          where defects is not null; 
  1100.          date     code mst defects qty  
  1101.          -------- ---- --- ------- ---  
  1102.          02/07/87 GC   ID       12  15  
  1103.          02/01/87 GC   ID        0  55  
  1104.          02/02/87 NM   CA       17  93  
  1105.          02/03/87 DD   WA       22  46  
  1106.          02/02/87 NM   WA       15  25  
  1107.          02/04/87 DD   AZ       12  25  
  1108.          02/04/87 DD   CA       15  25  
  1109.          02/06/87 GC   AZ        4  43  
  1110.           
  1111.          8 rows selected 
  1112.          
  1113.          Null values can be used with both numeric columns and character
  1114.          columns.
  1115.          
  1116.          SPECIAL SEARCH EXPRESSION - LIKE, NOT LIKE 
  1117.          
  1118.          An expression with like is used when we want to retrieve data
  1119.          that is similar in some way to another set of characters. The
  1120.          like expression can only be used with character data. This is
  1121.          explained in the full documentation.
  1122.          
  1123.          AND, OR, NOT 
  1124.          
  1125.          We can link expressions with ands and ors. If you use an and,
  1126.          both expressions have to be true in order to add the row to the
  1127.          table we are producing. If you use an or, only one of the
  1128.          expressions has to be true. You can use as many ands and ors as
  1129.          you want in a query. The expressions are evaluated from left to
  1130.          right. All the ands are done first, then evaluation starts again
  1131.          from the left and the ors are processed. 
  1132.          If you want to reference different column names in a search, the
  1133.          logic as stated in English is typically the same as it is in SQL.
  1134.          Usually there is no confusion between the and and the or unless
  1135.          you are dealing with the same column name. 
  1136.          Find all products in Washington with defects over 16%:
  1137.          
  1138.          select distinct code 
  1139.          from manu 
  1140.          where mst = 'WA' and defects > 16; 
  1141.           
  1142.          code  
  1143.          ----  
  1144.          DD    
  1145.           
  1146.          1 row selected 
  1147.  
  1148.  
  1149.                                 SELECT-6
  1150.  
  1151.          
  1152.          Generally, the use of a single and or an or in a where clause is
  1153.          more or less common sense unless you are referring to the same
  1154.          column name. What if you wanted to find all the products
  1155.          manufactured in Washington and Idaho? This has two meanings in
  1156.          everyday English: 
  1157.          
  1158.          1. Out of all the products we manufacture, I want a list of the
  1159.          ones we manufacture in Washington and a list of the ones we
  1160.          manufacture in Idaho. 
  1161.          
  1162.          2. Out of all the products we manufacture, I want a list of each
  1163.          one we manufacture in BOTH Washington and Idaho. 
  1164.          
  1165.          We will accept the first meaning in this example. It is
  1166.          interesting to note that we could have also said, "List all the
  1167.          products manufactured in Washington or Idaho." It seems odd that
  1168.          in English "or" can have the same meaning as "and". Even if you
  1169.          don't agree with my interpretation, beware that others do and
  1170.          that it may cause communication problems. 
  1171.          In the English version you use the "and" so there is a tendency
  1172.          to always use the "and" in the SQL version too. This would be
  1173.          wrong. The English perspective and the SQL perspective are
  1174.          different. The English perspective often looks at the whole group
  1175.          of items. The SQL perspective looks at one item at a time. To
  1176.          translate Washington and Idaho into SQL we look at it from its
  1177.          perspective. We will look at each item. If that item is
  1178.          manufactured in Washington or Idaho, we will add it to our table.
  1179.          Notice that when we look at it from the SQL perspective, we use
  1180.          the "or" instead of the "and". This not just a quirk of SQL
  1181.          logic, it is common to virtually all data bases. The statement
  1182.          below shows how to find out all the products manufactured in
  1183.          Washington and Idaho: 
  1184.          
  1185.          select distinct code 
  1186.          from manu 
  1187.          where mst='WA' or mst='ID'; 
  1188.           
  1189.          code  
  1190.          ----  
  1191.          GC    
  1192.          DD    
  1193.          NM    
  1194.           
  1195.          3 rows selected 
  1196.          
  1197.          It is interesting to note what the meaning would be if we used an
  1198.          and instead of an or. Would it tell us every product manufactured
  1199.          in BOTH Washington and Idaho? No, it wouldn't as you can see in
  1200.          the following:
  1201.          
  1202.          select distinct code 
  1203.          from manu 
  1204.          where mst='WA' and mst='ID'; 
  1205.           
  1206.          no rows found 
  1207.  
  1208.                                 SELECT-7
  1209.  
  1210.  
  1211.  
  1212.          
  1213.          Actually, it makes no sense. How could a state for an individual
  1214.          item be both WA and ID????? 
  1215.          
  1216.          But how WOULD you find out every product manufactured in both
  1217.          Washington and Idaho? The search strategy is rather complex and
  1218.          the topic is covered under joining tables and again when we
  1219.          discuss subqueries. 
  1220.          
  1221.          In order to express the opposite logic, you use the not but you
  1222.          have to be careful. The statement below shows how to find the
  1223.          products manufactured outside of Washington or Idaho. 
  1224.          
  1225.          select * 
  1226.          from manu 
  1227.          where not (mst='WA' or mst='ID'); 
  1228.          
  1229.          date     code mst defects qty  
  1230.          -------- ---- --- ------- ---  
  1231.          02/02/87 NM   CA       17  93  
  1232.          02/04/87 DD   AZ       12  25  
  1233.          02/04/87 DD   CA       15  25  
  1234.          02/06/87 GC   AZ        4  43  
  1235.           
  1236.          4 rows selected 
  1237.          
  1238.          Again, we must be careful about how we express the query in
  1239.          English. The query above cannot be translated as: Find the
  1240.          products NOT manufactured in Washington or Idaho. The query above
  1241.          retrieved some products that happen to be manufactured in Idaho
  1242.          or Washington. We really did not care. The only constraint we
  1243.          cared about was the state in which it was manufactured. The query
  1244.          could accurately be translated in fractured English: Find the
  1245.          products manufactured not in Washington or Idaho. By changing the
  1246.          statement to one that requests products not manufactured in
  1247.          Washington or Idaho, we change the words modified by "not" from
  1248.          the states to products manufactured. To find the products not
  1249.          manufactured in Washington or Idaho we must first find the set of
  1250.          all products manufactured in Washington or Idaho:
  1251.          
  1252.          select distinct code 
  1253.          from manu 
  1254.          where mst = 'WA' or mst = 'ID'; 
  1255.          
  1256.          code  
  1257.          ----  
  1258.          GC    
  1259.          DD    
  1260.          NM    
  1261.           
  1262.          3 rows selected 
  1263.  
  1264.  
  1265.  
  1266.  
  1267.                                 SELECT-8
  1268.  
  1269.  
  1270.  
  1271.          
  1272.          Then we must find all the products in the other states that are
  1273.          not in the above set of three products. We end up with the
  1274.          following which finds that there are not any products
  1275.          manufactured in the other states that are not manufactured in
  1276.          Washington and Idaho.
  1277.          
  1278.          select distinct code 
  1279.          from manu 
  1280.          where code != 'NM' and code != 'GC' and code != 'DD'; 
  1281.           
  1282.          no rows found
  1283.          
  1284.          Well, you may ask, what IS the select statement to find the
  1285.          products not manufactured in Washington or Idaho? Not so fast.
  1286.          Actually, it would require a subquery that is covered in the
  1287.          section on subqueries. 
  1288.          
  1289.          If I may digress...
  1290.          
  1291.          I know. Just a few pages into the basics of the select statement
  1292.          and you are ready to give up. Remember, the logic of SQL is not
  1293.          all that difficult. The main problem is that you do not think
  1294.          critically about the meaning of sentences. Much has been written
  1295.          about the problems of communication between humans and computers.
  1296.          Most of the problems are actually caused by the end-user and the
  1297.          "computer expert". I put the burden on the "computer expert" who
  1298.          should know enough about English to note the subtlties and
  1299.          ambiguities of what the end-user is requesting. This is just as
  1300.          relevant for interpreting queries as it is building complete
  1301.          application systems.
  1302.          
  1303.          Meanwhile, back at the ranch...
  1304.          
  1305.          Normally, if a condition is true, we add that row to the table.
  1306.          By using the not, if the condition is false, we add the row to
  1307.          the table. On the first row of the select statement to find
  1308.          products manufactured outside Idaho and Washington, the state is
  1309.          CA. It was selected because it is not equal to WA or ID. Just as
  1310.          we can express the same thought in English more than one way, we
  1311.          can do the same with SQL. We could have written:
  1312.          
  1313.          select * 
  1314.          from manu 
  1315.          where mst != 'WA' and mst != 'ID'; 
  1316.          OR 
  1317.          select * 
  1318.          from manu 
  1319.          where not (mst = 'WA') and not (mst = 'ID'); 
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.          
  1326.                                 SELECT-9
  1327.  
  1328.  
  1329.  
  1330.          
  1331.          What if we would have written or instead of and in the two
  1332.          examples above? Every row would have been selected. No matter
  1333.          what the state is, it would either be not equal to WA or not
  1334.          equal to ID! 
  1335.          
  1336.          Remember that whenever you use the not, you must enclose the
  1337.          expression in parentheses. 
  1338.          
  1339.          You can have as many ands and ors in a query as you need. What if
  1340.          you wanted all products in Washington and Idaho with defects less
  1341.          than 16%?  
  1342.          
  1343.          select * 
  1344.          from manu 
  1345.          where (mst='WA' or mst='ID') and defects < 16; 
  1346.           
  1347.          date     code mst defects qty  
  1348.          -------- ---- --- ------- ---  
  1349.          02/07/87 GC   ID       12  15  
  1350.          02/01/87 GC   ID        0  55  
  1351.          02/02/87 NM   WA       15  25  
  1352.           
  1353.          3 rows selected 
  1354.          
  1355.          We find that only two products are selected. Notice that
  1356.          parentheses enclose the or expression. This is because without
  1357.          the parentheses the and expression would have been executed
  1358.          first. Remember that without parentheses the ands are processed
  1359.          first and the ors are processed next. Not inserting the
  1360.          parentheses would have changed the meaning considerably:
  1361.          
  1362.          select * 
  1363.          from manu 
  1364.          where mst='WA' or mst='ID' and defects < 16; 
  1365.           
  1366.          date     code mst defects qty  
  1367.          -------- ---- --- ------- ---  
  1368.          02/07/87 GC   ID       12  15  
  1369.          02/01/87 GC   ID        0  55  
  1370.          02/03/87 DD   WA       22  46  
  1371.          02/02/87 NM   WA       15  25  
  1372.           
  1373.          4 rows selected 
  1374.          
  1375.          The above query is the equivalent of saying, "List all products
  1376.          in Washington and list all products in Idaho with defects under
  1377.          16%."  Another way of stating it is "In looking through all the
  1378.          products, if a product is manufactured in Washington or an Idaho
  1379.          product has under 16% defects, add it to the list." The important
  1380.          concept is that the 16% defects relates to the products
  1381.          manufactured in Idaho, not all products. 
  1382.          
  1383.  
  1384.  
  1385.                                 SELECT-10
  1386.  
  1387.  
  1388.  
  1389.  
  1390.          
  1391.          SETS (used with constants) 
  1392.          
  1393.          Instead of relating items one at a time, we can relate groups of
  1394.          items at once. These groups are called sets. They are enclosed
  1395.          parentheses and the constants are separated by commas. 
  1396.          
  1397.          You saw above how you can use =, !=, <, >, <=, and >=. We can
  1398.          expand that concept to include any and all.  This chapter has a
  1399.          brief introduction to sets.  Sets are the main topic of the
  1400.          chapters on subqueries where instead of using constants in sets,
  1401.          we use select statements.
  1402.          
  1403.          ANY 
  1404.          
  1405.          As you will see, the use of the any with constants is simply a
  1406.          short-hand method of writing a series of comparisons with ors.
  1407.          The any may be combined with any of the comparison operators:
  1408.          =any  equal to any 
  1409.          >any  greater than any 
  1410.          <any  less than any 
  1411.          !=any  not equal to any 
  1412.          >=any  greater than or equal to any 
  1413.          <=any  less than or equal to any 
  1414.          
  1415.          Although there are many possible combinations with any, very few
  1416.          are of any use. With constants, the only one that is typically
  1417.          used is =any. 
  1418.          
  1419.          Let's rewrite the query concerning all products manufactured in
  1420.          Washington or Idaho:
  1421.          
  1422.          select distinct code 
  1423.          from manu 
  1424.          where mst =any ('WA','ID'); 
  1425.          code  
  1426.          ----  
  1427.          GC    
  1428.          DD    
  1429.          NM    
  1430.           
  1431.          3 rows selected 
  1432.          
  1433.          Translated into English it would be: Select the description from
  1434.          the manufacturing table where the state of manufacture is equal
  1435.          to any of the following - WA or ID. 
  1436.          We can use numbers too. What if you wanted to know the basic
  1437.          information on products that have a percentage of defects of 12%
  1438.          or 15%?
  1439.  
  1440.  
  1441.  
  1442.          
  1443.  
  1444.                                  SELECT-11
  1445.  
  1446.  
  1447.  
  1448.          select date, code, mst 
  1449.          from manu 
  1450.          where defects =any (12,15); 
  1451.          date     code mst  
  1452.          -------- ---- ---  
  1453.          02/07/87 GC   ID   
  1454.          02/02/87 NM   WA   
  1455.          02/04/87 DD   AZ   
  1456.          02/04/87 DD   CA   
  1457.           
  1458.          4 rows selected 
  1459.          
  1460.          IN 
  1461.          
  1462.          An equivalent to =any is in. The above query could be translated:
  1463.          
  1464.          select date, code, mst
  1465.          from manu  
  1466.          where defects in (12,15);  
  1467.          
  1468.          The above two queries could be translated into a query without a
  1469.          set:
  1470.          
  1471.          select date, code, mst 
  1472.          from manu 
  1473.          where defects = 12 or defects = 15; 
  1474.          
  1475.          ALL
  1476.          
  1477.          The all can be combined with all the comparison operators just as
  1478.          was seen with the any. The only common use with constants occurs
  1479.          with the !=all which allows us to exclude a set of data. In the
  1480.          following statement we show information on products manufactured
  1481.          outside of Washington and Idaho.
  1482.          
  1483.          select date, code, mst 
  1484.          from manu 
  1485.          where mst !=all('WA','ID'); 
  1486.           
  1487.          date     code mst  
  1488.          -------- ---- ---  
  1489.          02/02/87 NM   CA   
  1490.          02/04/87 DD   AZ   
  1491.          02/04/87 DD   CA   
  1492.          02/06/87 GC   AZ   
  1493.           
  1494.          4 rows selected 
  1495.          
  1496.          Although we translated =any as "equal to any item in the set",
  1497.          !=all is usually translated as "not equal to any item in the
  1498.          set". Another way to state the above query is to find products
  1499.          manufactured in a state that is not any of the following -
  1500.          Washington and Idaho.  Confusing, isn't it??  Just remember that
  1501.  
  1502.  
  1503.                                 SELECT-12
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.          
  1510.          !=all is the opposite of =any.  This ambiguity in English is
  1511.          related to a riddle I heard Theo ask on the Cosby show:  "What
  1512.          two coins add up to 30 cents and one of them is not a quarter?" 
  1513.          The answer, of course, is a nickel and a quarter.  The nickel is
  1514.          not a quarter which satisfies the rule.  Theo was applying a
  1515.          !=any to the set when a typical listener would think that !=all
  1516.          was actually meant.
  1517.          
  1518.          BETWEEN constant AND constant 
  1519.          An easy way to select a range of values is use the between/and
  1520.          component of the where clause. The following shows how to find
  1521.          the basic information for products with defects between 12% and
  1522.          16%: 
  1523.          
  1524.          select date, code, mst, defects 
  1525.          from manu 
  1526.          where defects between 12 and 16; 
  1527.           
  1528.          date     code mst defects  
  1529.          -------- ---- --- -------  
  1530.          02/07/87 GC   ID       12  
  1531.          02/02/87 NM   WA       15  
  1532.          02/04/87 DD   AZ       12  
  1533.          02/04/87 DD   CA       15  
  1534.           
  1535.          4 rows selected 
  1536.          
  1537.          Notice that when we use the between, we include the 12 and the
  1538.          16. Unfortunately, the word "between" can have more than one
  1539.          meaning in standard English. If you say "The treasure is located
  1540.          between San Francisco and Walnut Creek", you would assume that
  1541.          the treasure is not IN San Francisco or Walnut Creek. That is,
  1542.          you would not include the end points - San Francisco or Walnut
  1543.          Creek. If you say "This insurance plan is available for those
  1544.          people between the ages of 18 and 65", you would assume that if
  1545.          you are 18 or 65, you are eligible. That is, you would include
  1546.          the end points. In English, when the word between is used with
  1547.          numbers, the end points are included. In other cases, the end
  1548.          points are not included. Silly, isn't it!! Just remember that in
  1549.          SQL, between includes the end points.
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.          
  1562.                                   SELECT-13
  1563.  
  1564.  
  1565.  
  1566.  
  1567.          IMPORTANCE OF THE NULL (UNKNOWN VALUE)
  1568.          Now that you understand the basics of how the where clause works,
  1569.          we need to complicate it a bit with considering the unknown value
  1570.          (the null).  If the where clause evaluates to TRUE, then the row
  1571.          is displayed, otherwise, it is not displayed.  In most non-SQL
  1572.          databases, the where clause only evaluates to TRUE and FALSE.  In
  1573.          SQL we have TRUE, FALSE, and UNKNOWN.  We can use truth tables to
  1574.          show the relationship.
  1575.          
  1576.           AND  |TRUE  FALSE  UNK     OR   |TRUE  FALSE  UNK
  1577.           ----------------------     -----------------------
  1578.           TRUE |TRUE  FALSE  UNK     TRUE |TRUE  TRUE   TRUE
  1579.           FALSE|FALSE FALSE  FALSE   FALSE|TRUE  FALSE  UNK   
  1580.           UNK  |UNK   FALSE  UNK     UNK  |TRUE  UNK    UNK
  1581.          
  1582.           NOT
  1583.           ------------
  1584.           TRUE  |FALSE
  1585.           FALSE |TRUE
  1586.           UNK   |UNK
  1587.          
  1588.          This can cause some confusing results until you are used to it.
  1589.          For example, there are nine rows in the manu table.  However, the
  1590.          two queries below result in a total of only eight rows.  
  1591.          
  1592.          select *
  1593.          from manu
  1594.          where defects = 15;
  1595.          
  1596.          date     code mst defects qty 
  1597.          -------- ---- --- ------- --- 
  1598.          02/02/87 NM   WA       15  25 
  1599.          02/04/87 DD   CA       15  25 
  1600.          
  1601.          2 rows selected
  1602.          
  1603.          select *
  1604.          from manu
  1605.          where defects != 15;
  1606.          
  1607.          date     code mst defects qty 
  1608.          -------- ---- --- ------- --- 
  1609.          02/07/87 GC   ID       12  15 
  1610.          02/01/87 GC   ID        0  55 
  1611.          02/02/87 NM   CA       17  93 
  1612.          02/03/87 DD   WA       22  46 
  1613.          02/04/87 DD   AZ       12  25 
  1614.          02/06/87 GC   AZ        4  43 
  1615.          
  1616.          6 rows selected
  1617.          
  1618.  
  1619.  
  1620.  
  1621.                                   SELECT-14
  1622.  
  1623.  
  1624.          At first glance you would think that defects are either equal to
  1625.          fifteen or not equal to fifteen.  However there is one instance
  1626.          where defects has an unknown value so it is not displayed.
  1627.          Intuitively you may be saying to yourself that since an unknown
  1628.          value is not equal to fifteen, it should be displayed, but that
  1629.          is not the way it works.  If you look at both the AND truth table
  1630.          and OR truth table, there is no difference between the false and
  1631.          the unknown since a result of FALSE or UNKNOWN will prevent a row
  1632.          from being displayed.  However, in the truth table for NOT, there
  1633.          is a difference. The opposite of unknown is still unknown.
  1634.          
  1635.          AGGREGATE FUNCTIONS - AVG, MIN, MAX, SUM, COUNT 
  1636.          
  1637.          All the aggregate functions operate on numeric columns except
  1638.          count which will operate on any columns. 
  1639.          
  1640.          SYNTAX: 
  1641.          
  1642.          AVG([DISTINCT] column_name) 
  1643.          MIN(column_name) 
  1644.          MAX(column_name) 
  1645.          SUM([DISTINCT]column_name) 
  1646.          COUNT([DISTINCT]column_name) 
  1647.          
  1648.          SUMMARY 
  1649.          
  1650.          AVG - without DISTINCT: The average of all values in a column
  1651.          AVG - with DISTINCT: The average of non-duplicate values 
  1652.          MIN - The minimum value in the column 
  1653.          MAX - The maximum value in the column 
  1654.          SUM - without DISTINCT: The sum of all values in the column 
  1655.          SUM - with DISTINCT: The sum of non-duplicate values 
  1656.          COUNT - without DISTINCT: the number of column values 
  1657.          COUNT - with DISTINCT: the number of non-duplicate column values 
  1658.          
  1659.          Notice that some of the functions have the distinct option. This
  1660.          will exclude all rows in the table which have duplicate column
  1661.          values. The follwing two examples show the difference. 
  1662.          select count(mst) 
  1663.          from manu; 
  1664.          CNT(mst)  
  1665.          --------  
  1666.                 9  
  1667.           
  1668.          9 rows selected 
  1669.          
  1670.          The above query can be translated as: 
  1671.          How many rows have values in the mst column? There are a total of
  1672.          9. This is not very useful because every row is going to have an
  1673.          entry for mst. The answer will always be the total number of rows
  1674.          in the table since the mst column was created with the "not null"
  1675.          constraint. 
  1676.          The following query is typically more useful. It answers the
  1677.          question: "How many different states manufacture products?"
  1678.  
  1679.  
  1680.                                   SELECT-15
  1681.  
  1682.  
  1683.          select count(distinct mst) 
  1684.          from manu; 
  1685.          CNT(mst)  
  1686.          --------  
  1687.                 4  
  1688.           
  1689.          Typically, when you are using a column name with count, you will
  1690.          modify it with distinct. When querying a table with a multiple
  1691.          part key, all columns will have the possibility of having
  1692.          duplicate entries. This is the case with the manu table with a
  1693.          key of date, code, mst. With such tables, you will modify the
  1694.          column name with distinct. When you query a table that has a key
  1695.          based on a single column, the only column you can be assured of
  1696.          not having duplicates is the key column. If you want to count all
  1697.          the rows in the table, there is a short-cut:
  1698.          
  1699.          select count(*) 
  1700.          from manu; 
  1701.          
  1702.          CNT(*)  
  1703.          ------  
  1704.               9  
  1705.           
  1706.          At this point, you may ask what use the column name is without
  1707.          the distinct modifier? It is that wonderful concept of the null
  1708.          that has been touched on before and will be discussed in more
  1709.          detail about the other functions. The count function will not
  1710.          count rows that have a null value in the column you are counting.
  1711.          Actually, all the functions skip rows with nulls. In the employee
  1712.          table, not everyone has a corresponding manager number. The entry
  1713.          for employees without a manager is a null (actually only the
  1714.          president doesn't have a manager in this table). The following
  1715.          tells us how many employees have managers.
  1716.          
  1717.          select count(mgrnum) 
  1718.          from emp; 
  1719.          CNT(mgrnum)  
  1720.          -----------  
  1721.                   14  
  1722.           
  1723.          15 rows selected 
  1724.          
  1725.          This can be contrasted with the following query. Notice how
  1726.          simply adding the word "distinct" completely changes the meaning
  1727.          of the query to finding how many managers there are in the
  1728.          company.
  1729.          
  1730.          select count(distinct mgrnum) 
  1731.          from emp; 
  1732.          
  1733.          CNT(mgrnum)  
  1734.          -----------  
  1735.                    7  
  1736.  
  1737.  
  1738.           
  1739.                                 SELECT-16
  1740.  
  1741.  
  1742.  
  1743.          
  1744.          The functions also can be used with the where clause. The
  1745.          following shows how many batches of product DD are in the table.
  1746.          
  1747.          select count(*) 
  1748.          from manu 
  1749.          where code = 'DD'; 
  1750.          
  1751.          CNT(*)  
  1752.          ------  
  1753.               4  
  1754.           
  1755.          4 rows selected 
  1756.           
  1757.          Before we discuss the other functions, let's look at the
  1758.          percentage of defects in Idaho:
  1759.          
  1760.          select * 
  1761.          from manu 
  1762.          where mst = 'ID'; 
  1763.          date     code mst defects qty  
  1764.          -------- ---- --- ------- ---  
  1765.          02/07/87 GC   ID       12  15  
  1766.          02/01/87 GC   ID        0  55  
  1767.          02/02/87 DD   ID           25  
  1768.           
  1769.          3 rows selected 
  1770.          
  1771.          In Idaho, the are three entries: 12, 0, and "nothing". This
  1772.          "nothing" is called a null. Nulls are important in relational
  1773.          data bases. It does not mean zero or blank. A null means that the
  1774.          data element is not relevant in the row. In our case, product DD
  1775.          is not tracked for defects like other products. This isn't to say
  1776.          that there are zero defects. It just means we have no entry.
  1777.          Because there are only two valid entries for defects in Idaho,
  1778.          the functions will not take into account the null data element.
  1779.          The average percentage of defects will be 12/2 = 6, not 12/3 = 4:
  1780.          
  1781.          select avg(defects) 
  1782.          from manu 
  1783.          where mst='ID'; 
  1784.          AVG(defects)  
  1785.          ------------  
  1786.                     6  
  1787.           
  1788.          3 rows selected 
  1789.          
  1790.          
  1791.          The sum function totals the numeric values in the column(s)
  1792.          requested. Below we sum the number of product DD that has been
  1793.          manufactured.
  1794.  
  1795.  
  1796.  
  1797.  
  1798.                                 SELECT-17
  1799.  
  1800.          
  1801.          select sum(qty) 
  1802.          from manu 
  1803.          where code = 'DD'; 
  1804.          
  1805.          SUM(qty)  
  1806.          --------  
  1807.               121  
  1808.           
  1809.          4 rows selected 
  1810.          
  1811.          More than one function can be used in a query: 
  1812.          
  1813.          
  1814.          select avg(qty), sum(qty) 
  1815.          from manu 
  1816.          where code = 'DD'; 
  1817.           
  1818.          AVG(qty) SUM(qty)  
  1819.          -------- --------  
  1820.                30      121  
  1821.           
  1822.          4 rows selected 
  1823.          
  1824.          
  1825.          Except for the group by clause, you cannot retrieve column values
  1826.          and functions based on column values in the same query. 
  1827.          
  1828.          COLUMN CALCULATIONS (*, /, +, -)
  1829.          
  1830.          Numeric columns can be used in calculations.  The following
  1831.          symbols are used:
  1832.          
  1833.          * = multiplication
  1834.          / = division
  1835.          + = addition
  1836.          - = subtraction
  1837.          The following shows the values if the defects doubled.
  1838.          
  1839.          select code, defects*2
  1840.          from manu;
  1841.          
  1842.          code      defects 
  1843.          ---- ------------ 
  1844.          GC             24 
  1845.          GC              0 
  1846.          NM             34 
  1847.          DD                
  1848.          DD             44 
  1849.          NM             30 
  1850.          DD             24 
  1851.          DD             30 
  1852.          GC              8 
  1853.          
  1854.          9 rows selected
  1855.  
  1856.  
  1857.                                 SELECT-18
  1858.  
  1859.  
  1860.  
  1861.  
  1862.          
  1863.          In a calculation, multiplication and division are processed
  1864.          first, then the addition and subtraction.  The order of
  1865.          processing can be changed by adding parentheses.  Calculations
  1866.          within parentheses are calculated first.  More than one column
  1867.          can be used in a calulation and even the same column can be
  1868.          referenced more than once.  The column name for the calculation
  1869.          is taken from the first column name however you can rename the
  1870.          column (as shown in the full documentation).  The data type for
  1871.          the calculation is based on the first column in the calculation.
  1872.          Calculations can occur also in the where clause.
  1873.          
  1874.          GROUP BY, HAVING
  1875.          
  1876.          The group by clause is normally used with aggregate functions. 
  1877.          It has two operations:
  1878.          
  1879.          1.  It sorts by the column name.
  1880.          
  1881.          2.  The aggregate functions only operate based on the rows that
  1882.          have the same column name.  The functions in essence create
  1883.          sub-totals based on the column name.
  1884.          
  1885.          What if we wanted to know the average defects in each state:
  1886.          
  1887.          select mst, avg(defects) 
  1888.          from manu 
  1889.          group by mst; 
  1890.           
  1891.          mst AVG(defects)  
  1892.          --- ------------  
  1893.          AZ             8  
  1894.          CA            16  
  1895.          ID             6  
  1896.          WA            18  
  1897.           
  1898.          4 rows selected 
  1899.          
  1900.          
  1901.          The column name after the group by must exist on the line with
  1902.          the select.  After the select you can only have column names in
  1903.          the group by and aggregate functions - nothing else.
  1904.          You can restrict the group by clause with the having component.
  1905.          In SSQL you are allowed one simple selection that includes the =.
  1906.          <, <=, >, >=, and !=.  As with the group by, the column name
  1907.          in the having component must exist in the select clause.
  1908.          If you only wanted a list of states with defects over 10% you
  1909.          would use the following:
  1910.          
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.                                 SELECT-19
  1917.  
  1918.  
  1919.  
  1920.          
  1921.          select mst, avg(defects) 
  1922.          from manu 
  1923.          group by mst having avg(defects) > 10; 
  1924.           
  1925.          mst AVG(defects)  
  1926.          --- ------------  
  1927.          CA            16  
  1928.          WA            18  
  1929.           
  1930.          2 rows selected 
  1931.          
  1932.          The group by clause can have up to two fields.  This is when you
  1933.          want a group within a group.  
  1934.          The group by clause can be used with the where clause.  When we
  1935.          found the average defects in each state we could have modified it
  1936.          by excluding product DD. 
  1937.          The where clause and the having clause are related.  However, the
  1938.          where clause operates on the whole table without regard for the
  1939.          grouping.  The having clause operates on the data that has
  1940.          already been grouped.  Because of the way the having clause
  1941.          works, most of the time the having clause contains an operation
  1942.          with an aggregate function because that is the basic purpose of
  1943.          the group by clause.
  1944.          
  1945.          ORDER BY
  1946.          
  1947.          The order by clause sorts the output of the table based on the
  1948.          column name(s) listed.  The original table is not changed. As
  1949.          with the group by clause, the order by clause can be used with
  1950.          the where clause.  For example, the following query will produce
  1951.          a list of manufacturing information sorted by defects for those
  1952.          items manufactured in Idaho.
  1953.          
  1954.          select date, code, defects 
  1955.          from manu 
  1956.          where mst = 'ID' 
  1957.          order by defects; 
  1958.           
  1959.          date     code defects  
  1960.          -------- ---- -------  
  1961.          02/02/87 DD            
  1962.          02/01/87 GC         0  
  1963.          02/07/87 GC        12  
  1964.           
  1965.          3 rows selected 
  1966.          
  1967.          You also can produce a sort within a sort.  The following query
  1968.          will produce an ordered list of dates and within each date the
  1969.          state will be sorted.
  1970.  
  1971.  
  1972.  
  1973.  
  1974.          
  1975.                                 SELECT-20
  1976.  
  1977.  
  1978.  
  1979.  
  1980.          
  1981.          select date, mst, code, qty 
  1982.          from manu 
  1983.          order by date, mst; 
  1984.           
  1985.          date     mst code qty  
  1986.          -------- --- ---- ---  
  1987.          02/01/87 ID  GC    55  
  1988.          02/02/87 CA  NM    93  
  1989.          02/02/87 ID  DD    25  
  1990.          02/02/87 WA  NM    25  
  1991.          02/03/87 WA  DD    46  
  1992.          02/04/87 AZ  DD    25  
  1993.          02/04/87 CA  DD    25  
  1994.          02/06/87 AZ  GC    43  
  1995.          02/07/87 ID  GC    15  
  1996.           
  1997.          9 rows selected 
  1998.          
  1999.          The order by clause assumes ascending order.  You can reverse the
  2000.          order by finding out in the full documentation
  2001.          
  2002.          When there are two columns to sort, one can be in ascending order
  2003.          while the other is in descending order. 
  2004.          In the current version of SSQL, you are limited to two sort
  2005.          columns.
  2006.          
  2007.          REDIRECTTO file_name
  2008.          
  2009.          Normally the report that is produced by the select command is
  2010.          displayed on the monitor.  However, with the redirectto clause,
  2011.          the report can be redirected to a text file or to the printer. 
  2012.          The text file is in standard ASCII text.  That is, there are no
  2013.          special characters in it.  The file name you give it must not
  2014.          have an extension because SSQL adds the extension ".txt".  
  2015.          If you wanted all the Arizona products stored in a file called
  2016.          ARIZ.TXT and you wanted the report sorted by product code, you
  2017.          would use the following query.
  2018.          
  2019.          select * 
  2020.          from  manu 
  2021.          where mst='AZ' 
  2022.          redirectto ariz 
  2023.          order by code; 
  2024.          
  2025.          The output would be stored in a file called ARIZ.TXT on your
  2026.          default drive.  To display the file on the monitor, you would
  2027.          exit SSQL and the following at the DOS prompt:
  2028.          
  2029.          type ariz.txt
  2030.  
  2031.  
  2032.  
  2033.          
  2034.                                 SELECT-21
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.          
  2041.          You also can have the report sent to the printer.  Replace the
  2042.          file name with prn or lpt1.  Make sure the printer is on when you
  2043.          use this option.  
  2044.          
  2045.          UNION
  2046.          The union command allows you to combine output from two or 
  2047.          three different select statements.  The two select statements 
  2048.          must have exactly the same output format - the same number of 
  2049.          columns and each column in the corresponding tables must be of 
  2050.          the same data type.  For the following example, I assume that 
  2051.          there are two tables with the same structure as SALES called 
  2052.          SALESCA and SALESAZ however it is not necessary that the tables 
  2053.          have the same structure, just the output columns.  The 
  2054.          following statement will get the customer code and quantity 
  2055.          from SALESCA for those customers who have purchased over 50 of 
  2056.          a particular item.  The output will also contain all the 
  2057.          customers and corresponding quanitities from the SALESAZ table. 
  2058.          The output is sorted by customer code.
  2059.  
  2060.          select cc,qty
  2061.          from salesca
  2062.          where qty > 50
  2063.          union
  2064.          select cc,qty
  2065.          from salesaz
  2066.          order by cc;
  2067.          
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.                                   SELECT-22
  2094.  
  2095.  
  2096.  
  2097.          JOINING TABLES
  2098.          
  2099.          
  2100.          Often, the data we need exists in more than one table. In order
  2101.          to extract the data, we need to select the appropriate columns
  2102.          and join the tables. The type of join primarily discussed is
  2103.          called a natural join but for the sake of brevity I will just use
  2104.          the word "join" alone. 
  2105.          Whenever we join tables, there must normally be a common column.
  2106.          Hear the common column contains the customer code. It is not
  2107.          important that the column names are different. The important
  2108.          aspect is that they describe precisely the same thing - i.e.,
  2109.          they have a common domain.
  2110.          In SQL, the join operation normally has three basic components:
  2111.          
  2112.          SELECT column names - for duplicate names, precede the column
  2113.          name with the table name and a period as in prod.code
  2114.          
  2115.          FROM table names separated by commas
  2116.          
  2117.          WHERE common columns are set to be equal
  2118.          
  2119.          The following shows how to combine the sales information from 
  2120.          the sales table and the customer name from cust:
  2121.          
  2122.          select name, pc, qty 
  2123.          from sales, cust
  2124.          where cc=code;
  2125.  
  2126.          name            pc qty 
  2127.          --------------- -- --- 
  2128.          Organomice      MW  23 
  2129.          Organomice      DD  34 
  2130.          QuarkCo         AB   2 
  2131.          Marswarp        MW  81 
  2132.          Technoharps     NM   3 
  2133.          Technoharps     MW  41 
  2134.          Technoharps     GC  33 
  2135.          Compugorp       MW 125 
  2136.          Compugorp       MW 947 
  2137.          Compugorp       DD 452 
  2138.          Compugorp       NM  32 
  2139.          
  2140.          name            pc qty 
  2141.          --------------- -- --- 
  2142.          QuarkCo         GC 845 
  2143.          Organomice      NM  45 
  2144.          Organomice      MW  73 
  2145.          Compugorp       GC 127 
  2146.          Compugorp       DD  32 
  2147.          
  2148.          16 rows selected
  2149.  
  2150.          MUCH MORE IN THE FULL DOCUMENTATION
  2151.  
  2152.                                 JOIN-1
  2153.  
  2154.  
  2155.  
  2156.  
  2157.          SIMPLE SUBQUERIES
  2158.          
  2159.          OVERVIEW
  2160.          
  2161.          In previous chapters, any comparison operation was done with
  2162.          actual values or a column name. Whenever we used the =, !=, <,
  2163.          > , <=, >=, in, all, or any there was a column name on one
  2164.          side and either a column name, an actual value or for the in,
  2165.          all, or any, a set of actual values on the other side. The
  2166.          problem is that often the values on right side of the comparison
  2167.          operation are not known until the query is made. In such cases
  2168.          the right side of the comparison is stated as a separate select
  2169.          command that is called a subquery. The result of the subquery is
  2170.          obtained first and the data is passed back to the previous query.
  2171.          Many queries can be linked together in this way.
  2172.          Another way of looking at it is that often, we need to break down
  2173.          our problem into more than one query. Each query will have its
  2174.          own select. We need subqueries when, in analyzing a problem, we
  2175.          find that we need information from one query before we can
  2176.          process another query. Sometimes we can either join tables or use
  2177.          a subquery. Typically, subqueries retrieve information more
  2178.          rapidly than joining tables.
  2179.          
  2180.          SUBQUERIES WITH =, !=, <, >, <=, AND >=
  2181.          
  2182.          These operators can be used with any subquery that produces a
  2183.          single value. It is commonly used with subqueries that contain  
  2184.          an aggregate function. The following shows the customer codes and
  2185.          quantity for those customers who have purchased more of product
  2186.          MW in a single purchase than the total purchased by customer BB.
  2187.          
  2188.          select  cc, qty
  2189.          from  sales
  2190.          where  pc = 'MW'
  2191.          and  qty >
  2192.            (select  sum(qty)
  2193.            from  sales
  2194.            where  cc = 'BB'
  2195.            and  pc = 'MW');
  2196.          
  2197.          cc qty
  2198.          -- ---
  2199.          EE  81
  2200.          AA 125
  2201.          AA 947
  2202.          ZZ  73
  2203.  
  2204.          4 rows selected
  2205.          
  2206.          This method also can be used with any column where we know it can
  2207.          only return a single value.
  2208.  
  2209.  
  2210.  
  2211.                                      SUB-1
  2212.  
  2213.  
  2214.  
  2215.  
  2216.  
  2217.          SET EXPRESSIONS - IN
  2218.          
  2219.          In the section on simple select statements, we used any and in
  2220.          with constants (actual values). The true power of the set
  2221.          expressions involves its use in subqueries. 
  2222.          
  2223.          We will first compare a join with a subquery. If we wanted to get
  2224.          product codes for all products purchased by customers in
  2225.          California, we could join the SALES table and the customer table.
  2226.          
  2227.          select  distinct pc
  2228.          from  sales, cust
  2229.          where  cc = code
  2230.          and  st = 'CA';
  2231.          
  2232.          pc 
  2233.          -- 
  2234.          MW 
  2235.          
  2236.          1 row selected
  2237.          
  2238.          
  2239.          Another approach would be to break it down into two queries. We
  2240.          know that we need a list of product codes from the SALES table.
  2241.          The SALES table has customer codes but it doesn't have
  2242.          information on states. Another way to state the problem is that
  2243.          we want all product codes from the SALES table where the customer
  2244.          code is in the set of customers that are in California. find the
  2245.          customers in California.
  2246.          
  2247.          
  2248.          select  distinct pc
  2249.          from  sales
  2250.          where  cc in
  2251.            (select  code
  2252.            from  cust
  2253.            where  st='CA');
  2254.          
  2255.          pc 
  2256.          -- 
  2257.          MW 
  2258.          
  2259.          1 row selected
  2260.          
  2261.          THERE IS MUCH MORE IN THE FULL DOCUMENTATION
  2262.         
  2263.         
  2264.         
  2265.         
  2266.         
  2267.         
  2268.         
  2269.          
  2270.                                      SUB-3
  2271.  
  2272.  
  2273.          CORRELATED SUBQUERIES 
  2274.           
  2275.          OVERVIEW 
  2276.           
  2277.          A correlated subquery is a subquery that refers to a table 
  2278.          OUTSIDE the subquery. This causes correlated subqueries to be 
  2279.          processed differently from simple subqueries. Simple subqueries 
  2280.          are processed once, and the result is passed back to the main 
  2281.          query. The correlated subquery is processed for every row in the
  2282.          
  2283.          main query. This allows us to process a subquery based on a 
  2284.          particular column value in each row of the main query. It can get
  2285.          even more complex with multiple levels of subqueries. With simple
  2286.          subqueries, each subquery is isolated and passes back a single 
  2287.          set of values to the previous level. With correlated subqueries,
  2288.          each subquery can depend on more than one of the previous levels.
  2289.          
  2290.          If we are working on the SALES table we can have a subquery that
  2291.          is processed for each customer or product, not the whole table. 
  2292.          Another way of looking at it is that for each row in the main 
  2293.          query we can take a value, say customer code, and process a 
  2294.          subquery based on the customer. The value it produces for the 
  2295.          customer can be compared to another value in the same row in the
  2296.          main query. We will do that in the next section when we want to 
  2297.          display the complete row in a SALES table for every customer 
  2298.          purchase where the quantity purchased is above average FOR THAT 
  2299.          CUSTOMER. With a simple subquery we could only compare customer 
  2300.          purchases with average SALES for the whole table or a SINGLE 
  2301.          customer. Using the correlated subquery is sometimes like being 
  2302.          able to compare values to a subquery with a GROUP BY.  
  2303.           
  2304.          Often, we just want to test for the existence of a row in a 
  2305.          subquery based on a value in the first level of the query so SQL
  2306.          has the exists predicate. By using the correlated subquery we can
  2307.          create some rather complex tests on sets of data.  
  2308.           
  2309.          AGGREGATE FUNCTIONS 
  2310.           
  2311.          Let's contrast the correlated subquery with a simple subquery 
  2312.          that is produced below: 
  2313.           
  2314.          select * 
  2315.          from sales  
  2316.          where qty > 
  2317.           (select avg(qty) 
  2318.           from sales); 
  2319.           
  2320.          date     bc cc sn pc qty  
  2321.          -------- -- -- -- -- ---  
  2322.          04/01/87 1A AA 10 MW 947  
  2323.          04/08/87 1A AA 10 DD 452  
  2324.          04/08/87 1B DD 11 GC 845  
  2325.           
  2326.          3 rows selected 
  2327.  
  2328.  
  2329.                                      COR-1
  2330.  
  2331.  
  2332.  
  2333.  
  2334.           
  2335.          It solves the problem for displaying the complete row in the 
  2336.          SALES table for every customer purchase where the quantity 
  2337.          purchased is above average (based on the complete SALES table). 
  2338.           
  2339.          The way a simple subquery works is that it is processed ONCE, and
  2340.          
  2341.          the result is passed back to the main query. 
  2342.          Now let's change the query a little bit. Now we want to display 
  2343.          the complete row in a SALES table for every customer purchase 
  2344.          where the quantity purchased is above average FOR THAT CUSTOMER.
  2345.          
  2346.          Without knowing much else, it should at least be obvious that we
  2347.          need to process the subquery more than once since the query 
  2348.          states that we need an average for EACH customer to compare the 
  2349.          quantity to. Actually, the correlated subquery is more 
  2350.          inefficient than this. Usually, for EVERY row in the first table,
  2351.          
  2352.          the subquery is processed: 
  2353.           
  2354.          select * 
  2355.          from sales sales1 
  2356.          where qty > 
  2357.           (select avg(qty) 
  2358.           from sales 
  2359.           where cc = sales1.cc); 
  2360.           
  2361.          date     bc cc sn pc qty  
  2362.          -------- -- -- -- -- ---  
  2363.          04/12/87 1B BB 27 MW  41  
  2364.          04/15/87 2A BB 33 GC  33  
  2365.          04/01/87 1A AA 10 MW 947  
  2366.          04/08/87 1A AA 10 DD 452  
  2367.          04/08/87 1B DD 11 GC 845  
  2368.          04/01/87 2A ZZ 12 NM  45  
  2369.          04/08/87 2A ZZ 12 MW  73  
  2370.           
  2371.          7 rows selected 
  2372.           
  2373.           
  2374.          On the last line of the subquery we set cc = sales1.cc. This 
  2375.          means that for every row in the first table, it will take the 
  2376.          customer code (sales1.cc), and compute the average quantity for 
  2377.          that customer. It will then determine whether the quantity is 
  2378.          greater than the average quantity. We had to use the alias sales1
  2379.          to distinguish one SALES table from the other. Following any 
  2380.          table name we can rename it to avoid confusion or ambiguity. The
  2381.          alias can be any name that would be a valid table name. 
  2382.           
  2383.  
  2384.  
  2385.  
  2386.  
  2387.  
  2388.                                      COR-2
  2389.  
  2390.  
  2391.  
  2392.  
  2393.  
  2394.          EXISTS/NOT EXISTS  
  2395.           
  2396.          The exists predicate tests for the existence of a row selected in
  2397.          a subquery BASED ON DATA IN THE CURRENT QUERY (and possibly outer
  2398.          queries). We do not care what data selected happens to be in the
  2399.          subquery, we just want to know that it exists. The following 
  2400.          shows how to find the names of products in the SALES table. 
  2401.           
  2402.          select descrip 
  2403.          from prod 
  2404.          where exists 
  2405.           (select * 
  2406.           from sales 
  2407.           where code = pc); 
  2408.           
  2409.          descrip          
  2410.          ---------------  
  2411.          Megawamp         
  2412.          Gigasnarf        
  2413.          Nanomouse        
  2414.          Dynamic Disk     
  2415.           
  2416.          4 rows selected 
  2417.           
  2418.          THE TRUE VALUE OF THE CORRELATED SUBQUERY CAN BE SEEN IN THE FULL
  2419.          DOCUMENTATION - ABOUT 40 PAGES!!! 
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.                                      COR-3
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.          CONVERTING ENGLISH TO SQL
  2455.          
  2456.          OVERVIEW
  2457.          The previous chapters emphasized the point of view of SQL and
  2458.          understanding all the parts.  This chapter is more concerned with
  2459.          the point of view of a person wanting to solve queries using SQL. 
  2460.          There are three pieces of important information - understanding
  2461.          the database, understanding the query, and finally writing the
  2462.          SQL statement.
  2463.          
  2464.          ***************************************************************
  2465.          **  THE FULL DOCUMENTATION HAS THE SQL STATEMENTS AND        **
  2466.          **  EXPLANATIONS.  THE FOLLOWING WILL GIVE YOU A GOOD        **
  2467.          **  IDEA OF THE FLEXIBILITY OF SQL AND THE VARIETY OF        **
  2468.          **  PROBLEMS THAT IT CAN SOLVE  (IT SHOULD ALSO CONVINCE     **
  2469.          **  YOU TO REGISTER SO YOU CAN GET THE FULL DOCUMENTATION!)  **
  2470.          ***************************************************************
  2471.          
  2472.          UNDERSTANDING THE DATABASE
  2473.          In the chapter on the example databases, when a column was
  2474.          described, any other common columns in other tables was mentioned
  2475.          also.  This commonality allows us to work with information that
  2476.          spans tables.  There are other ways to group this information
  2477.          that may be useful.  The first way is to group them by the common
  2478.          domains.  The Monolith Manufacturing database has four basic
  2479.          domains (I do not count date): state, product code, employee
  2480.          number, and branch code. We can group them as follows:
  2481.          
  2482.          
  2483.          
  2484.          NAME TABLES COLUMN NAME
  2485.          STATE CUST st
  2486.           MANU mst
  2487.           BRANCH st
  2488.          
  2489.          PRODUCT MANU code
  2490.          CODE PROD code
  2491.           SALES pc
  2492.          
  2493.          EMPLOYEE SALES sn
  2494.          NUMBER EMP enum
  2495.           EMP mgrnum
  2496.           BRANCH mgrnum
  2497.          
  2498.          BRANCH BRANCH code
  2499.          CODE SALES bc
  2500.          
  2501.          Whenever we join tables or use a subquery, we must use common
  2502.          domains so the above information is essential.
  2503.          
  2504.          
  2505.          
  2506.                                     ENG-1
  2507.  
  2508.  
  2509.  
  2510.          GENERAL APPROACH TO SOLVING THE QUERY
  2511.          (This section is only in the full documentation)
  2512.  
  2513.          The Numbering Scheme For Queries
  2514.          Each key word or words is given a number from 1 to 4.  The number
  2515.          is further divided into numbers to the right of a decimal as to
  2516.          how the keywords are used.  Each example is  followed by a
  2517.          letter.  Query 3.2B corresponds to the second example(B) for the
  2518.          second use(2) of keyword 3.  Queries preceded by an 'N' means
  2519.          that it is a negated version of the query.  There may be more
  2520.          than one way to negate it so the version is in the right-most
  2521.          position as in: N3.2B.2.
  2522.          
  2523.          1 - AND
  2524.          The and is used to refer to information in columns.  In its
  2525.          simplest form, it is used to select which columns to display.  It
  2526.          is commonly used to determine which rows to display.  As you will
  2527.          see from the examples, there are many ways to translate and into
  2528.          SQL.
  2529.          1.1 Used to describe which columns to display.
  2530.          1.1A List the date of manufacture, product code, and quantity.
  2531.          
  2532.          1.2 Used when you need or:
  2533.          1.2A List the rating for Organomice and Compugorp.
  2534.          1.2B List the dates of purchase for products MW and NM.
  2535.          
  2536.          1.3 Ambiguous 
  2537.          Solution could be an or a more complex construction.  Usually the
  2538.          ambiguity arises because we are referring to sets of values
  2539.          instead of single values.  The answer could be a union of the two
  2540.          sets (or) or an intersection (subquery). Query 1.2A asks for
  2541.          rating which will be a single value for each customer.  In query
  2542.          1.3A, we are concerned with two sets of values - products
  2543.          manufactured in Washington and products manufactured in Idaho. 
  2544.          Just because it refers to sets of values does not necessarily
  2545.          mean that it is ambiguous.  Query 1.2B refers to sets but (in my
  2546.          opinion) it is not ambiguous.  Contrast 1.2B with 1.3B which I
  2547.          think is ambiguous.
  2548.          1.3A  List the products manufactured in Washington and Idaho.
  2549.          1.3B  List the dates when products MW and NM were purchased.
  2550.          
  2551.          1.4. Not Ambiguous
  2552.          However the and translates into a more complex SQL statement. 
  2553.          What we are after here is often an intersection of the sets as
  2554.          you can see in 1.4A and 1.4B.  That is, we want values common to
  2555.          all sets. An intersection can normally be achieved through a
  2556.          series of subqueries.
  2557.          1.4A  List the products manufactured in both Washington and
  2558.          Idaho.
  2559.          1.4B  List the dates when all of the following products were
  2560.          purchased: MW, NM, and GC.
  2561.          1.4C  List the customers who are from Arizona and have purchased
  2562.          product MW.  
  2563.  
  2564.  
  2565.                                     ENG-2
  2566.  
  2567.  
  2568.  
  2569.  
  2570.  
  2571.          1.5 Definitely need an and in the where clause.  
  2572.          The and is used when we want to test two or more different
  2573.          columns in a row within a single table.
  2574.          1.5A  List the customers who are from Arizona and have a rating
  2575.          over 15.
  2576.          
  2577.          1.6 Implied and
  2578.          1.6A  List the Arizona customers who have a rating over 15.
  2579.          Same as 1.5A
  2580.          1.6B  List the Arizona customers who have purchased product MW.
  2581.          Same as 1.4C
  2582.          
  2583.          OR
  2584.          2.1  Normally translates to or:
  2585.          2.1A  List the customer codes for customers who have purchased MW
  2586.          or NM.
  2587.          2.1B  List the customers names for customers who have purchased
  2588.          MW or NM.
  2589.          2.1C  List the customer names for customers who are from
  2590.          California or have a rating less than 10.
  2591.          
  2592.          2.2 Comparison operators <= and >=.
  2593.          2.2A  List customers who have a rating greater than or equal to
  2594.          15.
  2595.          
  2596.          3 - WHICH, WHO, WHOSE, THAT, WITH, WHERE
  2597.          The above words have various meanings but I will emphasize their
  2598.          use in clauses.  Clauses connect parts of sentences.  Sometimes
  2599.          is makes a query clearer when it is converted to a form that has
  2600.          a clause.  The first six queries ask the same thing in different
  2601.          ways.  
  2602.          3.1A  Which customers have a rating over 10?
  2603.          3.1B  Who has a rating over 10?
  2604.          3.1C  List customers who have ratings over 10.
  2605.          3.1D  List customers with ratings over 10.
  2606.          3.1E  List every customer whose rating is over 10.
  2607.          3.1F  List every customer where the rating is over 10.
  2608.          Queries 3.1C through 3.1D are the most straightforward because
  2609.          the first part describes what is to be displayed and the second
  2610.          part describes the logic to determine which rows we want.  Many
  2611.          of the following can be rewritten in a similar manner but I will
  2612.          usually present only one approach.
  2613.          
  2614.          3.2  Link tables  
  2615.          3.2A  List the customer's states for customers who have purchased
  2616.          products  that are manufactured in Idaho.
  2617.          3.2B  Get branch codes for branches that sell products that are
  2618.          sold by branches that sell product DD. This is a rather
  2619.          convoluted one but shows the power of recognizing clauses. 
  2620.          Logically we want to find all the branches that sell product DD.
  2621.  
  2622.  
  2623.  
  2624.                                     ENG-3
  2625.  
  2626.  
  2627.  
  2628.  
  2629.          Then we want to take those branches and find all the products
  2630.          they sell.  With that set of products, we want to find all the
  2631.          branches that sell those products.  Notice how in 3.2B each
  2632.          subquery corresponds to each clause.  Be careful doing the
  2633.          conversion.  Sometimes the "that" does not have to be included. 
  2634.          Get branch codes for branches that sell products sold by branches
  2635.          that sell product DD.  In the previous sentence the "that" is
  2636.          missing between "products" and "sold".
  2637.          3.2C  List the names of employees who have sold products to
  2638.          Compugorp.
  2639.          3.2D. Get customer names and product names for customers who have
  2640.          purchased items that are manufactured outside their own state.
  2641.          3.2E  Get branch code, customer names and product names for those
  2642.          who purchase products that are manufactured in their own state
  2643.          from a branch in their own state.
  2644.          3.2F  Get branch numbers for branches that sell to both AA and
  2645.          BB.
  2646.          3.2G Who are the managers?
  2647.          3.2H  What are the names of managers who actually sold something?
  2648.          3.2I  What are the names of managers whose salespeople have sold
  2649.          products to Organomice?
  2650.          3.2J  What are the names of managers whose salespeople have sold
  2651.          products ONLY to Organomice? We have to add something to exclude
  2652.          salespeople who sold to a customer other than Organomice.
  2653.          3.2K List the names of Xero Xanadu's subordinates who sold
  2654.          Technowidgits and what were the quantities sold?
  2655.          3.2L  Get branch codes for branches that sell to a Washington or
  2656.          Arizona customer a product manufactured in Oregon.
  2657.          3.2M Get all pairs of state values where the branch in the first
  2658.          state sells to a customer in a second state.
  2659.          
  2660.          3.4  Implied that or who  
  2661.          The words in parentheses in the following three queries are
  2662.          optional.
  2663.          3.4A  Get product codes for products (that were) sold to any
  2664.          customer in California.
  2665.          3.4B  Get product numbers and corresponding customer names for
  2666.          products (that are) manufactured in the same state as a customer.
  2667.          3.4C  Get customer numbers for customers (who were) sold at least
  2668.          one product from a branch in the same state.
  2669.          
  2670.          4 - ANY/ALL
  2671.          Translating any and all cause the same sort of problems that were
  2672.          caused by and.  This is because the way we use a word in English
  2673.          may be different from the way we translate it to SQL.
  2674.          First of all, I will skip over any queries in which the any or
  2675.          all serve no purpose.  Often they are added for emphasis and CAN
  2676.          mean the same thing as in queries 4.1A and 4.1B.  Normally, the
  2677.          word any serves no purpose unless it is used in a comparison.
  2678.  
  2679.  
  2680.  
  2681.  
  2682.  
  2683.                                     ENG-4
  2684.  
  2685.  
  2686.  
  2687.          4.1  All and any not needed.  
  2688.          4.1A  Get all product codes for all products purchased by
  2689.          customers in California. 
  2690.          Same as 3.4A
  2691.          4.1B  Get any product code for any products purchased by any
  2692.          customer in California. 
  2693.          Same as 3.4A
  2694.          4.1C  Get product codes for products purchased by customers in
  2695.          California. 
  2696.          Same as 3.4A
  2697.          
  2698.          4.2  All used as part of a FORALL quantifier
  2699.          This was discussed in detail in the chapter on correlated
  2700.          subqueries. It is used when we want to compare one set of values
  2701.          to another set of values instead of comparing a single value to a
  2702.          set of values or comparing a single value to a single value.
  2703.          4.2A  Get product codes for products purchased by all customers.
  2704.          4.2B  Get product codes for products purchased by all customers
  2705.          in California.
  2706.          4.2C  Get product codes for products where the minimum defects is
  2707.          greater than 10 and has been purchased by all customers.
  2708.          4.2C  Get customer names for customers who have purchased all
  2709.          products.
  2710.          4.2D  States that manufacture all products sold by a branch in
  2711.          the same state.
  2712.          4.2E  Branches that have sold all the products manufactured in
  2713.          their own state.
  2714.          4.2F Branches that have sold all the products to all the
  2715.          customers.
  2716.          
  2717.          4.3 Using any and all in comparisons
  2718.          These are very deceiving.  The use of any normally translates to
  2719.          all in SQL or an equivalent construction.  This idea is discussed
  2720.          in the chapter on subqueries.
  2721.          4.3A  List products that have defects greater than any defect for
  2722.          products manufactured in Idaho.  Compare this to 4.3B.
  2723.          4.3B  List products that have defects greater than all defects
  2724.          for products manufactured in Idaho.
  2725.          Query 4.3A is normally interpreted as the same as 4.3B although
  2726.          logically it means something different.  If you think that they
  2727.          are different in common English then I guess SQL has distorted
  2728.          your thinking a bit.  When a tennis player says that he is better
  2729.          than ANY tennis player, we assume that he means that he is better
  2730.          than ALL tennis players.  The confusion is caused by the fact
  2731.          that in most contexts any and all are very different as in the
  2732.          following two sentences.  Give me any book.  Give me all the
  2733.          books.
  2734.          4.3C  List products that have defects less than any (all) defects
  2735.          for products manufactured in Idaho.
  2736.          4.3D  List products that have defects the same as any defects for
  2737.          products manufactured in Idaho.  In this case we cannot replace
  2738.          the "any" with an "all" and retain the same meaning.
  2739.          
  2740.  
  2741.  
  2742.                                     ENG-5
  2743.  
  2744.  
  2745.  
  2746.  
  2747.  
  2748.          N - NEGATION
  2749.          In this section I will take many of the above queries and negate
  2750.          them.
  2751.          N1.2a List the rating for all except Organomice and Compugorp.
  2752.          N1.3A.1  List the products manufactured in neither Washington nor
  2753.          Idaho.
  2754.          N1.3A.2 List the products not manufactured in both Washington and
  2755.          Idaho.
  2756.          N1.3B.1  List the dates that products MW or NM were not
  2757.          purchased.
  2758.          N1.3B.2  List the dates that products other than MW or NM were
  2759.          purchased.
  2760.          N1.4B.1  List the dates when all of the following products were
  2761.          not purchased: MW, NM, and GC.
  2762.          N1.4B.2  List the dates when none of the following products were
  2763.          purchased: MW, NM, and GC.
  2764.          N1.4B.3  List the dates when something other than the following
  2765.          products were purchased: MW, NM, and GC.
  2766.          N1.4C.1  List the customers who are from Arizona and have not
  2767.          purchased product MW.  
  2768.          N2.1B.1  List the customers names for customers who have not
  2769.          purchased MW or NM.
  2770.          N2.1B.2  List the customers names for customers have purchased
  2771.          products other than MW and NM.
  2772.          The difference between N2.1B.1 and N2.1B.2 is that the first one
  2773.          would exclude any customer who has purchased MW or NM.  The
  2774.          second one would include the customer as long as the customer
  2775.          purchased another product.
  2776.          N3.2A.1  List the customer's states for those customers who have
  2777.          not purchased products manufactured in Idaho.
  2778.          N3.2A.2  List the customer's states for those customers who have 
  2779.          purchased products not manufactured in Idaho.
  2780.          N3.2A.2  List the customers' states for those customers who have 
  2781.          purchased products manufactured outside (not in) Idaho.
  2782.          The above differs from the previous query in that it will include
  2783.          any product manufactured in Idaho IF it is manufactured outside
  2784.          Idaho.
  2785.          N3.2B.1  Get branch codes for branches that do not sell products
  2786.          that are sold by branches that sell product DD.
  2787.          N3.2B.2  Get branch codes for branches that sell products that
  2788.          are not sold by branches that sell product DD.
  2789.          N3.2B.3  Get branch codes for branches that sell products that
  2790.          are sold by branches that do not sell product DD.
  2791.          The negation is on selling product DD.  This is different from
  2792.          saying: "...branches that sell products other than DD".  
  2793.          N3.2C.1  List the names of employees who have not sold products
  2794.          to Compugorp.
  2795.          N3.2D.1 Get customer names and product names for customers who
  2796.          have purchased items not manufactured outside their own state.
  2797.          N3.2D.2 Get customer names for customers who have not purchased
  2798.          items manufactured outside their own state.  
  2799.  
  2800.  
  2801.                                     ENG-6
  2802.  
  2803.  
  2804.  
  2805.  
  2806.  
  2807.          N3.2D.3 Get customer names for customers who have only purchased
  2808.          items in their own state. 
  2809.          At first glance N3.2D.3 seems the same as N3.2D.2 but N3.2D.2
  2810.          would display customers who have not purchased anything.  We have
  2811.          to add some more to make sure that the customer actually made a
  2812.          purchase.
  2813.          N3.2F.1  Get branch codes for branches that do not sell to both
  2814.          AA and BB.
  2815.          N3.2F.1  Get branch codes for branches that sell to neither AA
  2816.          nor BB.
  2817.          N3.2F.1  Get branch codes for branches that do not sell to AA or
  2818.          BB.
  2819.          N3.2G.1  What are the names of managers who did not sell
  2820.          anything?
  2821.          N3.2I.1  What are the names of managers whose salespeople have
  2822.          not sold products to Organomice?
  2823.          N3.2I.2  What are the names of managers whose salespeople have 
  2824.          sold products to everyone except Organomice?  This becomes a
  2825.          tough one because the opposite of "only Organomice" in 3.2I is
  2826.          "everyone except Organomice" and "everyone" corresponds to the
  2827.          FORALL quantifier covered in the chapter on correlated
  2828.          subqueries.  The core of this query has to do with "salespeople
  2829.          selling to ALL customers (except Organomice)".
  2830.          N3.2L.1  Get branch codes for branches that sell to a Washington
  2831.          or Arizona customer a product not manufactured in Oregon.
  2832.          N3.2L.2  Get branch codes for branches that sell to a Washington
  2833.          or Arizona customer a product manufactured outside of Oregon.
  2834.          N4.2A.1  Get product codes for products not purchased by all
  2835.          customers.
  2836.          N4.2A.2  Get product codes for products purchased by none of the
  2837.          customers.
  2838.          N4.2D.1  States that do not manufacture all products sold by a
  2839.          branch in the same state.
  2840.          N4.2D.2  States that manufacture no products sold by a branch in
  2841.          the same state.
  2842.          N4.2E.1  Branches that have not sold all the products
  2843.          manufactured in their own state.
  2844.          N4.2E.2  Branches that have sold none of the products
  2845.          manufactured in their own state.
  2846.          N4.2E.3  Branches that have sold all the products not
  2847.          manufactured in their own state.
  2848.          N4.2E.4  Branches that have sold all the products manufactured
  2849.          outside their own state.
  2850.  
  2851.          
  2852.          
  2853.          
  2854.          
  2855.          
  2856.          
  2857.          
  2858.          
  2859.          
  2860.                                     ENG-7
  2861.  
  2862.  
  2863.  
  2864.  
  2865.  
  2866.          CREATE  TABLE
  2867.          
  2868.          OVERVIEW
  2869.          The CREATE TABLE command is used to create a table file. You can
  2870.          control the contents of the columns in two ways.  You can prevent
  2871.          a column from being null and you can make sure that a column or
  2872.          set of columns never contain duplicate values.  These
  2873.          restrictions allow us to maintain valid primary keys when we use
  2874.          the INSERT command to add rows to a table or the UPDATE command
  2875.          to modify values in a table.
  2876.          
  2877.          SYNTAX
  2878.          
  2879.          CREATE TABLE table_name (
  2880.            column_definition [, column_definition...]
  2881.            [, uniqueness_constraint]
  2882.          );
  2883.  
  2884.          EXAMPLE:
  2885.          The statement below shows how to create the cust table that  the
  2886.          documentation uses. 
  2887.          
  2888.          create table cust ( 
  2889.          code char(2) not null unique,   
  2890.          name char(15) not null,
  2891.          st char(2) not null,
  2892.          rating numeric(2)
  2893.          );
  2894.          
  2895.          You can define up to 27 columns in a table.
  2896.          As with all SQL commands, they can be in uppercase or lowercase. 
  2897.          After the name of the table, parentheses enclose the column
  2898.          definitions. The column definitions are put on separate lines to
  2899.          aid in readability.  SQL never requires anything to be put on
  2900.          separate lines or have specific spacing. Note that all column
  2901.          definitions except the last one has a comma after it.
  2902.          The above command states that there will be four columns in the
  2903.          cust table:
  2904.          
  2905.          code can have up to two characters.  It is the primary key so
  2906.          nulls will not be allowed and each code will be unique.
  2907.          
  2908.          name can have up to fifteen characters. It must contain a value.
  2909.          
  2910.          st   is the state postal code and it can have up to 2 characters.
  2911.          It must contain a value.
  2912.          
  2913.          rating is the customer rating.  Since NOT NULL is not specified,
  2914.          the customer rating is optional - it does not have to be entered.
  2915.  
  2916.  
  2917.  
  2918.  
  2919.                                       CREATE-1
  2920.  
  2921.  
  2922.  
  2923.  
  2924.  
  2925.          COMPONENTS OF CREATE TABLE
  2926.          
  2927.          table_name
  2928.          A table name can be from 1 to 10 characters.  The first eight
  2929.          characters must be unique.  The first character must be a letter 
  2930.          of the alphabet.  The rest can be letters or digits, _
  2931.          (underscore), or #.  The table files are created on the disk as in
  2932.          table_name.DBF and table_name.SQD.  For example, the cust 
  2933.          table, created above would be stored as CUST.DBF and CUST.SQD 
  2934.          on your disk.  However, from SSQL, you would always refer to it 
  2935.          as CUST.  Since SSQL adds the extension of DBF and SQD to all 
  2936.          table files, you must not use a period in your table name.   
  2937.          
  2938.          column_definition
  2939.          
  2940.          column_name data_type [NOT NULL [UNIQUE]]
  2941.          
  2942.          column_name
  2943.          A column name can be from 1 to 10 characters.  The first
  2944.          character must be a letter of the alphabet.  The rest can be
  2945.          letters or digits.  In creating column names remember that when
  2946.          you display a table, the full column name is displayed too as the
  2947.          heading.  Long column names tend to fill the screen (or printer)
  2948.          very rapidly when you want to display many columns.  The column
  2949.          name is displayed exactly as you type it, retaining the uppercase
  2950.          and lowercase letters.  The create statement above created the
  2951.          column names and hence the headings in lowercase letters.  We
  2952.          could have written it as shown below.
  2953.          
  2954.          create table cust ( 
  2955.          CODE char(2) not null unique,   
  2956.          NAME char(15) not null,
  2957.          ST char(2) not null,
  2958.          RATING numeric(2)
  2959.          );
  2960.          
  2961.          With the statement above, whenever we display data from the
  2962.          table, the headings would in uppercase.
  2963.  
  2964.          data_type
  2965.          The data types in SQL fall into two broad categories - numbers
  2966.          and characters.  Within the numbers category, there are exact
  2967.          numeric types and approximate numeric types.  However, all 
  2968.          numeric data types are converted to the dBase numeric data 
  2969.          type.  The other types are included to maintain compatibility 
  2970.          with ANSI SQL.  Two non-standard data have been added to 
  2971.          maintain compatibility with dBase - Date and Logical.
  2972.  
  2973.  
  2974.  
  2975.  
  2976.          
  2977.  
  2978.                                 CREATE-2
  2979.  
  2980.  
  2981.          Numeric
  2982.          
  2983.           (Exact Numeric)
  2984.           NUMERIC[(length [,decimal_places])]
  2985.           DECIMAL[(length [,decimal_places])]
  2986.           DEC[(length [,decimal_places])]
  2987.           INTEGER
  2988.           INT
  2989.           SMALLINT
  2990.           (Approximate Numeric covered in the full documentation)
  2991.  
  2992.          When you use numeric data types the values are always
  2993.          right-justified (values are pushed to the right so all the
  2994.          decimal lines up).  Data must be of this type in order to use any
  2995.          of the numeric functions such as avg, max, min, and sum.  
  2996.          The first three data types in the exact numeric category -
  2997.          NUMERIC, DECIMAL, and DEC, can be used interchangeably.  It
  2998.          sounds a little odd that a data type called DECIMAL does not
  2999.          necessarily mean that the contents of the column will contain a
  3000.          decimal place since the modifier decimal_places is optional. 
  3001.          
  3002.          length (optional)
  3003.          The length of the column be up to 12 digits (including the
  3004.          decimal place. Without this specification, the length is 1.
  3005.          
  3006.          decimal_places (optional)
  3007.          The number of places to the right of the decimal.  For Example:  
  3008.          
  3009.          cost  numeric(5,2)
  3010.          
  3011.          This would allow a maximum of 99.99 to be stored in the column.
  3012.          
  3013.          This also could be defined as:
  3014.          
  3015.           cost decimal(5,2)
  3016.          or 
  3017.           cost dec(5,2)
  3018.          
  3019.          It is allowable to use whole numbers even though you define it as
  3020.          having decimals.  For example, you want to enter grades that are
  3021.          from  0 to 100 but when you calculate grade averages, you want it
  3022.          calculated to one tenth of a grade point.  You would define the
  3023.          column as:
  3024.          
  3025.           grade  numeric(5,1)
  3026.          
  3027.          You would enter the grades as whole numbers but when the average
  3028.          is calculated, the decimal would be included. 
  3029.          With NUMERIC, DECIMAL and DEC, if you omit the dec_places
  3030.          modifier, the result is a whole number.
  3031.          
  3032.          Data types such as INTEGER (which is the same as INT) and
  3033.          SMALLINT have lengths associated with them.  The ANSI standards
  3034.          state that the lengths shall be determined by the implementor so
  3035.          I have picked lengths of ten and five respectively.
  3036.  
  3037.                                CREATE-3
  3038.  
  3039.          
  3040.          INTEGER is the same as numeric(10) or decimal(10) or dec(10)
  3041.          SMALLINT is the same as numeric(5) or decimal(5) or dec(5)
  3042.          
  3043.          You can see that although there are six exact numeric data types,
  3044.          you only need one.
  3045.          
  3046.          Characters
  3047.          
  3048.           CHARACTER[(length)]
  3049.           CHAR[(length)]
  3050.  
  3051.          These data types can be used for any column that you do not use
  3052.          in a calculation.  Although the data is usually a combination of
  3053.          alphabetic and numeric data, it is alright if the column just
  3054.          contains digits.  The characters are left-justified.  The maximum
  3055.          column width is 80.  CHARACTER and CHAR can be used
  3056.          interchangeably.  If you omit the length modifier, then the
  3057.          length of the column is one.
  3058.          
  3059.          Date
  3060.            DATE
  3061.            The DATE data type creates a column of 8 characters.  Dates 
  3062.          are entered as mm/dd/yy.  
  3063.          
  3064.          Logical
  3065.            LOGICAL - A one character column that can store Y, y, N, n, 
  3066.          T, t, F, f, or ? 
  3067.  
  3068.          NOT NULL
  3069.          This modifier to a column definition will ensure that there is
  3070.          always a value for this column. It prevents the INSERT and UPDATE
  3071.          commands from allowing a column to contain a null value. When
  3072.          used with the UNIQUE modifier, it is used to specify a primary
  3073.          key.  There are special commands to retrieve rows that only have
  3074.          null values in a particular column or to exclude rows that have
  3075.          null values in a particular column.
  3076.          
  3077.          UNIQUE
  3078.          This ensures that each value in the column is unique.  If an
  3079.          attempt is made to enter a duplicate value when inserting a new
  3080.          row, an error results and the row is not added to the table.  If
  3081.          you use unique, it must be used with NOT NULL. This is typically
  3082.          used when a single column is a primary key. 
  3083.          
  3084.          Uniqueness_constraint (covered in full documentation)
  3085.          
  3086.          CHECKING THE STRUCTURE OF A TABLE
  3087.          In order to display the create command used to create the table,
  3088.          type STRUCT followed by the table name.
  3089.          
  3090.  
  3091.  
  3092.  
  3093.  
  3094.  
  3095.  
  3096.                                      CREATE-4
  3097.  
  3098.  
  3099.          CREATE A VIEW
  3100.  
  3101.          OVERVIEW
  3102.          A view is a derived table.  A view describes an alternative
  3103.          access to columns and tables that already exist.  The create
  3104.          table command allowed us to create a physical table that is also
  3105.          called a base table.  With a base table every column corresponds
  3106.          to the source of data.  We can always use the insert, update, and
  3107.          delete commands on base tables.  Views never exist as permanent
  3108.          tables.  Whenever you query a view, it creates a temporary table
  3109.          based on your view.  When the query is complete, the temporary
  3110.          table is erased.
  3111.          The view can reference an existing column under a different name
  3112.          or keep the original name. The view can be based on more than one
  3113.          table.  You can reference as few or as many columns you want in
  3114.          the tables you select.
  3115.          
  3116.          SYNTAX
  3117.          
  3118.          CREATE VIEW view_name [(column_name [,column_name...])] AS
  3119.          select_statement
  3120.          
  3121.          EXAMPLES:
  3122.          Create a view which only shows less than the full number of
  3123.          columns in a table.  In a commercial database system, this can be
  3124.          used to prevent users from seeing sensitive data.  For example,
  3125.          The employee table may include salary information.  A view of
  3126.          that table could have everything except the salary information.
  3127.          It is necessary to have some background in the use of the select
  3128.          command before attempting to create a view of a table.  The
  3129.          statement below shows the creation of a view called custa.   It
  3130.          is based on the cust table that has four columns:  code, name,
  3131.          st, and rating.
  3132.          
  3133.          create view custa as
  3134.          select code, name, st
  3135.          from cust;
  3136.          
  3137.          The column names are the same as they are in the original table. 
  3138.          The only difference is that rating is not part of the view.
  3139.          When you use view custa in a query, it acts just like a regular
  3140.          table.  When it displays all the columns as shown below, you only
  3141.          see the three columns specified in the view.  
  3142.          
  3143.          select * 
  3144.          from custa; 
  3145.          
  3146.          code name            st  
  3147.          ---- --------------- --  
  3148.          AA   Compugorp       WA  
  3149.          BB   Technoharps     OR  
  3150.          ZZ   Organomice      AZ  
  3151.          DD   QuarkCo         AZ  
  3152.          EE   Marswarp        CA  
  3153.          FF   Multicrud       NV  
  3154.          
  3155.                                      VIEW-1
  3156.  
  3157.  
  3158.  
  3159.  
  3160.  
  3161.          We have no access to the rating column when using the custa view.
  3162.          
  3163.          RULES FOR USING VIEWS
  3164.          
  3165.          It is used in a select statement, NOT an insert, update, or
  3166.          delete statement. To erase the view, use the drop view statement.
  3167.          
  3168.          Can contain any select statement except one that has redirectto. 
  3169.          
  3170.          Views should only be used when they are absolutely needed.  They
  3171.          can add much confusion to a database design because it is easy to
  3172.          forget which are tables and which are views.
  3173.          
  3174.          (MORE IN THE FULL DOCUMENTATION)
  3175.  
  3176.  
  3177.  
  3178.  
  3179.  
  3180.  
  3181.  
  3182.  
  3183.  
  3184.  
  3185.  
  3186.  
  3187.  
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.  
  3194.  
  3195.  
  3196.  
  3197.  
  3198.  
  3199.  
  3200.  
  3201.  
  3202.  
  3203.  
  3204.  
  3205.  
  3206.  
  3207.  
  3208.  
  3209.  
  3210.  
  3211.  
  3212.  
  3213.  
  3214.                                       VIEW-2
  3215.  
  3216.  
  3217.  
  3218.  
  3219.  
  3220.  
  3221.          DROP TABLE/VIEW
  3222.  
  3223.          OVERVIEW
  3224.          The drop command is used to erase a table or view from the disk.
  3225.          The drop table command is used to erase the description of the
  3226.          table from the disk.  The drop view command erases the view but
  3227.          can never alter data or tables since the view simply describes
  3228.          access to a table or tables.
  3229.          
  3230.          SYNTAX
  3231.          
  3232.          DROP TABLE table_name;
  3233.          or
  3234.          DROP VIEW view_name;
  3235.          
  3236.          EXAMPLES:
  3237.          
  3238.          drop table cust;
  3239.          drop view custa; 
  3240.          
  3241.          COMPONENTS OF THE DROP COMMAND
  3242.          
  3243.          table_name
  3244.          The table_name must be a valid table name which contains NO data. 
  3245.          In order to delete all the data you can first type:
  3246.          
  3247.          delete from table_name;
  3248.          
  3249.          The delete command erases the data and the drop table command 
  3250.          erases the column descriptions. 
  3251.          
  3252.          view_name
  3253.          The view_name must be a valid view name.  Since no data is
  3254.          affected it can be used at any time.
  3255.  
  3256.  
  3257.  
  3258.  
  3259.  
  3260.  
  3261.  
  3262.  
  3263.  
  3264.  
  3265.  
  3266.  
  3267.  
  3268.  
  3269.  
  3270.  
  3271.  
  3272.  
  3273.                                 DROP-1
  3274.  
  3275.  
  3276.           INSERT DATA INTO A TABLE
  3277.  
  3278.           OVERVIEW
  3279.           The ANSI standards define two ways to add data to a table. One
  3280.           requires us to type each value for each column in each row. The
  3281.           other copies data from one table and puts it in another. It is
  3282.           very useful for making corrections to tables. I only describe the
  3283.           first type in this documentation.  Registered users find out
  3284.           about the more advanced use of the insert command and a special,
  3285.           non-ANSI command which makes inserting data MUCH easier.
  3286.           
  3287.           Both insert commands maintain the restrictions we may have
  3288.           imposed through the CREATE TABLE command.  We could have
  3289.           specified NOT NULL and UNIQUE to prevent certain types of data
  3290.           errors in our tables.
  3291.           
  3292.           INSERT VALUES - SYNTAX
  3293.           
  3294.           INSERT INTO table_name [column_name [,column_name...]]
  3295.           VALUES (column_value [,column_value]);
  3296.           
  3297.           EXAMPLES:
  3298.           
  3299.           The first example shows the typical way to use the insert command
  3300.           since it does not reference any column names.  The insert command
  3301.           assumes that you will enter the data in the order that they were
  3302.           created in the table.  The second example shows that column names
  3303.           can be referenced. The third example shows that the data entry
  3304.           does not have to be in the order in which the columns were
  3305.           created.  Compare the order of the values to the first example.
  3306.           insert into cust values('AA','Compugorp','WA',20);
  3307.           or
  3308.           insert into cust code, name, st, rating
  3309.           values('AA','Compugorp','WA',20);
  3310.           or
  3311.           insert into cust name, code, rating, st 
  3312.           values('Compugorp','AA',20,'WA');
  3313.           
  3314.           MORE DETAIL ON THE COMPONENTS OF THE SYNTAX
  3315.           
  3316.           table_name
  3317.           
  3318.           Any existing table in your database.
  3319.           
  3320.           column_name
  3321.           
  3322.           Normally column names are not used with the insert command.  This
  3323.           is because the insert command assumes that we are going to insert
  3324.           values for all the columns in the table in the order in which
  3325.           they were created. You saw this in the first example.  It has the
  3326.           same effect as the second example.  In the third example we want
  3327.           to insert data in a different order.  This could be useful if the
  3328.           source document that we are getting the data from presents the
  3329.           data in a different order from which the table was designed.  
  3330.  
  3331.  
  3332.                                    INSERT-1
  3333.  
  3334.  
  3335.  
  3336.           column_value
  3337.           There are three types of column values: character, numeric and
  3338.           null.
  3339.           
  3340.           Character data
  3341.           Character data must be enclosed in quotes or apostrophes. The
  3342.           first example above could have been entered as:
  3343.           
  3344.           insert into cust values("AA","Compugorp","WA",20);
  3345.           
  3346.           Although you can enter SQL commands in uppercase or lowercase,
  3347.           care must be taken when entering character data since everything
  3348.           is stored exactly as entered.  This can create problems when we
  3349.           are retrieving data.  Let's assume that Compugorp purchased some
  3350.           Megawamps so we insert the following information into the sales
  3351.           table: date, branch code, customer code, employee number, product
  3352.           code and quantity.
  3353.           
  3354.           insert into sales values('04/01/87','1A','aa',10,'MW',947);
  3355.           
  3356.           Although SQL would accept the above insert command, we made a
  3357.           critical logical error.  The correct customer code for Compugorp
  3358.           is 'AA', NOT 'aa'.  This is an error that is rather difficult to
  3359.           detect.  Any time we relate the sales table with the cust table,
  3360.           the above row in the sales table will not be retrieved.  This
  3361.           could cause reports to disagree.  The section on correlated
  3362.           subqueries explains how to detect this problem.  Technically, it
  3363.           is called referential integrity.
  3364.           Another mistake that is easily made involves accidentally having
  3365.           leading or trailing spaces in a character value.  If we inserted
  3366.           the data for Compugorp with the following:
  3367.           
  3368.           insert into cust values('AA','Compugorp ','WA',20);
  3369.           
  3370.           You will notice the space after the last 'p' in Compugorp.  There
  3371.           will not be a problem with it until you try a query that includes
  3372.           the name of the company as shown below.
  3373.           select *
  3374.           from cust 
  3375.           where name = 'Compugorp';
  3376.           
  3377.           The row with Compugorp will not be found because the query did
  3378.           not contain the space after the last p!  This is a very
  3379.           frustrating error as I have seen quite a few students accuse me
  3380.           of having a serious malfunction in my program with the look on
  3381.           their faces suggesting what I could do with SSQL.
  3382.           All columns have a particular length associated with them.  This
  3383.           denotes the maximum number of characters that the column can
  3384.           contain.  When we created the cust table, the name column was
  3385.           defined as char(15) which means that the maximum number of
  3386.           characters it can contain is 15.  If more than 15 characters are
  3387.           in the name, only the first 15 are saved.
  3388.  
  3389.  
  3390.  
  3391.                                    INSERT-2
  3392.  
  3393.  
  3394.  
  3395.  
  3396.           
  3397.           insert into cust values('GG','Bizzlesnarf & Sons','CA',10);
  3398.           
  3399.           Although the above command would be allowed, when we retrieve the
  3400.           customer name, only "Bizzlesnarf & " would be displayed.
  3401.           
  3402.           Numeric Data
  3403.           The basic rule to remember with numeric data is not to enclose
  3404.           them in quotes as we can see above with the entry for rating. 
  3405.           The length of the column as specified in the create table command
  3406.           must not be exceeded.  Rating was created as numeric(2) which
  3407.           means that the maximum number of digits is 2.  The following
  3408.           insert command would cause an error because the entry for rating
  3409.           contains 3 digits instead of 2:
  3410.           
  3411.           insert into cust values('AA','Compugorp','WA',123);
  3412.           
  3413.           Because of the error, none of the values would be added to the
  3414.           table.
  3415.           
  3416.           Null data
  3417.           I really should not write "null data" or "null value" because the
  3418.           concept of the null means absence of a value.  If we add a
  3419.           customer that has no rating we would do something like:
  3420.           
  3421.           insert into cust values('HH','MagnaMice','AZ',null);
  3422.           
  3423.           There are NO QUOTES enclosing the word null.  Like all other
  3424.           keywords in SQL, it can be in uppercase or lowercase letters. 
  3425.           The null is different from a 0 (zero) in that a null means that
  3426.           there is no rating.  A 0 (zero) means that there IS a rating and
  3427.           the rating is 0.  This idea is further developed when discussing
  3428.           data retrieval in the section on the select command.          
  3429.  
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441.  
  3442.  
  3443.  
  3444.  
  3445.  
  3446.  
  3447.  
  3448.  
  3449.  
  3450.                                    INSERT-3
  3451.  
  3452.  
  3453.  
  3454.  
  3455.          UPDATE DATA IN A TABLE
  3456.          
  3457.          OVERVIEW
  3458.          The update command is used to change column values in existing
  3459.          rows based on criteria in a search_condition that is accomplished
  3460.          through a where clause. If there where any constraints put on the
  3461.          column values when the table was created such as not null or not
  3462.          null unique, the update command makes sure that the constraints
  3463.          are maintained before the columns are actually updated.  Next to
  3464.          the insert command, it is the most cumbersome to use.  To make
  3465.          updates a bit easier, I made a non-ANSI command which is fully
  3466.          explained in the full documentation.
  3467.          
  3468.          UPDATE SYNTAX
  3469.          
  3470.          UPDATE table_name
  3471.          SET column_name = value [, column_name = value...]
  3472.          WHERE search_condition
  3473.  
  3474.          The value can be a mathmatical expression.
  3475.  
  3476.          EXAMPLES:
  3477.          In the first example, the rating for customer AA is increased 
  3478.          by 5.
  3479.          
  3480.          update cust
  3481.          set rating = rating + 5
  3482.          where code = 'AA';
  3483.          
  3484.          In the example below, the rating for customer CC is changed to a
  3485.          null.
  3486.          
  3487.          update cust
  3488.          set rating = null
  3489.          where code = 'CC';
  3490.          
  3491.          
  3492.          The example below shows how to change the percent defects to 10
  3493.          for a batch of product GC that occurred on July 21, 1987 in
  3494.          Idaho.
  3495.          
  3496.          update manu
  3497.          set defects = 10
  3498.          where date = '07/21/87' and code = 'GC' and mst = 'ID';
  3499.          
  3500.          In the following, two columns are changed.  For customer BB,
  3501.          rating is changed to 14 and the state column is changed to Idaho.
  3502.          
  3503.          update cust
  3504.          set rating = 14, st = 'ID'
  3505.          where code = 'BB';
  3506.  
  3507.          
  3508.  
  3509.                                       UPDATE-1
  3510.  
  3511.  
  3512.  
  3513.  
  3514.  
  3515.          The example below is a bit more involved.  We want to change the
  3516.          rating of branch 2A customers who have purchased above average
  3517.          quantities (overall) to 30. The use of the select command in a
  3518.          where clause is covered in a later section.  
  3519.  
  3520.          update cust
  3521.          set rating = 30 
  3522.          where code in 
  3523.           (select distinct code 
  3524.            from cust, sales
  3525.            where cc=code  
  3526.              and bc='2A'  
  3527.              and qty >
  3528.             (select avg(qty) 
  3529.              from sales)); 
  3530.          
  3531.          COMPONENTS OF UPDATE
  3532.          
  3533.          table_name
  3534.          Any existing table in your database.
  3535.          
  3536.          column_name
  3537.          Any valid column name in the table name accessed, including the
  3538.          column(s) which form the key. 
  3539.           
  3540.          value
  3541.          The value is anything appropriate for the data type of the
  3542.          column.  Any value that would work with the insert command will
  3543.          work with the update command.  As with the insert command, the
  3544.          word null is valid to show that there is no value for the column. 
  3545.          Remember to put quotes around character values.
  3546.          
  3547.          
  3548.          search_condition
  3549.          The section on the select command has more detail on the
  3550.          search_condition that is accomplished with the use of a where
  3551.          clause.  Typically, you want to update a single row.  To update a
  3552.          single row the search_condition must test for the primary key so
  3553.          that will be explained here.  The primary key allows you to
  3554.          uniquely define each row.  The cust table, which is used in most
  3555.          of the examples above, has a primary key based on code.  That is,
  3556.          based on the value in code, we know only a single row will be
  3557.          updated.  
  3558.          
  3559.          Compound keys are discussed in the full documentation.
  3560.  
  3561.  
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568.                                 UPDATE-2
  3569.  
  3570.  
  3571.          DELETE ROWS FROM A TABLE
  3572.          
  3573.          OVERVIEW
  3574.          The delete command deletes rows based on the criteria in the
  3575.          where clause.  Without a where clause, all the data in the table
  3576.          is deleted.
  3577.          Refer to the section on the select statement for details on the
  3578.          where clause.  The basic difference between the select * and the
  3579.          delete is that with select, the rows are displayed, and with
  3580.          delete, the rows are deleted.  Because of this, it is a smart
  3581.          idea to use the select before you use the delete so you can see
  3582.          the rows that you are going to delete.
  3583.          You can only delete complete rows.  If you want to delete column
  3584.          data within a row, use the update command.
  3585.          
  3586.          SYNTAX
  3587.          
  3588.          DELETE FROM table_name
  3589.          [WHERE search_expression]
  3590.          
  3591.          EXAMPLES:
  3592.          
  3593.          The following shows how to delete all the data from the cust
  3594.          table.
  3595.          
  3596.          delete from cust;
  3597.          
  3598.          The following shows how to delete customers from the cust table
  3599.          whose rating is less than 10.
  3600.          
  3601.          delete from cust
  3602.          where rating < 10;
  3603.          
  3604.          COMPONENTS OF DELETE
  3605.          
  3606.          table_name
  3607.          Any existing table in your database.
  3608.          
  3609.          search_condition
  3610.          The section on the select statement has more detail on the
  3611.          search_condition that is accomplished with a where clause. A
  3612.          simple example is shown above. The command to delete customers
  3613.          whose rating is less than 10 could create some problems if there
  3614.          were customers in the sales table with customer codes that you
  3615.          deleted.  Whenever you delete a row that contains a primary key
  3616.          that is used in another table, problems can arise.  In order to
  3617.          determine whether there are any of those customers in the sales
  3618.          table, you could use the select command as shown below.
  3619.          
  3620.          select *
  3621.          from   sales
  3622.          where  cc in (
  3623.            select code
  3624.            from   cust
  3625.            where  rating < 10);
  3626.  
  3627.                                      DELETE-1
  3628.  
  3629.  
  3630.  
  3631.  
  3632.          
  3633.          To delete all the corresponding rows in the sales table for
  3634.          customers whose rating is less than 10, you would simply take the
  3635.          statement above and replace the "select * from" with "delete" as
  3636.          shown below.
  3637.          
  3638.          delete from sales
  3639.          where  cc in (
  3640.            select code
  3641.            from   cust
  3642.            where  rating < 10);
  3643.          
  3644.          The delete command below shows how to delete all rows in the
  3645.          sales table that do not have a corresponding customer code in the
  3646.          cust table. The not exists is covered in the full documentation.
  3647.          
  3648.          delete from sales
  3649.          where not exists
  3650.              (select *
  3651.               from cust
  3652.               where code = cc);
  3653.          
  3654.          HOW TO RESTORE DATA YOU HAVE JUST DELETED
  3655.          
  3656.          Type it in again!!! (Therefore be very careful when you use this
  3657.          command).
  3658.          
  3659.  
  3660.  
  3661.  
  3662.  
  3663.  
  3664.  
  3665.  
  3666.  
  3667.  
  3668.  
  3669.  
  3670.  
  3671.  
  3672.  
  3673.  
  3674.  
  3675.  
  3676.  
  3677.  
  3678.  
  3679.  
  3680.  
  3681.  
  3682.  
  3683.  
  3684.  
  3685.  
  3686.                                 DELETE-2
  3687.  
  3688.  
  3689.          TUTORIAL
  3690.          
  3691.          OVERVIEW
  3692.          This tutorial will take you through the basics creating a table,
  3693.          inserting data into a table, updating rows in a table, deleting
  3694.          rows from a table, retrieving data from a table, creating a view
  3695.          of a table and dropping a table.
  3696.          
  3697.          START SSQL
  3698.          From the DOS prompt type SSQL and press ENTER:
  3699.          
  3700.          CREATE THE TABLE
  3701.          Before we can put any data into the table, you need to describe
  3702.          the type(s) of data that it is going to contain.  Often the table
  3703.          we create is referred to as a base table.  Base tables contain
  3704.          actual data which is distinguished from a derived table (see
  3705.          Create a View). It is assumed that you have already read the
  3706.          introduction which contains the basic definitions of columns,
  3707.          rows, and tables. You will create a shortened version of a
  3708.          payroll table.  For each employee you will store the last name,
  3709.          the first name, salary, and city.  The key will be
  3710.          a combination of last name and first name. Type the following:
  3711.          
  3712.          create table pay (
  3713.          last char(15) not null,
  3714.          first char(15) not null,
  3715.          salary numeric(5),
  3716.          city char(12),
  3717.          unique(last, first)
  3718.          );
  3719.          
  3720.          Last name and first name can each be up to fifteen characters in
  3721.          length.  Since they are part of the primary key, nulls will not
  3722.          be allowed in the columns.  The combination of the column names
  3723.          last and first are set to unique so there can be no duplicate
  3724.          primary keys.  The values for city are limited to twelve
  3725.          characters.  Salary is limited to five digits which means up to
  3726.          $99,999.
  3727.          If you made a mistake, skip to the last part of the tutorial to 
  3728.          find out how to drop a table which erases it from the disk so 
  3729.          you can start over.
  3730.          
  3731.          INSERT DATA INTO THE TABLE
  3732.          The following has has the commands to insert four rows of data 
  3733.          into the table. When you type them, be careful to include all the
  3734.          appropriate quotes and commas.  And yes, include the in correct
  3735.          spelling of Scottsdale.  You will correct it in the next section.
  3736.          Note that Hippity Hopper has a null in place of his
  3737.          salary.  This means that the salary is not available which is
  3738.          different from putting a salary of zero.
  3739.          
  3740.          insert into pay values('Everski','Willy',45000,'Scottsdale');
  3741.          insert into pay values('Everski','Wilshe',60000,'Scootsdale');
  3742.          insert into pay values('Hopper','Hippity',null,'Phoenix');
  3743.          insert into pay values('Nosebleed','Harvey',20000,'Peoria');
  3744.  
  3745.                                 TUTORIAL-1
  3746.  
  3747.  
  3748.  
  3749.  
  3750.          If you made any mistakes, you should be able to correct them
  3751.          after going through the next section on updates.
  3752.          
  3753.          UPDATE DATA IN A TABLE
  3754.          You need to make two changes to the table.  Scootsdale needs to
  3755.          be changed to Scottsdale and the salary for Willy Everski needs
  3756.          to be changed to 55,000.  The where clause is needed to access 
  3757.          the specific row you need.
  3758.          
  3759.          update pay
  3760.          set city = 'Scottsdale'
  3761.          where city = 'Scootsdale';
  3762.          
  3763.          update pay
  3764.          set salary = 55000
  3765.          where last = 'Everski' and first = 'Willy';
  3766.          
  3767.          
  3768.          DELETE A ROW FROM A TABLE
  3769.          Harvey Nosebleed has been fired so you want to delete the row
  3770.          that contains the data for Harv.  Figure D-5 shows you how.  In a
  3771.          manner similar to the update, the where clause describes which
  3772.          row should be deleted.
  3773.          The where clause only includes the last name
  3774.          because I know that all the other Nosebleeds have been fired. 
  3775.          However, the primary key is both last and first names.  With the
  3776.          Everskis you had to include both last and first names otherwise
  3777.          both would have been updated.  OK, technically we could have
  3778.          gotten by with just the first names but I know you realize that
  3779.          in a real-world scenario that would not be acceptable.
  3780.          
  3781.          delete from pay
  3782.          where  last='Nosebleed';
  3783.          
  3784.          RETRIEVE DATA FROM A TABLE
  3785.          The select command is used to retrieve data from a table.  The
  3786.          following select command retrieves all the data in the table. 
  3787.          
  3788.          select *
  3789.          from pay;
  3790.          
  3791.          last          first         salary city         
  3792.          ------------- ------------- ------ ------------ 
  3793.          Everski       Willy         55000 Scottsdale
  3794.          Everski       Wilshe        60000 Scottsdale
  3795.          Hopper        Hippity             Phoenix   
  3796.          
  3797.          3 rows selected
  3798.  
  3799.  
  3800.  
  3801.  
  3802.  
  3803.  
  3804.                                 TUTORIAL-2
  3805.  
  3806.  
  3807.          The following shows how to retrieve the first name and last name for
  3808.          all the rows.
  3809.          
  3810.          select first, last
  3811.          from pay;
  3812.          
  3813.          first           last            
  3814.          --------------- --------------- 
  3815.          Willy           Everski         
  3816.          Wilshe          Everski         
  3817.          Hippity         Hopper          
  3818.          
  3819.          3 rows selected
  3820.          
  3821.          The following displays the rows of employees which
  3822.          have salaries of less than $58,000.  Notice that the row for
  3823.          Hippity Hopper is not displayed because all nulls are excluded. 
  3824.          
  3825.          select *
  3826.          from pay
  3827.          where salary > 58000;
  3828.          
  3829.          last          first         salary city         
  3830.          ------------- ------------- ------ ------------ 
  3831.          Everski       Willy          55000 Scottsdale   
  3832.          
  3833.          1 row selected
  3834.          
  3835.          CREATE A VIEW
  3836.          A view is an alternate way of looking at a table or even a
  3837.          combination of tables.  It is also known as a derived table
  3838.          because a view is derived from one or more base tables. We will
  3839.          create a view which excludes salary information.  The view is
  3840.          based on a select statement. The view accesses data in the base
  3841.          table every time it is used in a select statement.  There is
  3842.          never any data contained in the view.  The following creates the
  3843.          view.  
  3844.          
  3845.          create view pay1 as
  3846.          select last, first, city
  3847.          from pay;
  3848.          
  3849.          Now let's see what happens when all the columns of the view are 
  3850.          selected.
  3851.          
  3852.          select *
  3853.          from pay1;
  3854.          
  3855.          last            first           city
  3856.          --------------- --------------- ------------
  3857.          Everski         Willy           Scottsdale
  3858.          Everski         Wilshe          Scottsdale
  3859.          Hopper          Hippity         Phoenix
  3860.          
  3861.          3 rows selected
  3862.  
  3863.                                 TUTORIAL-3
  3864.  
  3865.  
  3866.  
  3867.  
  3868.  
  3869.          DROP A TABLE
  3870.          When a table is dropped, the description of the table is erased
  3871.          from the disk.  The drop command will only work on an empty
  3872.          table.  The delete command is needed to delete all the data in 
  3873.          the table then the drop command erases the table from the disk.  
  3874.          
  3875.          delete from pay;
  3876.          drop table pay;
  3877.          drop view pay1;
  3878.          
  3879.          EXIT SSQL
  3880.          This is a tough one - type EXIT and press ENTER
  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.  
  3906.  
  3907.  
  3908.  
  3909.  
  3910.  
  3911.  
  3912.  
  3913.  
  3914.  
  3915.  
  3916.  
  3917.  
  3918.  
  3919.  
  3920.  
  3921.  
  3922.                                 TUTORIAL-4
  3923.  
  3924.          
  3925.          
  3926.          FORM Version 0.9 - A Form Generator for SSQL
  3927.  
  3928.          ** OVERVIEW
  3929.          The Form program allows you to insert, update, view and delete 
  3930.          in a much more user-friendly environment than standard SQL 
  3931.          offers.  It will generate a standard form for a table that can 
  3932.          then be modified - or you can start from scratch.  Each column 
  3933.          in the table can have a: 
  3934.               Help screen
  3935.               Column validation - it exists in another table
  3936.               Column validation - it is within a particular range
  3937.               Column validation - it is within a set of values
  3938.          
  3939.          With the column validation, if a mistake is made, the correct 
  3940.          values are displayed on the screen. 
  3941.          
  3942.          You have full data editing capabilities and you can easily move 
  3943.          from prompt to prompt to make changes.  You can even change the 
  3944.          colors. 
  3945.          
  3946.          You can retrieve rows of information from the form based on 
  3947.          simple search criteria and then page through the rows 
  3948.          retrieved. 
  3949.          
  3950.          The current version is limited to tables with less than 24 
  3951.          rows. 
  3952.          
  3953.          However, as an incentive for you to register, only the basics 
  3954.          are given to you.  The order form is on the last page of the 
  3955.          SSQL documentation or you can just print ORDER.FRM. 
  3956.          
  3957.          
  3958.          SUMMARY OF KEYS WITHIN THE FORM
  3959.  
  3960.          UP/DOWN ARROWS will move you from prompt to prompt.
  3961.  
  3962.          LEFT/RIGHT ARROWS will move you within the prompt as long as 
  3963.          there is text. 
  3964.  
  3965.          HOME KEY will take you to the beginning of the text.
  3966.  
  3967.          END KEY will take you to the end of the text.
  3968.  
  3969.          INSERT KEY will switch between the insert mode and the 
  3970.          overwrite mode. 
  3971.  
  3972.          DELETE KEY will delete the character where the cursor is at. 
  3973.  
  3974.          PAGEUP (used in Update/View mode) will display the previous row 
  3975.          of information. 
  3976.  
  3977.          PAGEDOWN (used in Update/View mode) will display the next row 
  3978.          of information. 
  3979.  
  3980.  
  3981.                                     FORM-1
  3982.  
  3983.  
  3984.          ESC will cancel the entry and exit the form program.
  3985.  
  3986.          ENTER will move you from prompt to prompt.  At the last prompt, 
  3987.          it will accept the form and insert the information into the 
  3988.          table. 
  3989.          
  3990.          F1   When help has been defined for the form, a window containing 
  3991.               the help message will be displayed.
  3992.          
  3993.          F2   Takes the information you have already entered in the form and
  3994.               searches the table for a match.  All rows that match are
  3995.               retrieved.  You can use the PAGEUP/PAGEDOWN to display the
  3996.               other rows.
  3997.          
  3998.          F4   Displays the column value entered in the previous row.  This
  3999.               is useful when, for example, you need to enter the same city
  4000.               over and over.  Type the city once.  For the next row of
  4001.               information, just press the F4 when you get to the city
  4002.               column.
  4003.          
  4004.          ALT-I     Puts you in INSERT mode.
  4005.          ALT-V     Puts you in VIEW mode and turns off column validation.
  4006.          ALT-U     UPDATEs what is on the screen.
  4007.          ALT-D     DELETEs based on what is on the screen.
  4008.          
  4009.          
  4010.          GENERATING A FORM
  4011.          In order to more easily explain the commands in the form, let's
  4012.          start by having the form program generate a form for us.  To do
  4013.          that:
  4014.          1.   Make sure that the form program is accessable from the
  4015.               directory with your table files.  We will be using the CUST
  4016.               table that is on the SSQL distribution disk.
  4017.          2.   Type: form cust (and press ENTER)
  4018.          
  4019.          The form program generates a file called CUST.SCR which 
  4020.          describes the form and then runs the form and puts you in the 
  4021.          insert mode. Press ESC to exit the form. 
  4022.          
  4023.          Now you have a file on your disk called CUST.SCR which looks 
  4024.          like: 
  4025.          
  4026.          D1 Data Entry for CUST
  4027.          D1 CODE   __
  4028.          D1 NAME   _______________
  4029.          D1 ST     __
  4030.          D1 RATING __
  4031.          CO CODE
  4032.          CO NAME
  4033.          CO ST
  4034.          CO RATING
  4035.          TA CUST
  4036.          
  4037.          The first two columns on every line must contain a command.  The
  4038.          commands are:
  4039.  
  4040.                                     FORM-2
  4041.  
  4042.  
  4043.          D1   This displays information on the screen.  Anything you type
  4044.               will appear on the screen EXCEPT the underline.  The underline
  4045.               shows where data entry will occur.  Every underline must have
  4046.               a corresponding CO line which defines the column.  The first
  4047.               underline will be matched with the first CO line, the second
  4048.               underline will be matched with the second CO line, and so on. 
  4049.               The underline does not have to be as wide as the column is
  4050.               defined.  If, for example, the column is defined as being 80
  4051.               characters, you can have an underline only 30 characters wide. 
  4052.               It will create a text window.  Although you can only see 30
  4053.               characters at a time, you can still enter the full 80
  4054.               characters.
  4055.          
  4056.          CO   A single valid column name in the table must follow the CO
  4057.               command.  There must be a CO command for every underline that
  4058.               appears in the D1 commands.
  4059.          
  4060.          TA   The table name is put here.
  4061.          
  4062.          As for the ordering of the commands, the only thing to remember 
  4063.          is that the CO command must follow the corresponding underline.  
  4064.          We could rewrite the cust screen to be any of the following: 
  4065.          
  4066.          TA CUST
  4067.          D1 Data Entry for CUST
  4068.          D1 CODE   __
  4069.          CO CODE
  4070.          D1 NAME   _______________
  4071.          CO NAME
  4072.          D1 ST     __
  4073.          CO ST
  4074.          D1 RATING __
  4075.          CO RATING
  4076.          
  4077.          D1******************************************************
  4078.          D1*              CUSTOMER FORM                         *
  4079.          D1* Customer Number  __  Company Name  _______________ *
  4080.          D1* State __ Rating __                                 *
  4081.          D1******************************************************
  4082.          CO CODE
  4083.          CO NAME
  4084.          CO ST
  4085.          CO RATING
  4086.          TA CUST
  4087.          
  4088.          (The following reverses ST and RATING to show that you do not have
  4089.          to put the columns in the same order as they exist in the table.)
  4090.  
  4091.                                     FORM-3
  4092.  
  4093.  
  4094.          D1******************************************************
  4095.          D1*              CUSTOMER FORM                         *
  4096.          D1* Customer Number  __  Company Name  _______________ *
  4097.          D1* Rating __  State __                                *
  4098.          D1******************************************************
  4099.          CO CODE
  4100.          CO NAME
  4101.          CO RATING
  4102.          CO ST
  4103.          TA CUST
  4104.          
  4105.          
  4106.          *** INSERT ***
  4107.          You are in the insert mode when you start the form.  Refer to 
  4108.          the SPECIAL KEYS section for your options within the form.  For 
  4109.          a NULL, press the ENTER key without entering anything for the 
  4110.          column. Pressing the ENTER key at the last prompt will insert 
  4111.          the row into the table. 
  4112.          
  4113.          *** UPDATE ***
  4114.          NOTE: The Update will only work on tables where the Unique 
  4115.          constraint has been specified.  By using the Unique on one or 
  4116.          more columns when the table is created, it determines the key 
  4117.          for the table.  For more information, see the Create Table 
  4118.          instructions in SSQL.DOC. There are three basic steps to 
  4119.          updating: 
  4120.          1.   Press ALT-V to get into the VIEW mode.  
  4121.          2.   Retrieve the row you want to update.  You retrieve rows by 
  4122.               entering data in one or more prompts on the form and pressing
  4123.               F2.  It will retrieve all rows that are equal to the data you
  4124.               entered.  For example, you want to change the rating on a
  4125.               customer in Arizona, but you forget the exact name.  You could
  4126.               move to the ST prompt, type AZ, and press F2.  The first
  4127.               customer in AZ would be displayed. You could then use PgUp and
  4128.               PgDn to find the correct customer.
  4129.          3.   Change the data in any of the prompts.  If more than one row
  4130.               was retrieved, you can press PageDn to move to the next row. 
  4131.               When you have changed the row you want updated, Press Alt-U.
  4132.          
  4133.          *** VIEW ***
  4134.          The only difference between update and view is that in view, no
  4135.          data is changed.
  4136.          1.   Press ALT-V to get into the view mode.  
  4137.          2.   Retrieve the row(s) you want to view.  You retrieve rows by
  4138.               entering data in one or more prompts on the form and pressing
  4139.               F2.  It will retrieve all rows that are equal to the data you
  4140.               entered.  For example, if you wanted to view all the customers
  4141.               in Arizona, you would move to the ST prompt, type AZ, and
  4142.               press F2.  The first customer in AZ would be displayed.  
  4143.          3.   If more than one row was retrieved, you can press PageDn to
  4144.               move to the next row - then PageUp move back a row.
  4145.          
  4146.          *** DELETE ***
  4147.          To delete a row, you follow the steps for updating except at step
  4148.          3 you press ALT-D to delete the row being displayed.
  4149.  
  4150.                                     FORM-4
  4151.  
  4152.  
  4153.  
  4154.  
  4155.    ***************************************************************************
  4156.    * TO ORDER SEE THE LAST PAGE OF THE SSQL DOCUMENTATION OR PRINT ORDER.FRM *
  4157.    ***************************************************************************
  4158.          
  4159.    ***************************************************************************
  4160.    * IT IS ALWAYS A BIT SCARY TO SEND OUT SOFTWARE FOR THE FIRST TIME.       *
  4161.    * THERE ARE PROBABLY A FEW BUGS LURKING SOMEWHERE AND IT PROBABLY DOESN'T *
  4162.    * DO ALL YOU NEED.  PLEASE SEND ME SUGGESTIONS, CRITICISMS, ETC. EVEN IF  *
  4163.    * YOU DO NOT REGISTER.  THANKS!                                           *
  4164.    ***************************************************************************
  4165.  
  4166.  
  4167.  
  4168.          REPORT Version 0.9 - The SSQL Report Generator
  4169.          Copyright (C) 1990 by Steven Silva
  4170.  
  4171.          *** OVERVIEW ***
  4172.          SSQL Report allows you to produce a formatted report.  You use 
  4173.          a text editor or word processor to describe the report.  The 
  4174.          resultant file is stored for future use.  The file must be 
  4175.          stored in simple ASCII text.  For many word processors, this 
  4176.          means that you cannot save it normally - you need to save it as 
  4177.          a DOS text file.  In WordPerfect, it would be Ctrl-F5(Text 
  4178.          In/Out), T(Dos Text), S(Save).  Instead of just reading the 
  4179.          documentation, it is a good idea to first study and run the 
  4180.          examples and then come back to the documentation.  As is common 
  4181.          with shareware documentation, you need to register in order to 
  4182.          find out all the details of operation. 
  4183.          
  4184.          *** PROCESS A REPORT ***
  4185.          To process a report, first make sure that REPORT and SSQL are 
  4186.          either in the current directory or the path.  The tables you 
  4187.          reference MUST be in the current directory.  Type: 
  4188.          REPORT <report file>
  4189.          
  4190.          where <report file> is the text file you create which describes 
  4191.          the report. The files RPT1.FRM to RPT7.FRM have been included on 
  4192.          the distribution disk.  Make sure that RPT1.FRM is in your 
  4193.          current directory along with the DBF/SQD files.  Type: 
  4194.          REPORT RPT1.FRM 
  4195.          
  4196.          ***SYNTAX OF REPORT FILES***
  4197.          All lines begin with a command.  All commands are two 
  4198.          characters long.  The first character after the command 
  4199.          corresponds to column one on the report. The following example 
  4200.          shows you a basic report. 
  4201.  
  4202.          -- RPT1.FRM (With 3 tables and no indexing, it is slow on an
  4203.          --           old PC)
  4204.          HT                      SALES REPORT
  4205.          HT
  4206.          HT      DATE      CUST NAME        PROD DESCRIP     QTY
  4207.          HT      ********  ***************  ***************  ***
  4208.          DT      XXXXXXXX  XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX  999
  4209.          ET**** END OF REPORT ****
  4210.          DC date, name, descrip, qty
  4211.          TA sales,cust,prod
  4212.          WH cc = cust.code and
  4213.          WH pc = prod.code
  4214.          
  4215.          ** COMMENTS (--) 
  4216.          Begin a line with two dashes if you do not want it processed by 
  4217.          the program. 
  4218.          
  4219.          ** PRINTER (PR)
  4220.          If you want the report sent to the printer, the command is PR, 
  4221.          and the number after it refers to the printer number.  If you 
  4222.          do not know the printer number, it is probably 1.  
  4223.          
  4224.                                    REPORT-1
  4225.          
  4226.          It advances to the next page after 59 lines.  I made it 59 
  4227.          instead of 66 for those of us with Laserjets. If you do not have 
  4228.          the PR command, the report will be displayed on the screen. 
  4229.          
  4230.          ** HEADING (HT) (optional)
  4231.          The heading is produced at the top of every page or screen.    
  4232.          Headings can contain text, the current date (in both U.S. and 
  4233.          international formats), a page number, and you can skip a pre-
  4234.          determined number of lines (good for setting margins).  For 
  4235.          information on HOW to do all this, register and get the full 
  4236.          documentation. 
  4237.          
  4238.          ** DETAIL TEXT(DT)
  4239.          The detail represents the placement of information for each row 
  4240.          retrieved from the table.  The information can be placed on 
  4241.          more than one line.  It corresponds to the rows requested in 
  4242.          the Detail Columns (DC) line. It may contain text, but Xs and 
  4243.          9s have special meaning. 
  4244.          
  4245.          X
  4246.          This is used for character data.  It will only display the 
  4247.          number of characters represented by the number of Xs you have 
  4248.          for the column.  This is useful when a column may be defined as 
  4249.          having 30 columns but you just want to see the first 15.  You 
  4250.          would simply put only 15 Xs in the position you wanted the 
  4251.          column information to be displayed. 
  4252.          
  4253.          9
  4254.          The 9 represents numeric data.  You need to make sure that you 
  4255.          have enough 9s for the largest number in the column, otherwise 
  4256.          asterisks (*) will be displayed instead.  Numeric data can be 
  4257.          specially formatted (more on that in the full documentation). 
  4258.          
  4259.          ** FOOTER (FT) (optional)
  4260.          The footer is placed at the bottom of every page sent to the 
  4261.          printer.  It is skipped over if the report is sent to the 
  4262.          screen.  The options for the footer are the same as the ones 
  4263.          for the heading. 
  4264.          
  4265.          ** END OF REPORT (ET) (optional)
  4266.          This information appears just once, at the end of the report.  
  4267.          The options are that same as the ones for the heading. 
  4268.          
  4269.          ** DETAIL COLUMNS (DC)
  4270.          There must be a column for every format described under DT.  
  4271.          The format follows the syntax for the select statement in SSQL.  
  4272.          This means that you can have calculated columns, aggregate 
  4273.          functions - anything that you can put between the word SELECT 
  4274.          and the word FROM in a select statement.  There can be only one 
  4275.          DC line. 
  4276.          
  4277.          ** SUBTOTALS (optional) **
  4278.          Actually, subtotal is a misnomer because you can use any 
  4279.          aggregate function (count, avg, min, max, sum) - not just sum.  
  4280.          There are three parts to creating subtotals.  The first part is 
  4281.          the format (1T) which has the same rules as the ones for detail 
  4282.          
  4283.                                    REPORT-2
  4284.          
  4285.  
  4286.          text (DT).  The second part determines the group you want 
  4287.          subtotaled (1B).  The third part (1C) contains the aggregate 
  4288.          function(s) that you want in the subtotal.  The command sent to 
  4289.          SSQL is: 
  4290.          SELECT Break Column(s), Subtotals 
  4291.          FROM   (TA line) 
  4292.          WHERE  (WH line(s)) GROUP BY Break Column(s); 
  4293.          
  4294.          For example:
  4295.          
  4296.          --RPT2.FRM
  4297.          HT SALES REPORT
  4298.          HTCust  Prod  QTY 
  4299.          HT----  ----  ---
  4300.          DTXX    XX    999
  4301.          1T           ----
  4302.          1T Cust Tot  9999
  4303.          1T
  4304.          DC cc, pc, qty
  4305.          1B cc
  4306.          1C sum(qty)
  4307.          TA sales
  4308.          
  4309.          ** SUBTOTAL TEXT (1T)
  4310.          This is where you describe the format of the subtotal.  The 
  4311.          rules are the same as the ones for detail text (DT) except that 
  4312.          you can only use numeric data (9s). 
  4313.          
  4314.          ** BREAK COLUMN(S) (1B)
  4315.          This refers to the column(s) that you want the aggregate 
  4316.          functions grouped by.  The column(s) must also exist in the 
  4317.          Detail Columns (DT).  It functions just like the GROUP BY 
  4318.          clause.  See the manual under the section GROUP BY in chapter 
  4319.          10 for more information on what can be put here. 
  4320.          
  4321.          ** SUBTOTALS (1C)
  4322.          This is where you put the aggregate functions that you want 
  4323.          displayed in the format you described in the Subtotal Text 
  4324.          (1T).  You need one aggregate function for every numeric format 
  4325.          in 1T. 
  4326.          
  4327.          ** GRAND TOTAL (optional)
  4328.          Again a misnomer.  You can do more than total a column (sum), 
  4329.          you can use any aggregate function.  The grand total is 
  4330.          displayed once at the end of the report.  There are two parts: 
  4331.          grand total text (GT) describes the format, and grand total 
  4332.          columns (GC) describes the aggregate function that you want 
  4333.          displayed.  The command sent to SSQL is: 
  4334.          SELECT Grand Total Column(s) 
  4335.          FROM   (TA line) 
  4336.          WHERE  (WH line(s)) 
  4337.          
  4338.  
  4339.  
  4340.  
  4341.          
  4342.                                    REPORT-3
  4343.          
  4344.  
  4345.          For example:
  4346.          
  4347.          --RPT3.FRM
  4348.          HT SALES REPORT
  4349.          HTCust  Prod  QTY 
  4350.          HT----  ----  ---
  4351.          DTXX    XX    999
  4352.          GT           ----
  4353.          GT Grand Tot 9999
  4354.          DC cc, pc, qty
  4355.          GC sum(qty)
  4356.          TA sales
  4357.          
  4358.          
  4359.          ** GRAND TOTAL TEXT (GT)
  4360.          This is where you describe the format of the grand total.  The 
  4361.          rules are the same as the ones for detail text (DT) except that 
  4362.          you can only use numeric data (9s). 
  4363.          
  4364.          ** GRAND TOTAL COLUMN(S) (GC)
  4365.          List the aggregate function(s) that you want in the grand 
  4366.          total.  There must be one aggregate function for every numeric 
  4367.          format described in GT. 
  4368.          
  4369.          
  4370.          ** TABLE(S) (TA)
  4371.          This correponds to one or more tables that you want selected.  
  4372.          There can be only on TA line in a report.  Anything valid after 
  4373.          the first FROM in SQL is valid here.  See the manual in chapter 
  4374.          10 and 11 for more information on what can be put here. 
  4375.          
  4376.          ** THE WHERE CLAUSE (WH) (optional)
  4377.          Anything valid after the first FROM in SQL is valid here 
  4378.          including subqueries with the following exception:  You cannot 
  4379.          have a GROUP BY or an ORDER BY if you have also specified 
  4380.          subtotals.  Make sure that the DC, TA, and WH lines go together 
  4381.          to form a valid statement as in: 
  4382.          SELECT (DC line) 
  4383.          FROM   (TA line) 
  4384.          WHERE  (WH line(s)); 
  4385.          
  4386.          
  4387.                                    REPORT-4
  4388.          
  4389.  
  4390.          Some more extensive report formats and hints.
  4391.          
  4392.          --RPT4.FRM (Be patient when running this one.  It actually 
  4393.          --          contains three queries)
  4394.          --
  4395.          HT        SALES REPORT
  4396.          HT    MONOLITH MANUFACTURING 
  4397.          HTCustomer Name    Product     QTY 
  4398.          HT---------------  ----------  ---
  4399.          DTXXXXXXXXXXXXXXX  XXXXXXXXXX  999
  4400.          1T                          ------
  4401.          1T The customer total is    99,999
  4402.          1T
  4403.          GT                         -------
  4404.          GT GRAND TOTAL ............999,999
  4405.          ET
  4406.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4407.          ET                 END OF REPORT
  4408.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4409.          DC name, descrip, qty
  4410.          1B name
  4411.          1C sum(qty)
  4412.          GC sum(qty)
  4413.          TA sales,prod,cust
  4414.          WH sales.cc = cust.code and
  4415.          WH sales.pc = prod.code
  4416.          
  4417.          
  4418.          --RPT4A.FRM - another way of writing RPT4.FRM
  4419.          HT        SALES REPORT
  4420.          HT    MONOLITH MANUFACTURING 
  4421.          HTCustomer Name    Product     QTY 
  4422.          HT---------------  ----------  ---
  4423.          DTXXXXXXXXXXXXXXX  XXXXXXXXXX  999
  4424.          DC name, descrip, qty
  4425.          1T                          ------
  4426.          1T The customer total is    99,999
  4427.          1T
  4428.          1B name
  4429.          1C sum(qty)
  4430.          GT                         -------
  4431.          GT GRAND TOTAL ............999,999
  4432.          GC sum(qty)
  4433.          ET
  4434.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4435.          ET        END OF REPORT
  4436.          ET^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4437.          TA sales,prod,cust
  4438.          WH sales.cc = cust.code and
  4439.          WH sales.pc = prod.code
  4440.  
  4441.  
  4442.  
  4443.  
  4444.  
  4445.          
  4446.                                    REPORT-5
  4447.          
  4448.  
  4449.          --RPT5.FRM - Multiple line detail
  4450.          HT                      SALES REPORT
  4451.          HT
  4452.          HT      DATE      CUST CODE        PROD CODE        QTY
  4453.          HT                CUST NAME        PROD DESCRIP     
  4454.          HT      ********  ***************  ***************  ***
  4455.          DT      XXXXXXXX  XX               XX               999
  4456.          DT                XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX
  4457.          DT
  4458.          DC date, cc, pc, qty, name, descrip
  4459.          TA sales,cust,prod
  4460.          WH cc = cust.code and
  4461.          WH pc = prod.code
  4462.          
  4463.          
  4464.          --RPT6.FRM - Projections
  4465.          -- Minor quirk - when you repeat column names as in the DC line
  4466.          -- below, "rename" them by putting a unique identifier after each
  4467.          -- one.
  4468.          HT          THE EFFECT OF A PERCENTAGE INCREASE 
  4469.          HT                 IN PERCENT OF DEFECTS
  4470.          HT
  4471.          HT                MANU   PROD     ** DEFECTS **
  4472.          HT      DATE      STATE  CODE     0% 10% 20% 30% 
  4473.          HT      --------  --     --      --  --  --  --   
  4474.          DT      XXXXXXXX  XX     XX      99  99  99  99 
  4475.          DC date,mst,code,defects,defects*1.1 d2, defects*1.2 d3,defects*1.3 d4
  4476.          TA prod
  4477.          
  4478.          --RPT7.FRM Multiple subtotals
  4479.          -- Minor quirk - when you repeat column names as in the 1C line
  4480.          -- below, "rename" them by putting a unique identifier after each
  4481.          -- one.
  4482.          HT      Code Defects Qty
  4483.          DT      XX   99      99
  4484.          1T      Average      99
  4485.          1T      Total        99
  4486.          1T      Maximum      99
  4487.          1T      Minimum      99
  4488.          DC code, defects,qty
  4489.          TA prod
  4490.          1B code
  4491.          1C avg(defects), sum(defects) c2, max(defects) c3, min(defects) c4
  4492.          
  4493.          
  4494.    ***************************************************************************
  4495.    * TO ORDER SEE THE LAST PAGE OF THE SSQL DOCUMENTATION OR PRINT ORDER.FRM *
  4496.    ***************************************************************************
  4497.          
  4498.    ***************************************************************************
  4499.    * IT IS ALWAYS A BIT SCARY TO SEND OUT SOFTWARE FOR THE FIRST TIME.       *
  4500.    * THERE ARE PROBABLY A FEW BUGS LURKING SOMEWHERE AND IT PROBABLY DOESN'T *
  4501.    * DO ALL YOU NEED.  PLEASE SEND ME SUGGESTIONS, CRITICISMS, ETC. EVEN IF  *
  4502.    * YOU DO NOT REGISTER.  THANKS!                                           *
  4503.    ***************************************************************************
  4504.          
  4505.                                    REPORT-6
  4506.          
  4507.  
  4508.  
  4509.  
  4510.