Oracle XSQL- P9 pptx

20 214 0
Oracle XSQL- P9 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

A couple of times, you’ve used a select statement that has a where clause, such as the following: SELECT ename,sal FROM emp WHERE sal > 1000 This statement returns just the names of employees with salaries greater than 1,000. This example has all the component parts. Here’s a translation of this select statement in plain English: SELECT the data I want FROM one or more data sources WHERE these conditions are met The first component—”the data I want”—is called the elements clause. In our first statement, you used the wildcard operator to say that you wanted all the fields for all the rows, where as in the second statement you asked only for two specific fields. The second component is the target—it tells the database what objects will be involved. The last component, the where clause, is optional, as is demonstrated in the first statement above. However, you’ll almost always use it in real-life code. The first topic covered will actually be the second component—the target. You need to know a little bit about this before you can fully understand the element expression, which is covered secondly. The section that follows is about the where clause. Target Clause The target clause is typically a comma-delimited list of tables, but it can also contain a couple other types of objects. First you’ll learn what else can be in the list. Then, since you have experience with select statements involving only one table, you’ll learn what happens when there is more than one object, as well as take your first look at joins. Finally, you’ll learn table aliasing and when it is used. Types of Target Objects So far, you’ve only seen tables used as target objects. However, tables can also contain snapshots and views, which you haven’t learned about yet. A view acts like a table but is actually a window—or “view”—onto the content of one or more tables. A snapshot is a view that refreshes periodically. Usually, a snapshot is used when there is a table that you want on a remote database; however, going across the network on every query is too costly. For now, just be aware that select statements can select from more than just tables. Their syntax for using views and snapshots in the target is exactly the same as it is for tables, so you won’t have to relearn anything. Multiple Target Objects and Joins When you have more than one target object involved, you have what is known as a join. The join is the fundamental piece of all of relational databases and allows you to arbitrarily map the data of one table with that of another. A simple example, found at the end of this section, is mapping an employee’s name in the emp table to the depart- ment’s name in the dept table. Before looking at that, it is important to understand what is happening beneath the surface. 140 Chapter 8 When more than one target object is listed in the target clause, the database does a Cartesian product between the two tables. A Cartesian product is the mapping of each member of one set with each member of another set. This is best illustrated by exam- ple. Try the following SQL statement: SELECT ename, dname FROM emp,dept Notice that there is a lot of repeated data in the result. Also notice the number of rows returned: 56. The emp table has 15 rows, while the dept table has 4. The number of rows you got back is 14 multiplied by 4, or 56. If you look at the data, you’ll see that for each employee, each department is listed. That result is a Cartesian product between ename and dname. You’re probably wondering, “Why would I ever want a result like that?” You really don’t. However, what you are seeing here is the foundation operation of a relational database, the join. You have taken two different sets of data and multiplied them so that every permutation is available to you. You then use the where clause to choose just the data you want. Without getting too far ahead of yourself, here is a simple example: SELECT ename, dname FROM emp,dept WHERE dept.deptno=emp.deptno Now you have only 14 rows, each having the correct department name for the employee. You’ve joined two different entities from two different tables—ename and dname. You can have any number of objects that you desire in your list. The Cartesian prod- uct works the same way. For instance, if you add the bonus table to your target as shown in the following select statement—and the bonus table has 3 rows—then you will get 168 rows: 14 × 4 × 3 = 168. If the bonus table has 0 rows, which is the default, you’ll get 0 rows: 14 × 4 × 0 = 0. SELECT * FROM emp,dept,bonus Target Aliasing In the previous example, you referenced the deptno columns by specifying the table and the column name. Aliasing allows you to declare that you want an object to be known by a different name in the SQL statement. Here is an example: SELECT ename, dname, a.deptno,b.deptno FROM emp a,dept b Why would you want to do this? Three reasons. First, it can make your SQL more readable. Second, you need to alias if you use subqueries or cursors. Third, it allows you to perform self-joins. This is where you join a table to itself. Here is an example with the dept table. The dept table has 4 rows, and your query will return 4 × 4 = 16 rows. SELECT a.deptno,b.deptno FROM dept a,dept b Oracle SQL 141 Self-joins are important for a table in which one column references another. A com- mon use is to represent a tree structure in the table Here’s how you might use this tech- nique to store the nodes of an XML document in a table. To simplify the example, we have ignored attributes, processing instructions, and text nodes. Your table might look similar to Table 8.7. To get the names of the child and its parent, you can execute the following self-join. It will not select the root node, since the root node has no parent listed in the table. SELECT child.name, parent.name FROM xml_test child,xml_test parent WHERE child.parent_id=node.node_id Subqueries as Targets When you use an object as a target, all the data in the object is a candidate to be returned. Oracle also gives you the ability to use queries as targets. A query is just a select statement, most likely with a limiting where condition. Here is an example in which you use a query as target so that you return employees with salaries greater than 1,300. SELECT ename,dname FROM dept, (SELECT * FROM emp WHERE sal > 1300) filtered_emp WHERE filtered_emp.deptno=dept.deptno; Often, a subquery isn’t necessary. As you’ll see when you read about the where clause, this result set can be produced more simply with a where condition. Alterna- tively, if you do a query on this subquery a lot, you can create a view. Table 8.7 XML Structure in a Database Table NODE_ID PARENT NAME 1 0 Root 2 1 Daughter1 3 2 GrandDaughter1 4 2 GrandDaughter2 5 1 Daughter2 142 Chapter 8 Elements Clause The elements clause describes what you want from your targets. It contains one or more expressions, which are typically column names. However, they can be any valid Oracle expression, including functions. You can alias the expressions so that the field is listed dif- ferently in the output. Doing so is very important in terms of XSQL, so we’ll pay special attention to it. In many cases, XSQL requires expressions to be aliased. Also covered here is the DISTINCT keyword. It allows you to get only the unique rows in the result set. Element Expressions An expression, in terms of the elements clause, is any one of the following: ■■ An unambiguous column name of any object listed in the target clause ■■ A function ■■ A number or quoted string It’s most important to note that the column name is unambiguous. In many cases, the same column, such as deptno, will exist multiple times in the tables that you are using. In such a case, you have to declare which column you mean. You do so by pre- fixing the object name onto the column—emp.deptno or dept.deptno. Here is an example that uses each of the different types of expressions: SELECT ‘howdy’ AS “howdy”,to_char(hiredate,’yyyy’),5,ename FROM emp By default, only the column names can possibly be valid XML names. You must use aliasing for other types of expressions, which is covered in detail in the “Expression Aliasing” section. Here is how to make our example work in XSQL: <?xml version=”1.0”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql”> <xsql:query> SELECT ename, ‘howdy’ AS “howdy”, 1 AS “number”, TO_CHAR(hiredate) AS “hiredate” FROM emp </xsql:query> </page> The result of this query is shown in Figure 8.1. The usefulness of string and number expressions in XSQL is a bit dubious, but it is a good technique to know. For one thing, it automatically creates an XML element for you. If you need to create XML elements for use with action handlers, you can use this technique to have the element ready to go. Then, all you have to do is set the value and the attributes. As you’ll also see when you learn about the special dual table, you can also use this technique to grab parameter names and put them into an XML element of your choosing. Oracle SQL 143 Figure 8.1 SELECT statement with all expression types. Expression Aliasing You’ve already seen a lot of basic elements clauses. You can use the wildcard charac- ter to fetch all the fields for each row, or you can name each row explicitly. Here are two SQL statements that produce the same result: SELECT * FROM dept SELECT deptno,dname,loc FROM dept; As you know from the last chapter, the XML elements for each piece of data take the name of the column. You can alter the name of the column by using aliasing. For instance, you might want the element names Department_Number, Department_Name, and Location in your XML. You would alias these names as fol- lows by using the AS keyword: <?xml version=”1.0”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql” > <xsql:query> SELECT 144 Chapter 8 deptno AS Department_Number, dname AS Department_Name, loc AS Location FROM dept </xsql:query> </page> Your results will come back as shown in Figure 8.2. But wait! All the columns are uppercase, following from the simple fact that SQL is case-insensitive. The fix is to use quotation marks: <?xml version=”1.0”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql” > <xsql:query> SELECT deptno AS “Department_Number”, dname AS “Department_Name”, loc AS “Location” FROM dept </xsql:query> </page> Figure 8.2 Aliasing. Oracle SQL 145 Figure 8.3 Case-sensitive aliasing. This will yield the desired result, as seen in Figure 8.3. In conjunction with XSQL, aliasing is often required. When functions are used, the default expression name isn’t a valid XML element name. Consider the following example where you want to know in which year each employee was hired: <?xml version=”1.0”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql” > <xsql:query> SELECT ename,to_char(HIREDATE,’yyyy’) FROM emp </xsql:query> </page> If you try to load this page in your browser, you will get an error. However, if you run it in SQL*Plus, the query runs fine. The problem is that parentheses and single quotes aren’t allowed in XML names. Thus, you have to alias the second expression to something valid, as follows: <?xml version=”1.0”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql” > <xsql:query> SELECT ename AS “Employee Name”, to char(HIREDATE,’yyyy’) AS “Hire Date” FROM emp 146 Chapter 8 </xsql:query> </page> Then you’ll get the desired result, as shown in Figure 8.4. The obvious corollary to this discussion is that you can’t choose an alias that isn’t a valid XML name. The most common instance in which this issue arises is an alias with spaces. XSQL won’t accept such queries because the column name violates the rules of XML element names. Distinct Keyword Sometimes, you want to know only what the unique members of a set are. For instance, you might want to know only those years in which any hiring was done. With what you know so far, you would do the following: SELECT to_char(HIREDATE,’yyyy’) AS “Hire_Date” FROM emp But then you’d have to go through all of the rows and eliminate those entries that are repeats. Instead, you just use the DISTINCT keyword. It does this work for you. Here’s how it works: SELECT DISTINCT to_char(HIREDATE,’yyyy’) AS “Hire_Date” FROM emp You can also use the UNIQUE keyword to do this—they are synonyms. Figure 8.4 Aliasing SQL functions. Oracle SQL 147 Where Clause The real work of any select statement is done in the where clause. In fact, you couldn’t even get through our discussions of the other two clauses without using the where clause a couple of times! Even a fairly modest production database has thousands, if not millions, of pieces of data. The rest of the select statement is used to get the data, while the where clause is the tool to filter it. First, you’ll look at how the where clause works. Next, you’ll examine joins, which are among the key components of the RDBMS architecture, more deeply. Finally, you’ll see some complex where clauses. How the Where Clause Works The where clause is a condition expression that evaluates either true, false, or NULL. If the expression evaluates to true, the row will be returned. In form, the expression is mostly identical to the Boolean conditions of any programming language. However the NULL evaluation is often tricky. You’ll look at this closely when you reach the dis- cussion of the NOT operator in the next section. In most cases, NULL acts like false, but it is important to understand the distinction. You can think of the where clause as a test that is applied to each row returned by the target clause. If the row passes, it will be included in the result set; if it doesn’t, it will be excluded. Here’s a simple example of this idea: SELECT ename FROM emp WHERE sal < 1500 ORDER BY ename Table 8.8 Simple Where Clause ename sal sal < 1500? ADAMS 1100 TRUE ALLEN 1600 FALSE BLAKE 2850 FALSE CLARK 2450 FALSE FORD 3000 FALSE JAMES 950 TRUE JONES 2975 FALSE KING 5000 FALSE 148 Chapter 8 Table 8.8 Simple Where Clause (Continued) ename sal sal < 1500? MARTIN 1250 FALSE MILLER 1300 TRUE SCOTT 3000 FALSE SMITH 800 TRUE TURNER 1500 FALSE WARD 1250 TRUE You can consider the where clause filtering as listed in Table 8.8. The six rows selected are the six for which the expression evaluates to true. The where clause doesn’t limit you to just one test. You can combine tests with boolean operators. Con- sider this query in which the salary test is set differently for different departments: SELECT ename FROM emp WHERE (deptno=10 AND sal < 1500) OR (deptno=20 AND sal < 2000) OR (deptno=40 AND sal < 3000) ORDER BY ename This yields the following matrix, as listed in Table 8.9: Table 8.9 Salary Range Based on Departments deptno=10 and sal<1500?, deptno=20 and sal<2000?, ename deptno sal deptno=40 and sal<3000? ADAMS 20 1100 FALSE, TRUE, FALSE ALLEN 30 1600 FALSE, FALSE, FALSE BLAKE 30 2850 FALSE, FALSE, FALSE CLARK 10 2450 FALSE, FALSE, FALSE FORD 20 3000 FALSE, FALSE, FALSE JAMES 30 950 FALSE, FALSE, FALSE (continues) Oracle SQL 149 [...]... comm IS NOT NULL Oracle SQL String-Matching Operators It is possible to do wildcard matching in SQL The like and not-like operators provide this functionality Wildcard matching differs from the text searching made possible with Oracle Text, which you’ll learn about later While not as powerful, the like and not-like operators work against any column and don’t carry the overhead of Oracle Text Both operators... of them have commissions greater than 500 SELECT ename, deptno, WHERE ( (deptno=10 and sal < OR (deptno=20 and sal < OR (deptno=40 and sal < ) OR comm . xmlns:xsql=”urn :oracle- xsql” > <xsql:query> SELECT deptno AS “Department_Number”, dname AS “Department_Name”, loc AS “Location” FROM dept </xsql:query> </page> Figure 8.2 Aliasing. Oracle. made possible with Oracle Text, which you’ll learn about later. While not as powerful, the like and not-like operators work against any column and don’t carry the overhead of Oracle Text. Both. contains one or more expressions, which are typically column names. However, they can be any valid Oracle expression, including functions. You can alias the expressions so that the field is listed

Ngày đăng: 03/07/2014, 08:20

Mục lục

    Oracle. XSQL Combining SQL, Oracle Text,XSLT, and Java to Publish Dynamic Web Content

    Chapter 1 Introducing Oracle XSQL

    Chapter 2 Getting Started with XSQL

    Chapter 5 Writing XSQL Pages

    Chapter 7 Database Modifications with XSQL

    Chapter 10 Using Oracle Text 253

    Chapter 14 Building XSQL Web Applications

    Chapter 15 Command Line Utility 443

    Chapter 16 Web Services with XSQL

    Chapter 17 XSQL Beyond Web Browsing

Tài liệu cùng người dùng

Tài liệu liên quan