Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 10 ■ ■ ■ Database Tables In this chapter, we will discuss the various types of database tables and cover when you might want to use each type (i.e., when one type of table is more appropriate than another). We will concentrate on the physical storage characteristics of the tables: how the data is organized and stored. Once upon a time, there was only one type of table, really: a “normal” table. It was managed in the same way a “heap of stuff” is managed (the definition of which appears in the next section). Over time, Oracle added more sophisticated types of tables. Now, in addition to the heap organized table, there are clustered tables (three types of those), index organized tables, nested tables, temporary tables, and object tables. Each table type has different characteristics that make it suitable for use in different application areas. Types of Tables We will define each type of table before getting into the details. There are nine major table types in Oracle: • Heap organized tables: These are “normal,” standard database tables. Data is managed in a heap-like fashion. As data is added, the first free space found in the segment that can fit the data will be used. As data is removed from the table, it allows space to become available for reuse by subsequent INSERTs and UPDATEs. This is the origin of the name “heap” as it refers to this type of table. A heap is a bunch of space, and it is used in a somewhat random fashion. • Index organized tables: These tables are stored in an index structure. This imposes physical order on the rows themselves. Whereas in a heap the data is stuffed wherever it might fit, in index organized tables (IOTs) the data is stored in sorted order, according to the primary key. • Index clustered tables: Clusters are groups of one or more tables, physically stored on the same database blocks, with all rows that share a common cluster key value being stored physically “near” each other. Two goals are achieved in this structure. First, many tables may be stored physically joined together. Normally, you would expect data from only one table to be found on a database block, but with clustered tables, data from many tables may be stored on the same block. Second, all data that contains the same cluster key value, such as DEPTNO=10, will be physically stored together. The data is “clustered” around the cluster key value. A cluster key is built using a B*Tree index. 337
338 CHAPTER 10 ■ DATABASE TABLES • Hash clustered tables: These tables are similar to clustered tables, but instead of using a B*Tree index to locate the data by cluster key, the hash cluster hashes the key to the cluster to arrive at the database block the data should be on. In a hash cluster, the data is the index (metaphorically speaking). These tables are appropriate for data that is read frequently via an equality comparison on the key. • Sorted hash clustered tables: This table type is new in Oracle 10g and combines some aspects of a hash clustered table with those of an IOT. The concept is as follows: you have some key value that rows will be hashed by (say, CUSTOMER_ID), and then a series of records related to that key that arrive in sorted order (timestamp-based records) and are processed in that sorted order. For example, a customer places orders in your order entry system, and these orders are retrieved and processed in a first in, first out (FIFO) manner. In such as system, a sorted hash cluster may be the right data structure for you. • Nested tables: These are part of the object-relational extensions to Oracle. They are simply system-generated and -maintained child tables in a parent/child relationship. They work in much the same way as EMP and DEPT in the SCOTT schema. EMP is considered to be a child of the DEPT table, since the EMP table has a foreign key, DEPTNO, that points to DEPT. The main difference is that they are not “stand-alone” tables like EMP. • Temporary tables: These tables store scratch data for the life of a transaction or the life of a session. These tables allocate temporary extents, as needed, from the current user’s temporary tablespace. Each session will see only the extents that session allocates; it will never see any of the data created in any other session. • Object tables: These tables are created based on an object type. They have special attributes not associated with non-object tables, such as a system-generated REF (object identifier) for each row. Object tables are really special cases of heap, index organized, and temporary tables, and they may include nested tables as part of their structure as well. • External tables: These tables are not stored in the database itself; rather, they reside outside of the database in ordinary operating system files. External tables in Oracle9i and above give you the ability to query a file residing outside the database as if it were a normal table inside the database. They are most useful as a means of getting data into the database (they are a very powerful data-loading tool). Furthermore, in Oracle 10g, which introduces an external table unload capability, they provide an easy way to move data between Oracle databases without using database links. We will look at external tables in some detail in Chapter 15. Here is some general information about tables, regardless of their type: • A table can have up to 1,000 columns, although I recommend against a design that does contain the maximum number of columns, unless there is some pressing need. Tables are most efficient with far fewer than 1,000 columns. Oracle will internally store a row with more than 254 columns in separate row pieces that point to each other and must be reassembled to produce the entire row image.
- Page 331 and 332: 286 CHAPTER 9 ■ REDO AND UNDO Fir
- Page 333 and 334: 288 CHAPTER 9 ■ REDO AND UNDO The
- Page 335 and 336: 290 CHAPTER 9 ■ REDO AND UNDO We
- Page 337 and 338: 292 CHAPTER 9 ■ REDO AND UNDO Wha
- Page 339 and 340: 294 CHAPTER 9 ■ REDO AND UNDO row
- Page 341 and 342: 296 CHAPTER 9 ■ REDO AND UNDO If
- Page 343 and 344: 298 CHAPTER 9 ■ REDO AND UNDO ops
- Page 345 and 346: 300 CHAPTER 9 ■ REDO AND UNDO Inv
- Page 347 and 348: 302 CHAPTER 9 ■ REDO AND UNDO The
- Page 349 and 350: 304 CHAPTER 9 ■ REDO AND UNDO 41
- Page 351 and 352: 306 CHAPTER 9 ■ REDO AND UNDO ins
- Page 353 and 354: 308 CHAPTER 9 ■ REDO AND UNDO So,
- Page 355 and 356: 310 CHAPTER 9 ■ REDO AND UNDO ops
- Page 357 and 358: 312 CHAPTER 9 ■ REDO AND UNDO ops
- Page 359 and 360: 314 CHAPTER 9 ■ REDO AND UNDO •
- Page 361 and 362: 316 CHAPTER 9 ■ REDO AND UNDO ...
- Page 363 and 364: 318 CHAPTER 9 ■ REDO AND UNDO •
- Page 365 and 366: 320 CHAPTER 9 ■ REDO AND UNDO bac
- Page 367 and 368: 322 CHAPTER 9 ■ REDO AND UNDO As
- Page 369 and 370: 324 CHAPTER 9 ■ REDO AND UNDO ops
- Page 371 and 372: 326 CHAPTER 9 ■ REDO AND UNDO wil
- Page 373 and 374: 328 CHAPTER 9 ■ REDO AND UNDO Thi
- Page 375 and 376: 330 CHAPTER 9 ■ REDO AND UNDO ops
- Page 377 and 378: 332 CHAPTER 9 ■ REDO AND UNDO Whe
- Page 379 and 380: 334 CHAPTER 9 ■ REDO AND UNDO Tha
- Page 381: 336 CHAPTER 9 ■ REDO AND UNDO tou
- Page 385 and 386: 340 CHAPTER 10 ■ DATABASE TABLES
- Page 387 and 388: 342 CHAPTER 10 ■ DATABASE TABLES
- Page 389 and 390: 344 CHAPTER 10 ■ DATABASE TABLES
- Page 391 and 392: 346 CHAPTER 10 ■ DATABASE TABLES
- Page 393 and 394: 348 CHAPTER 10 ■ DATABASE TABLES
- Page 395 and 396: 350 CHAPTER 10 ■ DATABASE TABLES
- Page 397 and 398: 352 CHAPTER 10 ■ DATABASE TABLES
- Page 399 and 400: 354 CHAPTER 10 ■ DATABASE TABLES
- Page 401 and 402: 356 CHAPTER 10 ■ DATABASE TABLES
- Page 403 and 404: 358 CHAPTER 10 ■ DATABASE TABLES
- Page 405 and 406: 360 CHAPTER 10 ■ DATABASE TABLES
- Page 407 and 408: 362 CHAPTER 10 ■ DATABASE TABLES
- Page 409 and 410: 364 CHAPTER 10 ■ DATABASE TABLES
- Page 411 and 412: 366 CHAPTER 10 ■ DATABASE TABLES
- Page 413 and 414: 368 CHAPTER 10 ■ DATABASE TABLES
- Page 415 and 416: 370 CHAPTER 10 ■ DATABASE TABLES
- Page 417 and 418: 372 CHAPTER 10 ■ DATABASE TABLES
- Page 419 and 420: 374 CHAPTER 10 ■ DATABASE TABLES
- Page 421 and 422: 376 CHAPTER 10 ■ DATABASE TABLES
- Page 423 and 424: 378 CHAPTER 10 ■ DATABASE TABLES
- Page 425 and 426: 380 CHAPTER 10 ■ DATABASE TABLES
- Page 427 and 428: 382 CHAPTER 10 ■ DATABASE TABLES
- Page 429 and 430: 384 CHAPTER 10 ■ DATABASE TABLES
- Page 431 and 432: 386 CHAPTER 10 ■ DATABASE TABLES
CHAPTER 10<br />
■ ■ ■<br />
<strong>Database</strong> Tables<br />
In this chapter, we will discuss the various types of database tables <strong>and</strong> cover when you might<br />
want to use each type (i.e., when one type of table is more appropriate than another). We will<br />
concentrate on the physical storage characteristics of the tables: how the data is organized<br />
<strong>and</strong> stored.<br />
Once upon a time, there was only one type of table, really: a “normal” table. It was managed<br />
in the same way a “heap of stuff” is managed (the definition of which appears in the next<br />
section). Over time, <strong>Oracle</strong> added more sophisticated types of tables. Now, in addition to the<br />
heap organized table, there are clustered tables (three types of those), index organized tables,<br />
nested tables, temporary tables, <strong>and</strong> object tables. Each table type has different characteristics<br />
that make it suitable for use in different application areas.<br />
Types of Tables<br />
We will define each type of table before getting into the details. There are nine major table<br />
types in <strong>Oracle</strong>:<br />
• Heap organized tables: These are “normal,” st<strong>and</strong>ard database tables. Data is managed<br />
in a heap-like fashion. As data is added, the first free space found in the segment that<br />
can fit the data will be used. As data is removed from the table, it allows space to become<br />
available for reuse by subsequent INSERTs <strong>and</strong> UPDATEs. This is the origin of the name<br />
“heap” as it refers to this type of table. A heap is a bunch of space, <strong>and</strong> it is used in a<br />
somewhat r<strong>and</strong>om fashion.<br />
• Index organized tables: These tables are stored in an index structure. This imposes<br />
physical order on the rows themselves. Whereas in a heap the data is stuffed wherever<br />
it might fit, in index organized tables (IOTs) the data is stored in sorted order, according<br />
to the primary key.<br />
• Index clustered tables: Clusters are groups of one or more tables, physically stored on<br />
the same database blocks, with all rows that share a common cluster key value being<br />
stored physically “near” each other. Two goals are achieved in this structure. First, many<br />
tables may be stored physically joined together. Normally, you would expect data from<br />
only one table to be found on a database block, but with clustered tables, data from<br />
many tables may be stored on the same block. Second, all data that contains the same<br />
cluster key value, such as DEPTNO=10, will be physically stored together. The data is<br />
“clustered” around the cluster key value. A cluster key is built using a B*Tree index.<br />
337