Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

12.2 The raw Datatype

The RAW datatype allows you to store and manipulate relatively small amounts of binary data. Unlike the case with VARCHAR2 and other character types, RAW data never undergoes any kind of character set conversion when traveling back and forth between your PL/SQL programs and the database. RAW variables are declared as follows:

variable_name RAW(maximum_size)

The value for maximum_size may range from 1 through 32767. Be aware that while a RAW PL/SQL variable can hold up to 32,767 bytes of data, a RAW database column can hold only 2000 bytes.

RAW is not a type that we use or encounter very often. It's useful mainly when you need to deal with small amounts of binary data. When dealing with the large amounts of binary data found in images, sound files, and the like, you should look into using the BLOB (binary large object) type. BLOB is described later in this chapter (see Section 12.4.2).

12.3 The urowid and rowid Datatypes

The UROWID and ROWID types allow you to work with database rowids in your PL/SQL programs. A rowid is a row identifier—a binary value that identifies a row of data in an Oracle table. Referencing rowids in UPDATE and DELETE statements can sometimes lead to desirable improvements in processing speed, as access by rowid is typically the fastest way to locate or retrieve a particular row in the database—faster even than a search by primary key. Figure 12-1 contrasts the use of a rowid in an UPDATE statement with the use of column values such as those for a primary key.

Figure 12-1. RowiDs take you directly to rows in a table

In the history of Oracle, the ROWID type came first. As Oracle added functionality such as index-organized tables (IOTs) and gateways to other types of databases, Oracle developed new types of rowids and hence had to develop a new datatype capable of holding them. Enter the UROWID datatype. The U in UROWID stands forUniversal, and a UROWID variable can contain any type of ROWID from any type of table.

We recommend the use of UROWID for all new development involving rowids. The ROWID type provides backward compatibility, but can't accommodate all types of rowids now encountered in an Oracle database. UROWID is safer because it accommodates any type of rowid.

12.3.1 Getting at Rowids

In the Oracle RDBMS, ROWID is a pseudocolumn that is a part of every table you create. The rowid is an internally generated and maintained binary value that identifies a row of data in your table. ROWID is called a pseudocolumn because a SQL statement may include it in places where you would normally use a column, but it is not an actual column that you create for the table. Instead, the RDBMS generates the rowid for each row as it is inserted into the database, and returns that value to you via the ROWID pseudocolumn. You cannot change the value of a rowid.

You can use the UROWID datatype to store rowids from the database in your PL/SQL program. You canSELECT or FETCH the rowid for a row into a ROWID variable; for example:

DECLARE

employee_rowid UROWID;

employee_salary NUMBER;

BEGIN

--Retrieve employee information that we might want to modify

SELECT rowid, salary INTO employee_rowid, employee_salary

FROM employee

WHERE last_name='Grubbs' AND first_name='John';

END;

You may find yourself wondering just what a rowid looks like, or what kind of data a UROWID value really contains. Unless you're a DBA, there's probably no reason to care. In fact, we're fairly certain that unless you are writing some kind of database administration utility, you shouldn't write programs that are concerned with the contents of rowids. However, if you are curious, you can SELECT rowids from SQL*Plus. Following are some rowids from an index-organized table:

SQL> SELECT rowid FROM employee;

ROWID

----------------------------------

*BAIACiICwQL+

*BAIACiICwQP+

and following are some from a regular table:

SQL> SELECT rowid FROM rtest;

ROWID

------------------

AAAH2pAAIAAAAoSAAA

AAAH2pAAIAAAAoSAAB

If you're using Oracle7, you'll see different results.

If you need to work with rowids at a detailed level, extracting the individual component values that make up a rowid, you will want to use the built-in package, DBMS_ROWID. You can read about DBMS_ROWID in Oracle Built-in Packages (O'Reilly) or in the Oracle manual Supplied PL/SQL Packages and Types Reference. In general, and certainly for uses such as we describe in this chapter, you should treat rowids as opaque values, meaning that you shouldn't worry about looking at and understanding the actual data they contain.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]