⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.htm

📁 21精通SQL
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	
	<TITLE>Teach Yourself SQL in 21 Days, Second Edition -- Day 2 -- Introduction to the Query: The SELECT Statement</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<CENTER>
<H1><IMG SRC="sams.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself SQL in 21 Days, Second Edition</FONT></H1>
</CENTER>
<CENTER>
<P><A HREF="ch01.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/ch01.htm"><IMG SRC="previous.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch03.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/ch03.htm"><IMG
SRC="next.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/index-1.htm"><IMG SRC="contents.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> 
<HR>

</CENTER>
<CENTER>
<H1><FONT COLOR="#000077">- Day 2 -<BR>
Introduction to the Query: The <TT>SELECT</TT> Statement</FONT></H1>
</CENTER>
<H2><FONT COLOR="#000077">Objectives</FONT></H2>
<P>Welcome to Day 2! By the end of the day you will be able to do the following:

<UL>
	<LI>Write an SQL query
	<P>
	<LI>Select and list all rows and columns from a table
	<P>
	<LI>Select and list selected columns from a table
	<P>
	<LI>Select and list columns from multiple tables
</UL>

<H2><FONT COLOR="#000077">Background</FONT></H2>
<P>To fully use the power of a relational database as described briefly on Day 1,
&quot;Introduction to SQL,&quot; you need to communicate with it. The ultimate communication
would be to turn to your computer and say, in a clear, distinct voice, &quot;Show
me all the left-handed, brown-eyed bean counters who have worked for this company
for at least 10 years.&quot; A few of you may already be doing so (talking to your
computer, not listing bean counters). Everyone else needs a more conventional way
of retrieving information from the database. You can make this vital link through
SQL's middle name, &quot;Query.&quot;</P>
<P>As mentioned on Day 1, the name Query is really a misnomer in this context. An
SQL query is not necessarily a question to the database. It can be a command to do
one of the following:

<UL>
	<LI>Build or delete a table
	<P>
	<LI>Insert, modify, or delete rows or fields
	<P>
	<LI>Search several tables for specific information and return the results in a specific
	order
	<P>
	<LI>Modify security information
</UL>

<P>A query can also be a simple question to the database. To use this powerful tool,
you need to learn how to write an SQL query.
<H2><FONT COLOR="#000077">General Rules of Syntax</FONT></H2>
<P>As you will find, syntax in SQL is quite flexible, although there are rules to
follow as in any programming language. A simple query illustrates the basic syntax
of an SQL select statement. Pay close attention to the case, spacing, and logical
separation of the components of each query by SQL keywords.</P>
<PRE><FONT COLOR="#0066FF"><B>SELECT NAME, STARTTERM, ENDTERM
FROM PRESIDENTS
WHERE NAME = 'LINCOLN';</B>
</FONT></PRE>
<P>In this example everything is capitalized, but it doesn't have to be. The preceding
query would work just as well if it were written like this:</P>
<PRE><FONT COLOR="#0066FF"><B>select name, startterm, endterm
from presidents
where name = 'LINCOLN';</B>
</FONT></PRE>
<P>Notice that <TT>LINCOLN</TT> appears in capital letters in both examples. Although
actual SQL statements are not case sensitive, references to data in a database are.
For instance, many companies store their data in uppercase. In the preceding example,
assume that the column <TT>name</TT> stores its contents in uppercase. Therefore,
a query searching for 'Lincoln' in the <TT>name</TT> column would not find any data
to return. Check your implementation and/or company policies for any case requirements.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Commands in SQL are not case sensitive.
	
<HR>


</BLOCKQUOTE>

<P>Take another look at the sample query. Is there something magical in the spacing?
Again the answer is no. The following code would work as well:</P>
<PRE><FONT COLOR="#0066FF"><B>select name, startterm, endterm from presidents where name = 'LINCOLN';</B>
</FONT></PRE>
<P>However, some regard for spacing and capitalization makes your statements much
easier to read. It also makes your statements much easier to maintain when they become
a part of your project.</P>
<P>Another important feature of ; (semicolon)semicolon (;)the sample query is the
semicolon at the end of the expression. This punctuation mark tells the command-line
SQL program that your query is complete.</P>
<P>If the magic isn't in the capitalization or the format, then just which elements
are important? The answer is keywords, or the words in SQL that are reserved as a
part of syntax. (Depending on the SQL statement, a keyword can be either a mandatory
element of the statement or optional.) The keywords in the current example are

<UL>
	<LI><TT>SELECT</TT>
	<P>
	<LI><TT>FROM</TT>
	<P>
	<LI><TT>WHERE</TT>
</UL>

<P>Check the table of contents to see some of the SQL keywords you will learn and
on what days.
<H2><FONT COLOR="#000077">The Building Blocks of Data Retrieval: SELECT and FROM</FONT></H2>
<P>As your experience with SQL grows, you will notice that you are typing the words
<TT>SELECT</TT> and <TT>FROM</TT> more than any other words in the SQL vocabulary.
They aren't as glamorous as <TT>CREATE</TT> or as ruthless as <TT>DROP</TT>, but
they are indispensable to any conversation you hope to have with the computer concerning
data retrieval. And isn't data retrieval the reason that you entered mountains of
information into your very expensive database in the first place?</P>
<P>This discussion starts with <TT>SELECT</TT> because most of your statements will
also start with <TT>SELECT</TT>:</P>
<H5>SYNTAX:</H5>
<PRE><FONT COLOR="#0066FF">SELECT &lt;COLUMN NAMES&gt;
</FONT></PRE>
<P>The commands, see also statementsbasic <TT>SELECT</TT> statement couldn't be simpler.
However, <TT>SELECT</TT> does not work alone. If you typed just <TT>SELECT</TT> into
your system, you might get the following response:</P>
<H5>INPUT:</H5>
<PRE><FONT COLOR="#0066FF">SQL&gt; <B>SELECT;</B></FONT></PRE>
<H5><FONT COLOR="#000000">OUTPUT:</FONT></H5>
<PRE><FONT COLOR="#000000"></FONT></PRE>
<PRE><FONT COLOR="#0066FF">SELECT
     *
