Tuesday, November 18, 2008

How To Reset MySQL Root Password

Do you forget your MySQL root password and can't login? Don't panic. You don't have to re-install your MySQL as long as you still have the privileges to start and stop the MySQL service. Just follow these instruction:

1. Stop your MySQL service.
2. Start your MySQL service with this following command:
mysqld --skip-grant-tables

This command will start your MySQL service without checking the users tables everytime users login. So, you can login as root and reset your password.
3. Update your root password with this command.
USE mysql;
UPDATE user SET password=PASSWORD('new password') WHERE user='root';
4. Refresh the users' privileges.
FLUSH privileges;

Now, your MySQL root password has been updated with the new password. You can login to MySQL with that new password.

Tuesday, November 11, 2008

Paging In Oracle

Oracle doesn't have the LIMIT keyword like MySQL. So, how can we do paging in Oracle? There is a little trick to do it.

Let's assume there is a table named table1 and it has two fields such as trans_id and user. When we execute the SELECT statement, the result will be like this:
SELECT trans_id, user FROM table1 ORDER BY trans_id;

TRANS_ID USER
----------- ------
080003 001
080003 001
080003 001
080005 001
080005 001
080024 002
080024 002
080025 001
080151 001
080151 001
080296 001
080296 001
080413 003
080427 ADMIN

First step to do the paging is we will use that result as a table. The trick will start here. We're going to add a new field to display the row number for the result. The row number will be our key to do the paging. So, the query and its result will be like this:
SELECT rownum rn, t1.* FROM (SELECT trans_id, user FROM table1
ORDER BY trans_id) t1 ;

RN TRANS_ID USER
----- ----------- ------
1 080003 001
2 080003 001
3 080003 001
4 080005 001
5 080005 001
6 080024 002
7 080024 002
8 080025 001
9 080151 001
10 080151 001
11 080296 001
12 080296 001
13 080413 003
14 080427 ADMIN

Next step. Based on the result, we will use it as a table. Then use the rn field to do the paging. For example we're going to show only the top five of the result. So, our query will be like this :
SELECT * FROM (SELECT rownum rn, t1.* FROM (SELECT trans_id, user FROM table1
ORDER BY trans_id) t1) t2 WHERE rn BETWEEN 1 AND 5;

RN TRANS_ID USER
----- ----------- ------
1 080003 001
2 080003 001
3 080003 001
4 080005 001
5 080005 001