Beginning with the Oracle Database 10g release, you can use regular expressions within your conditions. Regular expressions allow for much more complex pattern matching without the need for multiple conditions. For example, if you wanted to find all customers whose name begins with W, ends in “ies” and does not include L anywhere in the string, you could use multiple conditions with the LIKE and NOT LIKE operators: SELECT name NAME You can achieve the same result more succinctly, in a single expression, with the new REGEXP_LIKE function: SELECT name NAME If that second argument to REGEXP_LIKE looks like gibberish, fear not: we cover regular expressions in detail in Chapter 17. Handling NULLThe NULL expression represents the absence of a value. If, when entering an order into the database, you are uncertain when the order will be shipped, it is better to leave the ship date undefined than to fabricate a value. Until the ship date has been determined, therefore, it is best to leave the ship_dt column NULL. NULL is also useful for cases where data is not applicable. For example, a cancelled order’s shipping date is no longer applicable and should be set to NULL. When working with NULL, the concept of equality does not apply; a column may be NULL, but it will never equal NULL. Therefore, you will need to use the special operator IS NULL when looking for NULL data, as in: UPDATE cust_order In this example, all orders whose shipping date hasn’t been specified will have their expected shipping date set to tomorrow. You may also use the IS NOT NULL operator to locate non-NULL data: UPDATE cust_order This example sets the expected shipping date to NULL for all orders that have already shipped. Notice that the SET clause uses the equality operator (=) with NULL, whereas the WHERE clause uses the ISNOT NULL operator. The equality operator is used to set a column to NULL, whereas the ISNOT NULL operator is used to evaluate whether a column is NULL. A great many mistakes might have been avoided had the designers of SQL chosen a special operator to be utilized when setting a column to NULL (i.e., SET expected_ship_dt TO NULL), but this is not the case. To make matters worse, Oracle doesn’t complain if you mistakenly use the equality operator when evaluating for NULL. The following query will parse and execute but will never return rows: SELECT order_nbr, cust_nbr, sale_price, order_dt Hopefully, you would quickly recognize that the previous query never returns data and replace the equality operator with ISNULL. However, there is a more subtle mistake involving NULL that is harder to spot. Say you are looking for all employees who are not managed by Marion Blake, whose employee ID is 7698. Your first instinct may be to run the following query: SELECT fname, lname, manager_emp_i d FNAME LNAME MANAGER_EMP_ID While this query returns rows, it leaves out those employees who are top-level managers and, thus, are not managed by anyone. Since NULL is neither equal nor not equal to 7698, this set of employees is absent from the result set. To ensure that all employees are considered, you will need to explicitly handle NULL, as in: SELECT fname, lname, manager_emp_id FNAME LNAME MANAGER_EMP_ID Including two conditions for every nullable column in your WHERE clause can get a bit tiresome. Instead, you can use Oracle’s built-in function NVL, which substitutes a specified value for columns that are NULL, as in: SELECT fname, lname, manager_emp_i d FNAME LNAME MANAGER_EMP_ID In this example, the value -999 is substituted for all NULL values, which, since -999 is never equal to 7698, guarantees that all rows whose manager_emp_id column is NULL will be included in the result set. Thus, all employees whose manager_emp_id column is NULL or is not NULL and has a value other than 7698 will be retrieved by the query.
blog comments powered by Disqus |
|
|
|
|
|
|
|