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 mig
Trang 1D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
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 theSELECTstatement 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 theSELECTstatement 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
Trang 2Even 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)
The first three columns of the aboveSELECT 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
Chapter 3 ■ Calculations and Aliases
22
Trang 3Also 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)
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 WilliamSmith
Trang 4D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
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 Aliases
24
Trang 5OrderID FirstName LastName Name
The fourth column now has a header The keywordAS is used to specify a
col-umn alias, which immediately follows the keyword
D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
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