< Day Day Up > |
6.1 Using SQL Expressions and FunctionsExpressions are a common element of SQL statements, and they occur in many contexts. For example, expressions often occur in the WHERE clause of SELECT, DELETE, and UPDATE statements to identify which records to retrieve, delete, or update. But expressions may be used in many other places; for example, in the output column list of a SELECT statement, or in ORDER BY or GROUP BY clauses. Terms of expressions consist of constants (literal numbers, strings, dates, and times), NULL values, references to table columns, and function calls. Terms may be combined using operators into more complex expressions. Many types of operators are available, such as those for arithmetic, comparison, logical, and pattern-matching operations. Here are some examples of expressions:
All these types of values can be combined into more complex expressions to produce other values of interest. The following statement demonstrates this: mysql> SELECT Name, -> TRUNCATE(Population/SurfaceArea,2) AS 'people/sq. km', -> IF(GNP > GNPOld,'Increasing','Not increasing') AS 'GNP Trend' -> FROM Country ORDER BY Name LIMIT 10; +---------------------+---------------+----------------+ | Name | people/sq. km | GNP Trend | +---------------------+---------------+----------------+ | Afghanistan | 34.84 | Not increasing | | Albania | 118.31 | Increasing | | Algeria | 13.21 | Increasing | | American Samoa | 341.70 | Not increasing | | Andorra | 166.66 | Not increasing | | Angola | 10.32 | Not increasing | | Anguilla | 83.33 | Not increasing | | Antarctica | 0.00 | Not increasing | | Antigua and Barbuda | 153.84 | Increasing | | Argentina | 13.31 | Increasing | +---------------------+---------------+----------------+ The expressions in the preceding statement refer to these types of values:
Many functions are available in MySQL. This study guide uses several of them for examples, but they make up only a fraction of the number available. Consult the MySQL Reference Manual for a complete list of functions and how to use them. For the purpose of testing, Appendix C, "Quick Reference," lists a subset of functions with which you should familiarize yourself before going to the exam. Note that when you invoke a function, there must be no space after the function name and before the opening parenthesis. (It's possible to change this default behavior by starting the server with a special option; see section 6.5, "Using Reserved Words as Identifiers.") More examples of SQL expressions can be found in the sample exercises at the end of the chapter. 6.1.1 Case Sensitivity in String ComparisonsString comparisons are somewhat more complex than numeric or temporal comparisons. Numbers sort in numeric order and dates and times sort in temporal order, but string comparisons depend not only on the specific content of the strings, but on whether they are binary or nonbinary and on the character set of the server. A letter in uppercase may compare as the same or different than the same letter in lowercase, and a letter with one type of accent may be considered the same or different than that letter with another type of accent. The earlier discussion in Chapter 4, "Data Definition Language," on data and column types described how strings may be binary or nonbinary, and how the properties of these two types of strings differ. To summarize:
The rules that govern string comparison apply in several ways. They determine the result of comparisons performed explicitly with operators such as = and <, and comparisons performed implicitly by ORDER BY, GROUP BY, and DISTINCT operations. Literal strings are nonbinary by default, and thus are not case sensitive. You can see this by comparing strings that differ only in lettercase:
mysql> SELECT 'Hello' = 'hello';
+-------------------+
| 'Hello' = 'hello' |
+-------------------+
| 1 |
+-------------------+
Depending on the character set, strings might not be accent sensitive, either. If the German sorting order is in use, ue and ü are the same:
mysql> SELECT 'Mueller' = 'Müller';
+----------------------+
| 'Mueller' = 'Müller' |
+----------------------+
| 1 |
+----------------------+
A nonbinary string can be treated as a binary string by preceding it with the BINARY keyword. If either string in a comparison is binary, both strings are treated as binary: mysql> SELECT BINARY 'Hello' = 'hello'; +--------------------------+ | BINARY 'Hello' = 'hello' | +--------------------------+ | 0 | +--------------------------+ mysql> SELECT 'Hello' = BINARY 'hello'; +--------------------------+ | 'Hello' = BINARY 'hello' | +--------------------------+ | 0 | +--------------------------+ The same principles apply to CHAR and VARCHAR table columns. By default, they're nonbinary, but you can add BINARY to a column definition to make the column binary. Suppose that a table t contains a CHAR column c and has the following rows (each of which means "goodbye"):
mysql> SELECT c FROM t;
+-----------+
| c |
+-----------+
| Hello |
| goodbye |
| Bonjour |
| au revoir |
+-----------+
Because c is a nonbinary CHAR column, its character set collating order controls how its values are sorted. For the default character set (Latin-1, also known as ISO-8859-1), the collating order isn't case sensitive, so uppercase and lowercase are treated as identical and a sort operation that uses ORDER BY produces results like this:
mysql> SELECT c FROM t ORDER BY c;
+-----------+
| c |
+-----------+
| au revoir |
| Bonjour |
| goodbye |
| Hello |
+-----------+
If c is declared as a CHAR BINARY column instead, ORDER BY sorts based on raw byte codes and produces a different result. Assuming that values are stored on a machine that uses ASCII codes, uppercase precedes lowercase and the results look like this:
mysql> SELECT c FROM t ORDER BY c;
+-----------+
| c |
+-----------+
| Bonjour |
| Hello |
| au revoir |
| goodbye |
+-----------+
String comparison rules also apply to GROUP BY and DISTINCT operations. Suppose that t has a column c with the following contents:
mysql> SELECT c FROM t;
+---------+
| c |
+---------+
| Hello |
| hello |
| Goodbye |
| goodbye |
+---------+
If c is a CHAR column, GROUP BY and DISTINCT do not make lettercase distinctions: mysql> SELECT c, COUNT(*) FROM t GROUP BY c; +---------+----------+ | c | COUNT(*) | +---------+----------+ | Goodbye | 2 | | Hello | 2 | +---------+----------+ mysql> SELECT DISTINCT c FROM t; +---------+ | c | +---------+ | Hello | | Goodbye | +---------+ On the other hand, if c is a CHAR BINARY column, those operations do take lettercase into account: mysql> SELECT c, COUNT(*) FROM t GROUP BY c; +---------+----------+ | c | COUNT(*) | +---------+----------+ | Goodbye | 1 | | Hello | 1 | | goodbye | 1 | | hello | 1 | +---------+----------+ mysql> SELECT DISTINCT c FROM t; +---------+ | c | +---------+ | Hello | | hello | | Goodbye | | goodbye | +---------+ The preceding discussion shows that to understand sorting and comparison behavior for literal strings or string columns, it's important to know whether they are binary or nonbinary. This is important when using string functions as well. String functions may treat their arguments as binary or nonbinary strings, or return binary or nonbinary results. It depends on the function. Here are some examples:
These examples demonstrate that you must take into account the properties of the particular function you want to use. If you don't, you might be surprised at the results you get. See the MySQL Reference Manual for details on specific functions. |
< Day Day Up > |