home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1990 / 03 / ssql / intro.shr < prev    next >
Encoding:
Text File  |  1989-01-03  |  22.3 KB  |  571 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.                                      SSQL
  20.                                   VERSION 1.4b
  21.  
  22.  
  23.                        COPYRIGHT (C) 1988 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 my class on fourth generation 
  33.                      languages at the DeVry Institute of 
  34.                      Technology, Phoenix, Arizona.
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.                                  TABLE OF CONTENTS
  42.          
  43.          
  44.          INTRODUCTION  . . . . . . . . . . . . . . . . . . . . . . . INTRO-1
  45.            Key Words Needed to Understand the Documentation  . . . . INTRO-1
  46.            What Is SQL And Why Is It So Important To Know? . . . . . INTRO-1
  47.            How Does This Implementation Of SQL Differ From Others? . INTRO-3
  48.            Differences In The Registered Version . . . . . . . . . . INTRO-4
  49.            How To Register . . . . . . . . . . . . . . . . . . . . . INTRO-5
  50.            Tutorial  . . . . . . . . . . . . . . . . . . . . . . . . INTRO-6
  51.            Permission to copy  . . . . . . . . . . . . . . . . . . . INTRO-8
  52.            Changes from 1.2 to 1.3 . . . . . . . . . . . . . . . . . INTRO-8
  53.            Changes from 1.3 to 1.4 . . . . . . . . . . . . . . . . . INTRO-8
  54.  
  55.          THE NEW SSQL ENVIRONMENT  (1.4) . . . . . . . . . . . . . . ENV-1
  56.          
  57.          EXTRACTING DATA FROM A SINGLE TABLE . . . . . . . . . . . . SELECT-1
  58.            Distinct  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-2
  59.            Where . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-4
  60.            search_expression . . . . . . . . . . . . . . . . . . . . SELECT-4
  61.            Special Search Expression - is null, is not null  . . . . SELECT-5
  62.            Special Search Expression - like, not like  . . . . . . . SELECT-7
  63.            And, Or, Not  . . . . . . . . . . . . . . . . . . . . . . SELECT-8
  64.            Any . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-12
  65.            In. . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-13
  66.            All . . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-13
  67.            Mathmatical Functions Avg, Min, Max, Sum, Count . . . . . SELECT-14
  68.            Group by, Having  . . . . . . . . . . . . . . . . . . . . SELECT-16
  69.            Order by  . . . . . . . . . . . . . . . . . . . . . . . . SELECT-17
  70.            Into  . . . . . . . . . . . . . . . . . . . . . . . . . . SELECT-18
  71.       
  72.          SUBQUERIES  . . . . . . . . . . . . . . . . . . . . . . . . SUB-1
  73.  
  74.          JOINING . . . . . . . . . . . . . . . . . . . . . . . . . . JOIN-1
  75.  
  76.          CREATE A TABLE  . . . . . . . . . . . . . . . . . . . . . . CREATE-1
  77.  
  78.          CREATE A VIEW . . . . . . . . . . . . . . . . . . . . . . . VIEW-1
  79.  
  80.          INSERT DATA INTO A TABLE  . . . . . . . . . . . . . . . . . INSERT-1
  81.  
  82.          UPDATE DATA IN A TABLE  . . . . . . . . . . . . . . . . . . UPDATE-1
  83.  
  84.          DELETE DATA FROM A TABLE  . . . . . . . . . . . . . . . . . DELETE-1
  85.  
  86.          APPENDIX A - SAMPLE QUERIES . . . . . . . . . . . . . . . . APP-1
  87.  
  88.          APPENDIX B - ANSWERS TO TUTORIAL  . . . . . . . . . . . . . APP-3
  89.  
  90.          APPENDIX C - ORDER FORM . . . . . . . . . . . . . . . . . . LAST PAGE
  91.          
  92.          
  93.          
  94.          KEY WORDS NEEDED TO UNDERSTAND THE DOCUMENTATION
  95.  
  96.          SQL - Structured Query Language. A standard method of 
  97.          interacting with a database.  It is pronounced "SEQUEL"!!  NEVER 
  98.          SAY THE LETTERS S-Q-L!!  IT WILL BRING SEVEN YEARS OF BAD LUCK 
  99.          AND SHOW PEOPLE THAT YOU ARE NEW TO SQL!!!
  100.          
  101.          TABLE - A table is typically known as a FILE in other systems.
  102.          You may ask why they don't just call a table a file.  It is 
  103.          because that although normally a table does refer to a specific 
  104.          file, a table can refer to something that spans two or more 
  105.          files.  This can be done by "creating a view" (see 
  106.          documentation). If you read a book on relational databases, they 
  107.          will probably refer to a table as a relation. 
  108.  
  109.          ROW - A row corresponds to a record or a portion of a record in 
  110.          a file.  In relational theory it is called a tuple.
  111.  
  112.          COLUMN - A column is typically known as a field in other 
  113.          systems.  In relational theory it is called an attribute.
  114.  
  115.          The above names were created to give relational databases a 
  116.          consistent and accurate view of data.
  117.  
  118.          EXAMPLE:
  119.  
  120.          You may have a TABLE named sales which contains COLUMNs called 
  121.          date, custnum, partnum and quantity.  Every time you made a 
  122.          sale, you would add a ROW of data to the TABLE.
  123.  
  124.                       COLUMNS
  125.            -----------------------------
  126.           |         |        |         |
  127.          date    custnum  partnum  quantity
  128.          ------  -------  -------  --------
  129.          880201  8524     AD873         928       <-- ROW
  130.          880203  7687     VF8709         87       <-- ROW
  131.  
  132.          ----------------------------------
  133.                         ^
  134.                         |
  135.                       TABLE 
  136.  
  137.          WHAT IS SQL AND WHY IS IT SO IMPORTANT TO KNOW?
  138.          
  139.          SQL stands for Structured Query Language.  It was developed as 
  140.          a standard method to query (extract data from) a relational 
  141.          database and do other operations to maintain relational 
  142.          databases.  Relational databases look at files as if they were 
  143.          simply tables.  SQL was developed years ago at a theoretical 
  144.          level but because of its inherent inefficiencies and programming 
  145.          complexity, it has been very difficult to create workable programs.  
  146.          It provides the most flexible approach to extracting data from a 
  147.          database.  It allows us to extract data in seconds that would 
  148.          take a knowledgeable programmer hours, days or weeks to extract, 
  149.          even if the programmer had the most advanced non-SQL languages 
  150.          available.  For a sampling of the types of queries that SQL can 
  151.          handle, refer to Appendix A.
  152.  
  153.                                       INTRO-1
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.          It is also important to know that popular databases such as 
  161.          Rbase and dBase, are coming out with, or have already come out
  162.          with  SQL versions.  
  163.  
  164.          The new operating system for PCs, OS/2, will have an extended 
  165.          version (at a cost of about $800) which will include SQL.  IBM 
  166.          has stated that SQL is to become the standard interface for 
  167.          databases.
  168.  
  169.          
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.                                   INTRO-2
  210.          
  211.          HOW DOES THIS IMPLEMENTATION DIFFER FROM THE OTHERS?
  212.          The following is a table from the January, 1988 issue of BYTE.  
  213.          I have added SSQL to the end of the table for comparison: 
  214.  
  215.          SQL Command  Informix  Ingres  Oracle  SQLBase  XDB   XQL    SSQL
  216.                         2.0      5.0     5.1     3.2.2    II   1.0     1.1
  217.                        ($795)   ($950)  ($1295)  ($995) ($395) ($795) ($30)
  218.          DML (Data Manipulation Language)
  219.          SELECT         Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  220.            COLUMNS      Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  221.            EXPRESSIONS  Yes      Yes      Yes     Yes    Yes    No     No  
  222.          DISTINCT       Yes      Yes      Yes     Yes    Yes    No     Yes 
  223.          FROM           Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  224.          WHERE          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  225.          GROUP BY       Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  226.          HAVING         Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  227.          ORDER BY       Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  228.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  229.          UPDATE SET     Yes      Yes      Yes     Yes    Yes    Yes     1 
  230.          WHERE          Yes      Yes      Yes     Yes    Yes    Yes    Yes  
  231.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    No     Yes 
  232.          INSERT INTO    Yes      Yes      Yes     Yes    Yes    Yes     2  
  233.          SUBQUERY       Yes      Yes      Yes     Yes    Yes    No     No  
  234.          DELETE FROM    Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  235.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  236.          UNION          Yes      Yes      Yes     Yes    Yes    No     No  
  237.          CORRELATED -                                                      
  238.          SUBQUERIES     Yes      Yes      Yes     Yes    Yes    No     No  
  239.          DML Predicates 
  240.          BETWEEN        Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  241.          LIKE           Yes      Yes      Yes     Yes    Yes    No     Yes 
  242.          IS NULL        Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  243.          EXISTS         Yes      Yes      Yes     Yes    Yes    No     No  
  244.          ALL            Yes      Yes      Yes     Yes    Yes    No     Yes 
  245.          ANY            Yes      Yes      Yes     Yes    Yes    No     Yes 
  246.          SOME           No       No       No      No     No     No     No  
  247.          [NOT]          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  248.          DML Functions 
  249.          AVG            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  250.          COUNT(*)       Yes      Yes      Yes     Yes    Yes    No     No  
  251.          COUNT          Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  252.          MAX            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  253.          MIN            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  254.          SUM            Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  255.          DDL (Data Definition Language)
  256.          ALTER TABLE    Yes      Yes      Yes     Yes    Yes    Yes     3  
  257.          CREATE TABLE   Yes      Yes      Yes     Yes    Yes    Yes    Yes 
  258.            NOT NULL     Yes      Yes      Yes     Yes    Yes    No     No  
  259.          CREATE INDEX   Yes      Yes      Yes     Yes    Yes    Yes    No  
  260.          CREATE UNIQUE
  261.          INDEX          Yes      Yes      Yes     Yes    Yes    No     No  
  262.          CREATE VIEW    Yes      Yes      Yes     Yes    Yes    Yes    Yes
  263.          DROP TABLE     Yes      Yes      Yes     Yes    Yes    Yes    Yes
  264.          DROP INDEX     Yes      Yes      Yes     Yes    Yes    Yes    No  
  265.  
  266.          1. Although the syntax is a bit different (better), the insert 
  267.          is available.
  268.          2. The update exists, but you must update one row (record) at a 
  269.          time.  You cannot use a calculation to update a set of rows.
  270.          3. Although the syntax is different, You can delete columns in a 
  271.          table, change the names of the columns, change the size and data 
  272.          type of a column, etc.
  273.                                       INTRO-3
  274.          
  275.          
  276.          
  277.          
  278.          
  279.          STRENGTHS AND WEAKNESSES OF SSQL
  280.  
  281.          My emphasis has been on the data manipulation language since 
  282.          that is the most difficult to master and it is the most useful 
  283.          to the end-user.  Since the current version of SSQL cannot 
  284.          create indexes, querying large tables tends to be slow in 
  285.          comparison.  When joining tables, SSQL on a PC with a RAM disk 
  286.          can evaluate about 750-800 rows/minute.  On a PC AT with a hard 
  287.          disk it is about 3200 rows/minute.  
  288.  
  289.          I don't think SSQL can be touched on a price/performance basis 
  290.          though.  I use Oracle 5.1 - the stack of documentation is over 
  291.          a foot high and it requires a PC AT with one megabyte of 
  292.          extended memory.  It is an excellent package but not everybody 
  293.          needs the power of a $1,295 product. 
  294.  
  295.          SSQL documentation is oriented toward the end-user, not the 
  296.          programmer.
  297.  
  298.          DIFFERENCES IN THE REGISTERED VERSION
  299.          
  300.          PROGRAM
  301.          There is NO difference in the programs except that registered 
  302.          users are assured of getting the most current version.
  303.  
  304.          DISK-BASED DOCUMENTATION (UNREGISTERED USERS)
  305.          The disk-based documentation only shows you enough to get you 
  306.          started.  However, it does show all the options so you 
  307.          can see for yourself that the commands actually work.  
  308.  
  309.          FULL DOCUMENTATION (REGISTERED USERS)
  310.  
  311.          The full documentation includes the disk-based documentation but 
  312.          it is printed with a laser printer.
  313.          It also has:
  314.  
  315.          1) A full explanation on how to simultaneously extract data out 
  316.             of more than one table. This is called joining tables.
  317.  
  318.          2) In-depth information on how to create subqueries.  This 
  319.             technique allows you to put select statements within select 
  320.             statements.  This is where the word Structured comes from in 
  321.             the word Structured Query Language. 
  322.             
  323.          3) Answers and detailed explanations to the queries in the 
  324.             appendix.  This shows the amazing flexibility of SQL and why 
  325.             it is becoming so popular. 
  326.  
  327.          4) The power of SQL can be TOTALLY lost if the user does not 
  328.             understand the basics of data normalization.  Data 
  329.             normalization involves the rules for creating tables.  If the 
  330.             tables are not organized correctly, SQL cannot be used to its 
  331.  
  332.                                       INTRO-4
  333.          
  334.  
  335.  
  336.  
  337.  
  338.             full potential.  It is important to note that the topic is 
  339.             discussed with the non-technical end-user in mind.  Since 
  340.             there have not been any widely available SQL program, all the 
  341.             books on normalization tend to be very theoretical and 
  342.             academically oriented.  
  343.             
  344.             Even if our objective is to just create tables in the third 
  345.             normal form (there are more), going to the standard text 
  346.             books is frustrating.  For example, one of the best books on 
  347.             databases is AN INTRODUCTION TO DATABASE SYSTEMS by C. J. 
  348.             Date.  His definitions for first, second and third normal 
  349.             forms are:
  350.  
  351.             First normal form -
  352.             A relation R is in first normal form (1NF) if and only if all 
  353.             underlying domains contain atomic values only. 
  354.  
  355.             Second normal form - 
  356.             A relation R is in second normal form if and only if it is in 
  357.             1NF and every nonkey attribute is fully dependent on the 
  358.             primary key.
  359.  
  360.             Third normal form - 
  361.             A relation R is in third normal form (3NF) if and only if it 
  362.             is in 2NF and every nonkey attribute is nontransitively 
  363.             dependent on the primary key.
  364.  
  365.             Unfortunately, the above definitions are designed for 
  366.             students of database theory, not the typical end-user who 
  367.             wants to create some simple tables.  I give plenty of examples 
  368.             and try to avoid the jargon as much as possible.
  369.  
  370.          5) Full information on utilities to delete columns from 
  371.          tables, modify column names, change the width of columns, create 
  372.          tables which are subsets of a current table, etc. 
  373.  
  374.  
  375.          HOW TO REGISTER
  376.  
  377.          Send $30 plus $2.50 shipping and handling to:
  378.  
  379.          Silvaware
  380.          3902 North 87th St.
  381.          Scottsdale, AZ 85251
  382.  
  383.          Make checks payable to STEVE SILVA.  Sorry, no VISA or 
  384.          MasterCharge. There is an Order Form on the last page.
  385.  
  386.          You will receive full documentation and the latest version of 
  387.          SSQL.
  388.          
  389.  
  390.                                       INTRO-5
  391.          
  392.          
  393.          
  394.          
  395.          
  396.          TUTORIAL
  397.  
  398.          Read the section on Getting Data From a Single Table.  As you 
  399.          read the documentation, it is better to have SSQL running. Type 
  400.          the following at the prompt where the program and associated 
  401.          files reside:
  402.  
  403.          SSQL    (press <ENTER>)
  404.  
  405.          After a brief message, the cursor will stop at the following 
  406.          prompt: 
  407.  
  408.          SSQL>
  409.  
  410.          At this point, you can enter any SQL command.  I suggest that 
  411.          you type the select statements as you read about them.  As you 
  412.          understand the command, try your own variations.
  413.  
  414.          The table we want to create involves customer information.  We 
  415.          want to store the customer's code, name, state, and rating.
  416.          The following is the table we want to create:
  417.          
  418.          code name            st rating
  419.          ---- --------------- -- ------
  420.          c1   Compugorp       WA     20
  421.          c2   Techoharps      OR     
  422.          c3   Organomice      AZ     34
  423.  
  424.          
  425.          Our first step is to create the table.  Please refer to the 
  426.          section on creating a table, then type:
  427.  
  428.          SSQL> create cust ( 
  429.              > code char 2   
  430.              > name char 15  
  431.              > st char 2     
  432.              > rating num 2  
  433.              > ) 20;         
  434.          
  435.  
  436.          The above will create a table called "cust" with a column called 
  437.          code which is two characters wide, a column called name which is 
  438.          fifteen characters wide, a column called st which is two 
  439.          characters wide and a column called rating which is a 
  440.          number that can be two digits.  The last line tells you how many 
  441.          customers it can hold which is 20.  It is important to note the 
  442.          details - the "(" after cust, the ")" on the last line, and the 
  443.          ";" which ends the command.
  444.  
  445.          Now we need to put some data into the table.  Read the section 
  446.          on Insert Data Into the Table.  Type "insert cust", press 
  447.          <ENTER>, and type data for each prompt.  After you type all the 
  448.          data and are prompted to enter a new code, press the ESC key to 
  449.          exit the insert mode.  It will respond with the number of rows 
  450.  
  451.                                       INTRO-6
  452.          
  453.          
  454.          
  455.          
  456.          
  457.          inserted. 
  458.  
  459.          The screen should look like:
  460.  
  461.          SSQL> insert cust                
  462.          code            : c1             
  463.          name            : Compugorp______
  464.          st              : WA             
  465.          rating          : 20             
  466.          ----                             
  467.          code            : c2             
  468.          name            : Techoharps_____
  469.          st              : OR             
  470.          rating          : __             
  471.          ----                             
  472.          code            : c3             
  473.          name            : Organomice_____
  474.          st              : AZ             
  475.          rating          : 34             
  476.          ----                             
  477.          code            : <ESC>    <-- Press ESC to exit
  478.          3 rows inserted 
  479.          
  480.          There is a special case for customer c2.  Note that the rating 
  481.          column is blank.  This means that a rating is not appropriate 
  482.          for this customer.  This is not to say that the rating is zero.  
  483.          This is explained further in the section on the "select" 
  484.          statement.  Just press <ENTER> when you come to the rating
  485.          prompt for c2. 
  486.  
  487.          Now refer to the section called EXTRACTING DATA FROM A SINGLE 
  488.          TABLE in order to understand how to use the "select" statement 
  489.          and the logic of the "where" clause which are helpful in 
  490.          completing the tutorial.
  491.  
  492.          Let's assume that you made a mistake entering the name for 
  493.          customer c2.  The name should be Technoharps instead of 
  494.          Techoharps.
  495.  
  496.          Refer to the section called UPDATE DATA IN A TABLE for the 
  497.          solution and some other ways to update tables.
  498.  
  499.  
  500.          QUIZ TIME!!!  (THE ANSWERS ARE IN APPENDIX C)
  501.  
  502.          1. List all the data in the table.
  503.          2. List the customer name and rating for all customers in AZ.
  504.          3. List all the data for ratings from 10 to 35.
  505.          4. List the average rating.
  506.          5. How many customers have a rating of less than 25.
  507.  
  508.          The above will give you a start.  The real power of SQL would be 
  509.          realized if you had other related tables as explained in 
  510.  
  511.                                     INTRO-7
  512.          
  513.          
  514.          
  515.          
  516.          
  517.          Appendix A. 
  518.  
  519.          For deleting data, refer to the section called DELETING DATA 
  520.          FROM A TABLE.
  521.  
  522.  
  523.          PERMISSION TO COPY
  524.  
  525.          Please copy this disk and give it to a friend (or anybody else).
  526.          However, the following restrictions apply:
  527.          1.  No changes can be made to the distribution disk, including the 
  528.          documentation.
  529.          2. You cannot copy or reproduce the printed manual.  
  530.  
  531.          Any commercial, educational, governmental and other such 
  532.          organizations are required to purchase a copy of SSQL for 
  533.          every building it is used in.
  534.  
  535.          Quantity discounts available.
  536.  
  537.  
  538.          CHANGES FROM 1.2 TO 1.3
  539.  
  540.          Most of the changes were bug fixes - most of them in the area 
  541.          of create and insert.
  542.  
  543.          The use of a macro symbol, "@", which will call a text file 
  544.          with a command in it such as:
  545.  
  546.          SSQL>@REPORT1  <ENTER>
  547.  
  548.          where there exists a text file on the disk called REPORT1 which 
  549.          contains a "select" statement.  See the section on the new SSQL 
  550.          environment for more details.
  551.  
  552.          CHANGES FROM 1.3 TO 1.4
  553.  
  554.          More bug fixes.  This time the emphasis was on nested selects 
  555.          and large tables.
  556.  
  557.          Major changes in the user environment.  You can call your 
  558.          favorite word processor from within SSQL to make changes to your 
  559.          current statement or any other script file you have created.  
  560.          See the section on the new SSQL environment for more details.
  561.          
  562.          Numeric edits in INSERT.
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.                                      INTRO-8
  570.  
  571.