About ‘ROWNUM’ in Oracle


ROWNUM is an Oracle pseudo column which numbers the rows in a result set.

SELECT rownum, table_name
FROM user_tables;

ROWNUM TABLE_NAME
————- —————–
1 EMP
2 DEPT
3 BONUS
4 SALGRADE
5 DUMMY

5 rows selected

Here is a summary of how ROWNUM can be used.

Limiting Rows

ROWNUM can be used to limit the number of rows returned by a query in a similar way to LIMIT in Postgres and MySql, TOP in SQL Server and FETCH FIRST in DB2.

SELECT rownum, table_name
FROM user_tables
WHERE rownum <=3;

ROWNUM TABLE_NAME
————- —————–
1 EMP
2 DEPT
3 BONUS

3 rows selected

ROWNUM with DML

The use of ROWNUM is not restricted to select statements. It can be used with DML statements that update the database too.

CREATE TABLE o AS
SELECT *
FROM all_objects
WHERE rownum <= 1000;

Table created

UPDATE o
SET object_id = rownum,
created = created + INTERVAL '1' MINUTE * rownum
WHERE rownum 2;

RNUM TABLE_NAME
——– —————-
3 SALGRADE
4 DUMMY
5 DEPT

3 rows selected

You will notice that an inline view has been introduced to transform the ROWNUM pseudo column into a ‘real’ column before we do the comparison.

It is tempting to write the above SQL as follows.

SELECT table_name
FROM user_tables
WHERE rownum > 2;

TABLE_NAME
——————————

0 rows selected

However, this query will always return zero rows, regardless of the number of rows in the table.

To explain this behaviour, we need to understand how Oracle processes ROWNUM. When assigning ROWNUM to a row, Oracle starts at 1 and only only increments the value when a row is selected; that is, when all conditions in the WHERE clause are met. Since our condition requires that ROWNUM is greater than 2, no rows are selected and ROWNUM is never incremented beyond 1.

The bottom line is that conditions such as the following will work as expected.

.. WHERE rownum = 1;

.. WHERE rownum 10;

Top-n Query

Typically, a top-n query sorts data into the required sequence and then limits the output to a subset of rows.

For example, suppose we wish to retrieve the top three earners from our employee table.

SELECT ename, sal
FROM (
SELECT ename, sal
FROM emp
ORDER BY sal DESC)
WHERE rownum <=3;

ENAME SAL
———- ———
KING 5000
SCOTT 3000
FORD 3000

3 rows selected

The inline view (the inner select) sorts the rows and passes the result up to the outer select. The outer select then limits the output to three rows.

It may seem more natural to use the following SQL.

SELECT ename, sal
FROM emp
WHERE rownum <=3
ORDER BY sal DESC;

ENAME SAL
———- ———————-
ALLEN 1600
WARD 1250
SMITH 800

3 rows selected

However, this does not give us the result we want because Oracle assigns the ROWNUM values to the rows before it does the sort.

In this example, Oracle will retrieve three rows from the table, any three rows, and sort only these three rows. We really need Oracle to sort all the rows and then return the first three. The inline view will ensure that this will happen.

Sort Performance

Limiting rows on a sorted result set using ROWNUM can also provide an added performance benefit. Rather than physically sorting all the rows to retrieve just the top few, Oracle maintains an array which contains just the highest or the lowest values (depending on whether we specified ASC or DESC in the ORDER BY clause). The size of the array will be the number of rows we wish to return. As rows are processed, only the highest (or lowest) values are retained in the array. All other rows are discarded.

Pagination

Next, we will see how ROWNUM is used to select a range of rows from within a result set. This is useful if we are to provide pagination on a web screen, for example.

Suppose we are paging through the employee table in name order and we wish to display rows six to ten inclusive.

