Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab4-SELECT statement basic features.doc
Скачиваний:
22
Добавлен:
20.02.2016
Размер:
347.14 Кб
Скачать
      1. Set Operators

Set operators combine the results of two component queries into a single result. Queries containing set operators are called compound queries. Table lists SQL set operators.

Operator

Purpose

Example

UNION

Returns all rows selected by either query. Duplicate rows are deleted.

SELECT Building FROM DEPARTMENT

UNION

SELECT Building FROM FACULTY;

UNION ALL

Returns all rows selected by either query, including all duplicates.

SELECT Building FROM DEPARTMENT

UNION ALL

SELECT Building FROM ROOM;

INTERSECT

Returns all distinct rows selected by both queries.

SELECT Building FROM FACULTY

INTERSECT

SELECT Building FROM ROOM;

MINUS

Returns all distinct rows selected by the first query but not the second.

SELECT Building FROM ROOM

MINUS

SELECT Building FROM FACULTY;

All set operators have equal precedence. If a SQL statement contains multiple set operators, Oracle evaluates them from the left to right if no parentheses explicitly specify another order.

The corresponding expressions in the select lists of the component queries of a compound query must match in number and datatype. If component queries select character data, the datatype of the return values are determined as follows:

  • If both queries select values of datatype CHAR, the returned values have datatypeCHAR.

  • If either or both of the queries select values of datatype VARCHAR2, the returned values have datatypeVARCHAR2.

    1. Appendix b. Expressions

An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. An expression generally assumes the datatype of its components.

The expression “2*2” evaluates to 4 and has datatype NUMBER (the same datatype as its components).

The following expression is an example of a more complex expression that uses both functions and operators. The expression adds seven days to the current date, removes the time component from the sum, and converts the result to CHAR datatype:

TO_CHAR(TRUNC(SYSDATE+7))

You can use expressions in:

  • The select list of the SELECTstatement

  • A condition of the WHEREclause andHAVINGclause

  • The CONNECT BY andORDER BYclauses

  • The VALUESclause of theINSERTstatement

  • The SETclause of theUPDATEstatement

For example, you could use an expression in place of the quoted string 'smith' in this UPDATE statement SET clause:

SET ename = 'smith';

This SET clause has the expression LOWER(ename) instead of the quoted string 'smith':

SET ename = LOWER(ename);

Expressions have several forms, as shown in the following syntax:

expr::=

Oracle does not accept all forms of expressions in all parts of all SQL statements. You must use appropriate expression notation whenever expr appears in conditions, SQL functions, or SQL statements in other parts of SQL.

      1. Simple Expressions

A simple expression specifies column, constant or NULL.

simple_expression::=

Some valid simple expressions are:

emp.ename

'this is a text string'

10

      1. Compound Expressions

A compound expression specifies a combination of other expressions.

compound_expression::=

Note that some combinations of functions are inappropriate and are rejected. For example, the LENGTH function is inappropriate within an aggregate function.

Some valid compound expressions are:

('CLARK' || 'SMITH')

LENGTH('MOOSE') * 57

SQRT(144) + 72

my_fun(TO_CHAR(sysdate,'DD-MMM-YY')

      1. Variable Expressions

This type of expressions are not discussed here.

      1. Built-In Function Expressions

A built-in function expression specifies a call to a single-row SQL function.

built_in_function_expression::=

Some valid built-in function expressions are:

LENGTH('BLAKE')

ROUND(1234.567*43)

SYSDATE

For information on built-in functions, see Appendix of Lab 5.

      1. User-Defined Function Expressions

A user-defined function expression specifies a call to a user-defined function. Syntax of such calls are not discussed here.

      1. Type Constructor Expressions

A type constructor expression specifies a call to a type constructor. The argument to the type constructor is any expression or subquery. Syntax are not discussed here.

      1. CAST Expressions

A CAST expression converts one built-in datatype or collection-typed value into another built-in datatype or collection-typed value.

CAST_expression::=

CAST allows you to convert built-in datatypes or collection-typed values of one type into another built-in datatype or collection type. You can cast an unnamed operand (such as a date or the result set of a subquery) or a named collection (such as a varray or a nested table) into a type-compatible datatype or named collection. The type_name must be the name of a built-in datatype or collection type and the operand must be a built-in datatype or must evaluate to a collection value.

For the operand, expr can be either a built-in datatype or a collection type, and subquery must return a single value of collection type or built-in type. MULTISET informs Oracle to take the result set of the subquery and return a collection value.

To cast a named collection type into another named collection type, the elements of both collections must be of the same type.

If the result set of subquery can evaluate to multiple rows, you must specify the MULTISET keyword. The rows resulting from the subquery form the elements of the collection value into which they are cast. Without the MULTISET keyword, the subquery is treated as a scalar subquery, which is not supported in the CAST expression. In other words, scalar subqueries as arguments of the CAST operator are not valid in Oracle8i.

Examples:

SELECT CAST ('1997-10-22' AS DATE) FROM DUAL;

SELECT * FROM t1 WHERE CAST (ROWID AS VARCHAR2) = '01234';

Соседние файлы в папке English