ERROR at line 1:
ORA-00936: missing expression
</FONT></PRE>
<P>The asterisk under the offending line indicates where Oracle7 thinks the offense
occurred. The error message tells you that something is missing. That something is
the <TT>FROM</TT> clause:</P>
<H5>SYNTAX:</H5>
<PRE><FONT COLOR="#0066FF">FROM &lt;TABLE&gt;
</FONT></PRE>
<P>Together, the statements <TT>SELECT</TT> and <TT>FROM</TT> begin to unlock the
power behind your database.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>keywordsclausesAt this point you
	may be wondering what the difference is between a keyword, a statement, and a clause.
	SQL keywords refer to individual SQL elements, such as <TT>SELECT</TT> and <TT>FROM</TT>.
	A clause is a part of an SQL statement; for example, <TT>SELECT </TT>column1, column2,
	... is a clause. SQL clauses combine to form a complete SQL statement. For example,
	you can combine a <TT>SELECT</TT> clause and a <TT>FROM</TT> clause to write an SQL
	statement. 
<HR>
</P>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Each implementation of SQL has a
	unique way of indicating errors. Microsoft Query, for example, says it can't show
	the query, leaving you to find the problem. Borland's Interbase pops up a dialog
	box with the error. Personal Oracle7, the engine used in the preceding example, gives
	you an error number (so you can look up the detailed explanation in your manuals)
	and a short explanation of the problem. 
<HR>


</BLOCKQUOTE>

<H2><FONT COLOR="#000077">Examples</FONT></H2>
<P>Before going any further, look at the sample database that is the basis for the
following examples. This database illustrates the basic functions of <TT>SELECT</TT>
and <TT>FROM</TT>. In the real world you would use the techniques described on Day
8, &quot;Manipulating Data,&quot; to build this database, but for the purpose of
describing how to use <TT>SELECT</TT> and <TT>FROM</TT>, assume it already exists.
This example uses the <TT>CHECKS</TT> table to retrieve information about checks
that an individual has written.</P>
<P>The <TT>CHECKS</TT> table:</P>
<PRE><FONT COLOR="#0066FF">   CHECK# PAYEE                   AMOUNT REMARKS
--------- --------------------    ------ ---------------------
        1 Ma Bell                    150 Have sons next time
        2 Reading R.R.            245.34 Train to Chicago
        3 Ma Bell                 200.32 Cellular Phone
        4 Local Utilities             98 Gas
        5 Joes Stale $ Dent          150 Groceries
        6 Cash                        25 Wild Night Out
        7 Joans Gas                 25.1 Gas
</FONT></PRE>
<H2><FONT COLOR="#000077">Your First Query</FONT></H2>
<H5><FONT COLOR="#000000">INPUT:</FONT></H5>
<PRE><FONT COLOR="#0066FF">SQL&gt; <B>select * from checks;</B></FONT></PRE>
<H5><FONT COLOR="#000000">OUTPUT:</FONT></H5>
<PRE><FONT COLOR="#0066FF">queriesCHECK# PAYEE                   AMOUNT REMARKS 
------ --------------------   ------- ---------------------
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">     1 Ma Bell                    150 Have sons next time
     2 Reading R.R.            245.34 Train to Chicago
     3 Ma Bell                 200.32 Cellular Phone
     4 Local Utilities             98 Gas
     5 Joes Stale $ Dent          150 Groceries
     6 Cash                        25 Wild Night Out
     7 Joans Gas                 25.1 Gas

7 rows selected.
</FONT></PRE>
<H5>ANALYSIS:</H5>
<P>This output looks just like the code in the example. Notice that columns 1 and
3 in the output statement are right-justified and that columns 2 and 4 are left-justified.
This format follows the alignment convention in which numeric data types are right-justified
and character data types are left-justified. Data types are discussed on Day 9, &quot;Creating
and Maintaining Tables.&quot;</P>
<P>The asterisk (<TT>*</TT>) in <TT>select *</TT> tells the database to return all
the columns associated with the given table described in the <TT>FROM</TT> clause.
The database determines the order in which to return the columns.
<H3><FONT COLOR="#000077">Terminating an SQL Statement</FONT></H3>
<P>In some implementations of SQL, the semicolon at the end of the statement tells
the interpreter that you are finished writing the query. For example, Oracle's SQL*PLUS
won't execute the query until it finds a semicolon (or a slash). On the other hand,
some implementations of SQL do not use the semicolon as a terminator. For example,
Microsoft Query and Borland's ISQL don't require a terminator, because your query
is typed in an edit box and executed when you push a button.
<H3><FONT COLOR="#000077">Changing the Order of the Columns</FONT></H3>
<P>The preceding example of an SQL statement used the <TT>*</TT> to select all columns
from a table, the order of their appearance in the output being determined by the
database. To specify the order of the columns, you could type something like:</P>
<H5>INPUT:</H5>
<PRE><FONT COLOR="#0066FF">SQL&gt; <B>SELECT payee, remarks, amount, check# from checks;</B>
</FONT></PRE>
<P>Notice that each column name is listed in the <TT>SELECT</TT> clause. The order
in which the columns are listed is the order in which they will appear in the output.
Notice both the commas that separate the column names and the space between the final
column name and the subsequent clause (in this case <TT>FROM</TT>). The output would

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -