• Aucun résultat trouvé

Specify which of the following literals are correct and which are incorrect; also give the data type of the literal

Dans le document SQL for MySQL Developers (Page 115-121)

Common Elements

Exercise 5.1: Specify which of the following literals are correct and which are incorrect; also give the data type of the literal

---b'1001' 9 b'1111111' 127 b'0001' 1

Exercise 5.1: Specify which of the following literals are correct and which are incorrect; also give the data type of the literal.

1. 41.58E-8 2. JIM 3. 'jim' 4. 'A'14 5. '!?' 6. 45 7. '14E6' 8. '''''' 9. '1940-01-19'

10. '1992-31-12'

11. '1992-1-1' 12. '3:3:3' 13. '24:00:01'

14. '1997-31-12 12:0:0'

15. X'AA1' 16. TRUE

5.3 E

XPRESSIONS

An expression is a combination of literals, column names, complex calculations, operators, and functions that is executed according to certain rules and that leads to one value. An expression generally evaluatesto a value. Expressions are used, for example, in the SELECTandWHEREclauses of a SELECTstatement.

Example 5.5: Get the match number and the difference between sets won and sets lost for each match of which the number of sets won equals the number of sets lost plus 2.

SELECT MATCHNO, WON - LOST FROM MATCHES

WHERE WON = LOST + 2

Result:

MATCHNO WON - LOST --- ---1 2

Explanation: ThisSELECTstatement consists of several expressions. Four expres-sions come after the word SELECT: the columns MATCHNO, WON, and LOST, and the cal-culationWON - LOST. Four expressions also come after the word WHERE:WON,LOST,2, andLOST + 2.

An expression can be classified in three ways: by data type, by complexity of the value, and by form.

As with literals, the value of an expression always has a certain data type. Pos-sible data types are the same as for literals—among other things, alphanumeric, numeric, date, time, or timestamp. That is why we can call them, for example, inte-ger, alphanumeric, or date expressions. The following sections describe the various types of expressions individually.

Expressions can also be classified by the complexity of their value. So far, we have discussed only expressions that have one value as result—for example, a number, a word, or a date. These kinds of values are called scalar values.That’s why all the previous expressions are called scalar expressions.

Besides the scalar expressions, MySQL supports row expressions and table expressions. The result of a row expressionis a row consisting of a set of scalar val-ues. This result has a row value.Each row expression consists of one or more scalar expressions. If PLAYERNO, 'John', and 10000 are examples of scalar expressions, this is an example of a row expression:

(PLAYERNO, 'John', 100 * 50)

Imagine that the value of the PLAYERNO column is equal to 1; then the row value of this row expression is (1, 'John', 5000).

The result of a table expressionis a set of zero, one, or more row expressions.

This result is called a table value. If(PLAYERNO, 'John', 100 * 50),(PLAYERNO,

'Alex', 5000), and (PLAYERNO, 'Arnold', 1000 / 20)are examples of row expres-sions, then this is an example of a table expression:

((PLAYERNO, 'John', 100 * 50), (PLAYERNO, 'Alex', 5000),

(PLAYERNO, 'Arnold', 1000 / 20))

Imagine that the values of the three PLAYERNO columns are, respectively, 1,2, and3; the table value of this table expression, then, is equal to ((1, 'John', 5000),

(2, 'Alex', 5000), (3, 'Arnold', 50)). Note that these examples of row and table expressions are not correct SQL statements. The way we specify these expressions depends on the SQL statements in which they are used. The most popular form of a table expression is the SELECT statement. Each SELECT statement is also a table expression because the result or the value of a SELECTstatement is always a table and, therefore, a set of row values.

Sections 5.15 and 5.16 discuss row and table expressions, respectively, in more detail.

The third way to classify expressions is on the basis of form. We distinguish between singular and compound expressions. A singular expressionconsists of only one component. In the upcoming definition of expression,several possible forms of singular expressions are specified. You have already seen a few examples of it, such as a literal or the name of a column.

When an expression consists of operations and, thus, contains multiple singu-lar expressions, it is called a compound expression.Therefore, the expressions 20 *

100and'2002-12-12' + INTERVAL 2 MONTHare compound.

Table expressions can be compound as well. We can combine the result of two or more table expressions, which leads to one table value. Chapter 6, “SELECT Statements, Table Expressions, and Subqueries,” returns to this.

An example of a compound row expression is (1, 2, 3) + (4, 5, 6). This expression would have the following row value: (5, 7, 9). A new row value is put together from multiple row expressions. MySQL does not (yet) support compound row expressions, so this book does not cover them.

DEFINITION

<expression> ::=

<scalar expression> |

<row expression> |

<table expression>

<scalar expression> ::=

<singular scalar expression> |

<compound scalar expression>

<singular scalar expression> ::=

<literal> |

<column specification> |

<user variable> |

<system variable> |

<cast expression> |

<case expression> | NULL | ( <scalar expression> ) |

<scalar function> |

<aggregation function> |

<scalar subquery>

<row expression> ::=

<singular row expression>

<singular row expression> ::=

( <scalar expression> [ , <scalar expression> ]... ) |

<row subquery>

<table expression> ::=

<singular table expression> |

<compound table expression>

Before we discuss expressions and all their different forms, we explain the assignment of names to expressions, followed by the concepts from which scalar expressions are built. We have already described literals, but we still need to discus column specifications, system variables, case expressions, and functions.

Exercise 5.2:What is the difference between a literal and an expression?

Exercise 5.3:In which three ways can expressions be classified?

5.4 A

SSIGNING

N

AMES TO

R

ESULT

C

OLUMNS

When the result of a SELECTstatement is determined, MySQL must assign a name to each column of the result. If the expression in the SELECTclause consists of only a column name, the column in the result gets that name. Consider the next example.

Example 5.6: For each team, get the number and the division.

SELECT TEAMNO, DIVISION FROM TEAMS

Result:

TEAMNO DIVISION ---

---1 first 2 second

It’s obvious how MySQL came up with the names of the result columns in this example. But what is the name when something else is specified instead of a simple column name, such as a literal, or a complex expression? In that case, the name is equal to the entire expression. For example, if the expression WON * LOSTis used, the name of the result column is equal to WON * LOST.

Specifying an alternative name after an expression in a SELECTclause assigns a name to the matching result column. This is sometimes called a column headingor apseudonym.This column name is placed in the heading of the result. We recom-mend specifying a column name when an expression in a SELECT clause is not a simple column name.

Example 5.7: For each team, get the number and the division, and use the full names.

SELECT TEAMNO AS TEAM_NUMBER, DIVISION AS DIVISION_OF_TEAM FROM TEAMS

The result is:

TEAM_NUMBER DIVISION_OF_TEAM ---

---1 first 2 second

Explanation: After the column name, we specify the word AS followed by the name of the result column. The word AScan be omitted.

Example 5.8: For each penalty, get the payment number and the penalty amount in cents.

SELECT PAYMENTNO, AMOUNT * 100 AS CENTS FROM PENALTIES

The result is:

PAYMENTNO CENTS ---- ---1 ---10000 2 7500 3 10000 4 5000 : :

Explanation: When you look at the result, it is clear that the word CENTShas been placed above the second column. If we had not specified a column name in this example, MySQL would have come up with a name itself.

By way of illustration, this next example has somewhat more complex expressions and assigned column names.

Example 5.9: Get some data from the MATCHES table.

SELECT MATCHNO AS PRIMKEY, 80 AS EIGHTY,

WON - LOST AS DIFFERENCE,

TIME('23:59:59') AS ALMOST_MIDNIGHT, 'TEXT' AS TEXT

FROM MATCHES WHERE MATCHNO <= 4

The result is:

PRIMKEY EIGHTY DIFFERENCE ALMOST_MIDNIGHT TEXT --- -- -- --- ----1 80 2 23:59:59 TEXT 2 80 -1 23:59:59 TEXT 3 80 3 23:59:59 TEXT 4 80 1 23:59:59 TEXT

The examples specify only column names to make the result easier to read. The names are not mandatory in these examples. Later examples require column names;

you will see that the column names can be used in other parts of the statement.

So names for columns in a result are defined in the SELECTclause. These col-umn names can be used in most of the other clauses that are part of a select block, except for the FROMandWHEREclauses.

Example 5.10: Group all the penalties on penalty amount in cents, and order the result on that number of cents.

SELECT AMOUNT * 100 AS CENTS FROM PENALTIES

GROUP BY CENTS ORDER BY CENTS

The result is:

CENTS ---2500 3000 5000 7500 10000

New column names cannot be used in the same SELECTclause. Therefore, the clauseSELECT WON AS W, W * 2is not allowed.

Exercise 5.4: For each match, get the match number and the difference between

Dans le document SQL for MySQL Developers (Page 115-121)