SELECT rnum, ename, job
FROM
(SELECT /*+ FIRST_ROWS(10) */ rownum rnum, ename, job
FROM
(SELECT ename, job
FROM emp
ORDER BY ename)
WHERE rownum 5;

RNUM ENAME JOB
——– ———- ———
6 JAMES CLERK
7 JONES MANAGER
8 KING PRESIDENT
9 MARTIN SALESMAN
10 MILLER CLERK

5 rows selected

We use nested inline views to retrieve and sort the data and then apply the range check using ROWNUM. We have split the upper and lower bound check, which allows Oracle to use COUNT(STOPKEY) in the execution plan when checking for ROWNUM <= 10. This is a performance optimization which, along with the sorting optimization described earlier, will ensure that our query runs efficiently as the table grows.

The FIRST_ROWS(n) hint also tells Oracle to optimize the query so that the first n rows are returned as quickly as possible.

Summary

ROWNUM provides a mechanism for returning a subset or range of rows from a query. It can be misleading at first if not properly understood but, once mastered, is invaluable for limiting result set output for pagination and top-n style queries.

For more information on ROWNUM, see Tom Kytes article on OTN.

For more information on Oracle, visit level up.

Get the width and height of an image using a function in PHP


This is a very useful function in PHP. PHP provides function to get the width and height of an image. The
getimagesize() function will determine the size of image file including flash file(swf).

Syntax

list($width, $height, $type, $attr) = getimagesize(“image_name.jpg”);

Code

This is a sample code, change image_name to your image name and test it!

<?php

list($width, $height, $type, $attr) = getimagesize(“image_name.jpg”);

echo “Image width ” .$width;
echo “<BR>”;
echo “Image height ” .$height;
echo “<BR>”;
echo “Image type ” .$type;
echo “<BR>”;
echo “Attribute ” .$attr;

?>

When you run this script you will see the result like this

Image width 379
Image height 344
Image type 2
Image attribute width=”379″ height=”344″

You will get the width, height, type of an image and also attribute of an image, I use this function in my image

upload form.

Type of an image you can see from table below You can read a full manual here

Type of the image
1 = GIF      5 = PSD          9 = JPC     13 = SWC
2 = JPG     6 = BMP     10 = JP2     14 = IFF
3 = PNG   7 = TIFF(intel byte order)               11 = JPX     15 = WBMP
4 = SWF   8 = TIFF(motorola byte order)     12 = JB2     16 = XBM

Reset Webmin Password for ubuntu


I found this problem at the first time when webmin installed. Also this happens to everyone at one time or another. You go on vacation and when you come back, you forget all of the passwords on your computer.

I failed logging in several times and then Webmin blocked my IP. Getting localhost blocked isn’t a good thing to see. So I searched the Internet for a way to reset the password and I found this procedure:

1. Login to your computer as root. Type on browser address bar https://localhost:10000/
2. If you are running a Debian distribution (ubuntu), enter the following command:
/usr/share/webmin/changepass.pl /etc/webmin username password
3. Login to Webmin with your reset password.
4. eNjoY!

How to Get the Current Page URL


Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. Another example maybe a page URL has Querystring info appended to it and you need to send an email off to someone with that same exact URL and Querystring information. There are plenty of other reasons as well. Here is how you can do that.

Add the following code to a page:

<?php
function selfURL() {
$s = empty($_SERVER[“HTTPS”]) ? ” : ($_SERVER[“HTTPS”] == “on”) ? “s” : “”;
$protocol = strleft(strtolower($_SERVER[“SERVER_PROTOCOL”]), “/”).$s;
$port = ($_SERVER[“SERVER_PORT”] == “80”) ? “” : (“:”.$_SERVER[“SERVER_PORT”]);
return $protocol.”://”.$_SERVER[‘SERVER_NAME’].$port.$_SERVER[‘REQUEST_URI’];
}

function strleft($s1, $s2) {
$values = substr($s1, 0, strpos($s1, $s2));
return  $values;
}
?>
You can now get the current page URL using the line:
<?php
print $currenturl = selfURL();