Tải bản đầy đủ (.pdf) (5 trang)

The Language of SQL- P8 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (97.99 KB, 5 trang )

DATABASE DIFFERENCES: MySQL and Oracle
Both MySQL and Oracle will return a value in the header row for literal values. In MySQL, the header
for the first column in the previous example will appear as:
First Name:
In Oracle, the header for the first column will appear as:
'FIRSTNAME:'
One question you might very well ask is why the header row is important at all. If
you are using the
SELECT statement to bring back data in a computer program,
then you probably don’t care about the header. You only need the data. However,
if you are using the
SELECT statement to retrieve data for a report displayed to a
user, either on paper or on a computer screen, then the header might be relevant.
After all, when users look at a column of data, they generally want to know the
meaning of the column. In the case of a literal value, there really is no meaning to
the column, so a header isn’t truly necessary. But in other types of calculated
fields, there may be a meaningful label that could be applied to the column. Later
in this chapter, we will discuss the concept of column aliases, which is a way of
providing a header in this type of situation.
In addition to providing a column header where there is none, column aliases
also allow you to change the name of a column to something that may be more
meaningful for the person viewing the data. For example, a database designer
may have given your last name column the obscure name of LstNm222. A col-
umn alias can be employed to change it to something more descriptive.
One more point about literals. You might think that all literals need quotation
marks, but this is not necessarily true. For example, the following statement:
SELECT
5,
FirstName
FROM Orders
will return this data:


(no column name) FirstName
5 William
5 Natalie
5 Brenda
Literal Values 21
Even though the literal value 5 is completely meaningless, it is still a valid value.
Since it doesn’t have quote marks, the 5 is interpreted as a numeric value.
Arithmetic Calculations
Let’s return to a more typical example of a calculated field. Arithmetic calcula-
tions allow you to perform a calculation on one or more columns in a table. For
example:
SELECT
OrderID,
QuantityPurchased,
PricePerItem,
QuantityPurchased * PricePerItem
FROM Orders
will return this data:
OrderID QuantityPurchased PricePerItem (no column name)
1 4 2.50 10.00
2 10 1.25 12.50
3 5 4.00 20.00
The first three columns of the above SELECT are nothing different from what
you’ve previously seen. The fourth column is a calculated column with this
arithmetic expression:
QuantityPurchased * PricePerItem
In this case, the asterisk is a symbol that denotes multiplication. It doesn’t mean
‘‘all columns,’’ as was seen in the last chapter. In addition to the asterisk, several
other arithmetic operators are allowed. The most common are the following:
Arithmetic Operator Meaning

þ addition
À subtraction
à multiplication
= division
Chapter 3

Calculations and Aliases22
Also note that, as with the literals, the fourth column has no header, due to the
fact that it isn’t derived from a single column.
Concatenating Fields
Concatenation is a fancy computer term that means to combine or join character
data together. Just as arithmetic operations can be performed on numeric data,
character data can be concatenated together. The syntax for concatenation var-
ies, depending on the database you’re working with. Here’s an example from
Microsoft SQL Server:
SELECT
OrderID,
FirstName,
LastName,
FirstName þ ''þ LastName
FROM Orders
The data retrieved is:
OrderID FirstName LastName (no column name)
1 William Smith William Smith
2 Natalie Lopez Natalie Lopez
3 Brenda Harper Brenda Harper
Again, the first three columns are nothing new. The fourth column is this
expression:
FirstName þ ''þ LastName
The plus sign denotes concatenation. Since the operation involves character

rather than numeric data, SQL is smart enough to know that the plus sign means
concatenation and not addition. In this case, the concatenation expressed is
composed of three terms: the FirstName column, a literal space (' '), and the
LastName column. The literal space is necessary so that William Smith doesn’t
display as Willi amSmith.
Concatenating Fields 23
DATABASE DIFFERENCES: MySQL and Oracle
MySQL doesn’t use a symbol (such as +) to denote concatenation, but it does require you to use a
function called
CONCAT. We’ll be covering this function in the next chapter, but for now this is a
taste of what the same statement looks like in MySQL:
SELECT
OrderID,
FirstName,
LastName,
CONCAT (FirstName, ' ', LastName)
FROM Orders;
Oracle uses two vertical bars (||) rather than a plus sign (+) to denote concatenation. The equivalent
statement in Oracle is:
SELECT
OrderID,
FirstName,
LastName,
FirstName || ' ' || LastName
FROM Orders;
Column Aliases
In all the prior examples in this chapter, you have seen calculated fields with a
nondescriptive header. We’re now going to address the question as to how a
header can be specified for these types of columns. The answer is to use a column
alias. The term alias means an alternate name. Here’s an example of how to use a

column alias with the Microsoft SQL Server version of the previous
SELECT
statement:
SELECT
OrderID,
FirstName,
LastName,
FirstName þ ''þ LastName AS 'Name'
FROM Orders
Notice that the column alias of ‘Name’ is surrounded by single quotes. The
output is:
Chapter 3

Calculations and Aliases24
OrderID FirstName LastName Name
1 William Smith William Smith
2 Natalie Lopez Natalie Lopez
3 Brenda Harper Brenda Harper
The fourth column now has a header. The keyword AS is used to specify a col-
umn alias, which immediately follows the keyword.
DATABASE DIFFERENCES: MySQL and Oracle
The equivalent of the statement in MySQL is:
SELECT
OrderID,
FirstName,
LastName,
CONCAT (FirstName, ' ', LastName) AS 'Name'
FROM Orders;
Oracle does not require single quotes around column alias names. However, if the column alias
contains embedded spaces, then double quotes should be used. The same statement in Oracle is:

SELECT
OrderID,
FirstName,
LastName,
FirstName || ' ' || LastName AS Name
FROM Orders;
In addition to providing a header for a calculated field, column aliases are often
useful if a column in a table has a cryptic name that you’d like to change. For
example, if a table has a column with a name of Qty, you could issue this state-
ment to display the column as Quantity Purchased:
SELECT
Qty AS 'Quantity Purchased'
FROM table
Column Aliases 25

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×