Thursday, April 19, 2007



The Database Block (dbblock) has the following structure:
Header - The header contains general information about the data; i.e. block address and type of segments (table, index, etc.). The header also contains information about the table and the actual row (address) that holds the data.
Free Space - Space allocated for future update/insert operations. Generally affected by the values of PCTFREE and PCTUSED parameters.
Data - Actual row data.While creating/altering any table/index, Oracle used two storage parameters for space control.
PCTFREE: The percentage of space reserved for future update of existing data.
PCTUSED: The percentage of minimum space used for insertion of new row data. This value determines when the block gets back into Free List table.

PCTFREE & PCTUSED

PCTFREE
specifies the percentage of space in each of the table's data blocks
reserved for future updates to the table's rows. The value of
PCTFREE must be a positive integer from 1 to 99. A value of 0
allows the entire block to be filled by inserts of new rows. The
default value is 10. This value reserves 10% of each block for
updates to existing rows and allows inserts of new rows to fill a
maximum of 90% of each block.

PCTFREE has the same function in the commands that create and alter
clusters, indexes, snapshots, and snapshot logs. The combination of
PCTFREE and PCTUSED determines whether inserted rows will go into
existing data blocks or into new blocks.

PCTUSED
specifies the minimum percentage of used space that Oracle maintains
for each data block of the table. A block becomes a candidate for
row insertion when its used space falls below PCTUSED. PCTUSED is
specified as a positive integer from 1 to 99 and defaults to 40.

PCTUSED has the same function in the commands that create and alter
clusters, snapshots, and snapshot logs.

The sum of PCTFREE and PCTUSED must be less than 100. You can use
PCTFREE and PCTUSED together use space within a table more
efficiently.

Wednesday, April 18, 2007

-- Query to delete the duplicate rows in a table (without primary key)
DELETE FROM empWHERE ROWID NOT IN
(SELECT MIN(ROWID) FROM emp GROUP BY A, B)

Delete from empwhere rowid in (select idfrom (select rowid id,Row_number()
over(partition by A, B order by SAL asc) top1FROM emp)where top1 > 1)
-- Different Ranking functions usage
select rowid,Row_number() over(partition by A,B order by SAL asc) top1 FROM emp
select e.* ,rowid,DENSE_RANK() over(partition by A,B order by SAL asc) top1 FROM emp e
select e.*,rowid,RANK()over ( partition by A,B order by sal asc ) top1 FROM emp e
select e.*,rowid,CUME_DIST()over ( partition by A,B order by sal asc ) top1 FROM emp e
select e.*,rowid,PERCENT_RANK()over ( partition by A,B order by sal asc ) top1 FROM emp e
select e.*,rowid,NTILE(2)over ( partition by A,B order by sal asc ) top1 FROM emp e