Section 15

advertisement
Database Design Section 15 notes
lesson 1
slide 6: This lesson begins our exploration of SQL or Structured Query Language. This is the language
that we use to interact with database tables. From this point on you should have Oracle Application
Express open in a window on your computer so you can try examples as we go over them. You can
pause this audio at any time to practice what is being presented. using throughout the rest of the class
are: keyword, clause, and statement. A keyword refers to an individual SQL element. Keywords are
generally written in upper case. Examples of keywords are SELECT and FROM. or table name. A clause is
a part of a SQL statement. It includes a keyword plus some other words that describe column names or
other information. The example in the text is SELECT title. This is a clause. A statement is a combination
of two or more clauses. A full statement would be: SELECT title FROM d_songs. Take a moment now to
type this SELECT statement in Application Express. Run it and examine the results.
slide 8:As you look at the slide on this page, SELECT salary chooses the column from the database to
display information from. WHERE chooses the rows that the information is selected from. The WHERE
clause actually performs the selection, the SELECT clause actually does the projection. These are the
standard words that are used to describe the rows and columns shown from a database table. You can
use the projection capability in SQL to choose the columns in a table that you want returned by your
query. You can choose as few or as many columns from the table as you require. You can use the
selection capability in SQL to choose the rows in a table that you want returned by a query. You can use
various criteria to restrict the rows that you see.
Slide 10: You can display all the columns of data in a table by using an asterisk symbol instead of column
names in the SELECT clause. When reading SQL aloud, we refer to the asterisk as a star. For example,
SELECT star FROM employees. Run this example in Application Express and examine results. The star will
bring back all the columns in the table. Let’s look at the SELECT statement in more detail. The required
sections of the SELECT statement are: SELECT then a list of column names; followed by the FROM clause
and the table name. As you just saw, you can use the star in place of the column names to return all
columns in the table. Additional criteria may be added, which you will see in upcoming slides.
Slide 14: An arithmetic expression is the use of an arithmetic operator along with the data in the
database. An arithmetic operator may be used on numerical data only. When you display information
from a database using these operators, the expressions are not stored in the database. This is what we
call ‘derived’ data. We can use these arithmetic expressions and avoid entering derived data into our
database directly. An example of this would be age. You could derive age from birth date and then
subtract the birth date from today’s date to get the age of a person. Because dates are actually stored
in Oracle databases as a number, you can use the addition and subtraction operators with dates also.
Slide 17: In SQL, as in standard mathematics, multiplication and division take precedence over addition
and subtraction when you combine these operators into the same expression. If operators within an
expression are of the same priority, then evaluation is done left to right. It is always good practice to use
parentheses when you are combining different operators so that you have no doubt about which
operations are completed first.
Slide 20: This next slide describes a NULL value. If a row lacks the data value for a particular column, that
value is said to be null, or to contain a null. This is a good time to pause the audio to use Oracle’s
Application Express and examine some table descriptions. Once you log in to Application Express, click
the SQL workshop button or tab, then choose object browser. A list of your tables will appear on the
left. Click a table and you will see the table described on the right. Another way to view table
information is to type D-E-S-C space, then type in the name of the table you wish to examine. This can
be done from the SQL command processor window. Notice that each column has a descriptor nullable;
this tells you whether or not nulls are allowed in this column.
Slide 21: When a SQL statement contains arithmetic expressions that evaluate null values, the result is
always null. Remember, if you attempt to perform division with zero, you get an error. However, if you
divide a number by null, the result is null or unknown.
Slide 23: When you use derived values in columns, sometimes the name becomes cumbersome, and you
want to use a different descriptor for the heading. And other times, you just might need to use a
different word for the name of the column that is in the database. In these instances, a column alias may
be used to rename a column heading.
Slide 24: A column alias immediately follows the column name in the SELECT statement. The word AS is
a keyword that is used between the column name and the alias. This keyword is optional, but is helpful
for clarity. If the column alias contains spaces or if capitalization and lower case is important, then these
words have to be placed in double quotation marks. If you do not use the quotation marks to surround
the alias, only one word is allowed with no spaces, and it will also be displayed in all upper case no
matter how it is typed.
Slide 25: This slide shows you the syntax that we use to apply aliases. In the examples, the first simply
shows the renaming of the columns using an alias with the AS keyword. The column alias for the last
underscore name column is NAME. The column alias for commission underscore pct is COMM. Notice
that without quotation marks, all aliases resort to all upper case. The second example shows the
column alias applied to a calculated column. The salary times twelve column is ‘Annual Salary.’ See how
the alias immediately follows the column name? The AS keyword is used in the second column alias, but
not the first since only one word is used there, Name. The capitalization is maintained and the space is
allowed for ‘Annual Salary’ since it was placed within the double quotation marks in the SELECT clause.
Pause the audio now, and try these examples in Application Express.
Slide
Download