VISUAL QUICKSTART GUIDE SQL Second Edition [Electronic resources]

Chris Fehily

نسخه متنی -صفحه : 169/ 7
نمايش فراداده

l xmlns="http://www.w3.org/1999/l">

  • About This Book

    This book will teach you how to use the SQL programming language to maintain and query database information. After some expository material about DBMSes, the relational model, and SQL syntax in Chapters 1 through 3, I revert to the task-based, visual style that you''re familiar with if you''ve read other

    Visual QuickStart books.

    Although I don''t assume that you''ve had programming experience, I do expect that you''re competent with your operating system''s filesystem and know how to issue commands at a command prompt or shell (called the DOS prompt in older Windows versions or Terminal in Mac OS X).

    This book isn''t an exhaustive guide to SQL; I''ve limited its scope to the most-used statements. For information about other SQL statements, refer to your DBMS''s documentation or an SQL reference that covers the standard more completely.

    Tips

    • Peter Gulutzan and Trudy Pelzer''s

      SQL-99 Complete, Really (CMP Books) explains the complete SQL-99 standard. It''s less agonizing to read than the SQL standard itself, but it doesn''t cover individual DBMSes.

    • Kevin Kline, Daniel Kline, and Brand Hunt''s

      SQL in a Nutshell (O''Reilly) is an extensive SQL:2003 reference that covers the same DBMSes as this book (except Access). It''s appropriate for SQL programmers who already have learned the basics.

    • Troels Arvin''s "Comparison of Different SQL Implementations" explains how different DBMSes implement various SQL features, complete with links to source documentation and other SQL books, articles, and resources. It covers SQL:2003 and the same DBMSes as this book (except Access). It''s at http://troels.arvin.dk/db/rdbms.

    Companion Web Site

    At Appendix A). You can write me directly at fehily@pacbell.net with questions, suggestions, corrections, and gripes related to this book.

    Audience

    My audience is database-application programmers and database end-users (not database designers or administrators), so this book is appropriate for you if you:

    • Lack programming experience but are familiar with computers.

    • Are learning SQL on your own or from an instructor.

    • Are otherwise uninterested in databases but must process large amounts of structured information because of the nature of your work. (Accountants, investigators, scientists, epidemiologists, statisticians, web programmers, analysts, sales reps, financial planners and traders, office managers, and managers often fall in this group.)

    • Want to move beyond friendly but underpowered graphical query tools.

    • Are moving from a desktop to a server DBMS (see the sidebar in this section).

    • Already know some SQL and want to move past simple SELECT statements.

    • Need to create, change, or delete database objects such as tables, indexes, and views.

    • Need to embed SQL code in C, Java, Visual Basic, PHP, or other host languages.

    • Are a web programmer and need to display query results on web pages.

    • Need a desktop SQL reference book.

    SQL Server vs. Desktop DBMSes

    An

    SQL server DBMS acts as the server part of a client/server network; it stores databases and responds to SQL requests made by many clients. A

    client is an application or computer that sends an SQL request to a server and accepts the server''s response. If your network uses a client/server architecture, the client is the computer on your desk, and the server is a powerful, specialized machine in another room, building, or country. The rules that describe how client/server requests and responses are transmitted are part of DBMS protocols and interfaces such as ODBC, JDBC, and ADO.NET.

    A

    desktop DBMS is a stand-alone program that can store a database and do all the SQL processing itself or behave as a client of an SQL server. A desktop DBMS can''t accept requests from other clients (that is, it can''t act like an SQL server).

    SQL servers include Microsoft SQL Server, Oracle, DB2, MySQL, and PostgreSQL. Desktop systems include Microsoft Access and FileMaker Pro. Note that

    SQL server (not capitalized) can refer to any vendor''s SQL server product, and

    SQL Server (capitalized) is Microsoft''s particular SQL server product. By convention, I use

    client and

    server to refer to client and server software itself or to the machine on which the software runs, unless the distinction is important.

    Chapter 2).

  • Proprietary extensions that DBMS vendors add beyond the basic SQL statements.

  • Advanced programming or administration. I don''t cover installation, privileges, triggers, recursion,[*] stored procedures, replication, backup and recovery, optimization, cursors, collations, character sets, or translations.

    [*] To understand recursion, you first must understand recursion.

  • Typographic conventions

    I use the following typographic conventions:

    Italic type introduces new terms or represents replaceable variables in regular text.

    Monospace type denotes SQL code and syntax in listings and in regular text. It also shows executables, filenames, directory (folder) names, URLs, and command-prompt text.

    Red monospace type highlights SQL code fragments and results that are explained in the accompanying text.

    Italic monospace type denotes a variable in SQL code that must be replaced with a value. You''d replace

    column with a real column name, for example.

    Syntax conventions

    SQL is a free-form language without restrictions on line breaks or the number of words per line, so I use a consistent style in SQL syntax diagrams and code listings to make the code easy to read and maintain:

    • Each SQL statement begins on a new line.

    • The indentation level is two spaces.

    • Each clause of a statement begins on a new, indented line:

      SELECT au_fname, au_lname
      FROM authors
      ORDER BY au_lname;
      

    • SQL is case-insensitive, which means that myname, MyName, and MYNAME are considered to be identical identifiers. I use UPPERCASE for SQL keywords such as SELECT, NULL, and CHARACTER (see "SQL Syntax" in Chapter 3), and lowercase or lower_case for user-defined values, such as table, column, and alias names.

      (User-defined identifiers are case sensitive when quoted and in a few other situations for some DBMSes, so it''s safest to respect identifier case in SQL programs.)

    • Table i.1 shows special symbols that I use in syntax diagrams.

      Table i.1. Syntax Symbols

      C

      HARACTERS

      D

      ESCRIPTION

      |

      The vertical-bar or pipe symbol separates alternative items. You can choose exactly one of the given items. (Don''t type the vertical bar.) A | B | C is read "A or B or C." Don''t confuse the pipe symbol with the double-pipe symbol, ||, which is SQL''s string-concatenation operator.

      []

      Brackets enclose one or more

      optional items. (Don''t type the brackets.) [A | B | C] means "type A or B or C or type nothing." [D] means "type D or type nothing."

      {}

      Braces enclose one or more

      required items. (Don''t type the braces.) {A | B | C} means "type A or B or C".

      ...

      Ellipses mean that the preceding item(s) can be repeated any number of times.

      ()

      Parentheses, unlike the preceding symbols, actually are part of the SQL language; type them as indicated in the syntax diagram.

    • All quote marks in SQL code are straight quotes (such as '' and "), not curly, or smart, quotes (such as '' and "). Curly quotes prevent code from working.

    • When a column is too narrow to hold a single line of code or out put, I break it into two or more segments. A gray arrow indicates a continued line.

    Using SQL with a specific DBMS

    This icon indicates a vendor-specific departure from the SQL:2003 standard. If you see this icon, it means that a particular vendor''s SQL dialect doesn''t comply with the standard, and you must modify the listed SQL program to run on your DBMS. For example, the standard SQL operator that joins (concatenates) two strings is || (a double pipe), but Microsoft products use + (a plus sign) and MySQL uses the CONCAT() function instead, so you''ll need to change all occurrences of

    a ||

    b in the example SQL listing to

    a +

    b (if you''re using Microsoft Access or Microsoft SQL Server) or to CONCAT(

    a,b ) (if you''re using MySQL). In most cases, the SQL examples will work as is or with minor syntactic changes. Occasionally, SQL code won''t work at all because the DBMS doesn''t support a particular feature.

    This book covers the following DBMSes (see the next chapter for details):

    • Microsoft Access

    • Microsoft SQL Server

    • Oracle

    • IBM DB2

    • MySQL

    • PostgreSQL

    If you''re using a different DBMS (such as Teradata, Sybase, or Informix), and one of the SQL examples doesn''t work, read the documentation to see how your DBMS''s SQL implementation departs from the SQL standard.