Akdora’s Blog

Programming, Oracle, Life, Fun

Google App Engine Example August 13, 2009

Filed under: Non-technical — Akdora @ 5:24 am
Tags: , , ,

Chech this out:
http://www.jtpd.org/public/TheStory.jsf?storyId=165&title=adim-adim-google-app-engine-java-web-uygulamasi

Advertisements
 

Non-Predefined Exception Example February 5, 2009

Filed under: PL/SQL — Akdora @ 11:47 am
Tags: , , , ,

One of my friend asked me following question.

“I want to drop two columns of a table. So I wrote a basic drop command.

alter table t_test drop (col_name1, col_name2)

However, If this command is called more than once. I do not want an error message. What can I do for this?”

It’s a strange request, but this will learn us how to write a non-predefined  exception to solve this kind of requests. We can handle ORA-XXXXX exception as we want. We name them this numbered exceptions and handle them.  I will write a post about exception types as soon as possible. 

 When we try to drop a column that does not exists on the table. We get following error: ORA00904: invalid column name. It’s error number “-904”. We will use this number to define the exception.

(more…)

 

A Function Based Example August 10, 2007

Filed under: Oracle — Akdora @ 4:41 pm
Tags: , , , ,

SQL> set autotrace traceonly
SQL> SET serveroutput ON

–let’s create a table that contains country names and codes from apex environment
SQL> CREATE TABLE t_func AS select * from flows_020100.wwv_flow_countries t;

Table created.

–then write a query that searches brazil 🙂
SQL> SELECT * FROM t_func WHERE upper(country_name) LIKE ‘BRAZIL’;
Execution Plan
———————————————————-                     
Plan hash value: 175459743                                                     
                                                                               
—————————————————————————-   
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |   
—————————————————————————-   
|   0 | SELECT STATEMENT  |        |     1 |   146 |     2   (0)| 00:00:01 |   
|*  1 |  TABLE ACCESS FULL| T_FUNC |     1 |   146 |     2   (0)| 00:00:01 |   
—————————————————————————-   
                                                                               
Predicate Information (identified by operation id):                            
—————————————————                            
                                                                               
   1 – filter(UPPER(“COUNTRY_NAME”) LIKE ‘BRAZIL’)                             
                                                                               
Note                                                                           
—–                                                                          
   – dynamic sampling used for this statement                                  
Statistics
———————————————————-                     
         48  recursive calls                                                   
          0  db block gets                                                     
         12  consistent gets                                                   
          1  physical reads                                                    
          0  redo size                                                         
        482  bytes sent via SQL*Net to client                                  
        384  bytes received via SQL*Net from client                            
          2  SQL*Net roundtrips to/from client                                 
          0  sorts (memory)                                                    
          0  sorts (disk)                                                      
          1  rows processed  

–then let’s create an function based index on column country_name                                                  

SQL> CREATE INDEX t_func_idx ON t_func(upper(country_name));

Index created.

–write the query again

SQL> SELECT * FROM t_func WHERE upper(country_name) LIKE ‘BRAZIL’;
Execution Plan
———————————————————-                     
Plan hash value: 3533916538                                                    
                                                                               
——————————————————————————————                                                                     
                                                                               
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)|
Time     |                                                                     
                                                                               
——————————————————————————————                                                                     
                                                                               
|   0 | SELECT STATEMENT         |            |     1 |   146 |     1   (0)| 00:00:01                                                                                                                                            
|   1 |  TABLE ACCESS BY INDEX ROWID| T_FUNC     |     1 |   146 |     1   (0)| 00:00:01 |                                                                     
                                                                               
|*  2 |   INDEX RANGE SCAN          | T_FUNC_IDX |     1 |       |     1   (0)| 00:00:01 |                                                                     
                                                                               
——————————————————————————————                                                                     
                                                                               
                                                                               
Predicate Information (identified by operation id):                            
—————————————————                            
                                                                               
   2 – access(UPPER(“COUNTRY_NAME”) LIKE ‘BRAZIL’)                             
                                                                               
Note                                                                           
—–                                                                          
   – dynamic sampling used for this statement                                  
Statistics
———————————————————-                     
         28  recursive calls                                                   
          0  db block gets                                                     
          9  consistent gets                                                   
          0  physical reads                                                    
          0  redo size                                                         
        482  bytes sent via SQL*Net to client                                  
        384  bytes received via SQL*Net from client                            
          2  SQL*Net roundtrips to/from client                                 
          0  sorts (memory)                                                    
          0  sorts (disk)                                                      
          1  rows processed                                                    

SQL> spool off

In the first example, query made a FULL TABLE SCAN and cost was 2.

In the second example, our index worked and it made an INDEX RANGE SCAN by using T_FUNC_IDX and cost was 1. These tables that we query on are small tables. The difference will be increase in larger tables…

tested in Oracle XE Edition 10g Release 2 (10.2)

And Code :
SET serveroutput ON
set autotrace traceonly
alter session set QUERY_REWRITE_ENABLED=TRUE;
alter session set QUERY_REWRITE_INTEGRITY=TRUSTED;
drop table t_func;
drop index t_func_idx;
CREATE TABLE t_func AS select * from flows_020100.wwv_flow_countries t;
SELECT * FROM t_func WHERE upper(country_name) LIKE ‘BRAZIL’;
CREATE INDEX t_func_idx ON t_func(upper(country_name));
SELECT * FROM t_func WHERE upper(country_name) LIKE ‘BRAZIL’;

A advised and different perspective to function based index is 🙂

http://oracle-unix.blogspot.com/2007/07/performance-tuning-how-to-make-like.html

 

Dijkstra’s Algorithm Example in C January 10, 2007

Filed under: C & C++ — Akdora @ 5:56 pm
Tags: , , , ,

In this assignment, you will help a transportation company. There will be a main office of the company and a number of branch offices. The main office wants to deliver some goods to other offices, so for each branch, a truck goes out from main office. Company knows the distances between the offices and wants to use the shortest path toward each office to reduce their costs. Your program will find the shortest paths from main office to each branch offices.

1.JPG

Your program will read the number of offices and the distance matrix from a text file as in he following sample input file.

2.JPG

First row indicates the distance from 0th office to offices 0,1,2,3,4, respectively. Second row similarly indicates the distance from 1st office to offices 0,1,2,3,4, respectively and so forth. If there is a connection from x to y with a distance of d> 0 then you should set M[x][y] = d If there is no connection, -1 is provided as an entry, then M[x][y] = -1
Main office
Everyone will use his/her university id number’s first 2 and last 3 digits as the seed for the random number generator. If your id is 990702003 then you will use 99003 as the seed. Random number generator will decide which office will be considered as the main office and your algorithm will execute accordingly.
Output File
Example: Main office is randomly determined as “0”, then following output file must be generated:

3.JPG

Solution :

input.txt
output.txt
solution in c

 

SubSet Sum Problem Example in C

Filed under: C & C++ — Akdora @ 5:32 pm
Tags: , , ,

In this assignment, you will write a program that will fill 3 boxes with fruits. Your program will get the fruits’ names, their weights and the capacities of the boxes from a file. You can get the file from the link :

input.txt

 

4.JPG

 

RULES:

  •  The program will attempt to fill these boxes with fruits one by one, starting from the first (Box1) and continuing with the next (e.g. Box2) until to the last (e.g. Box3) based on the following criteria:
    • Either a box can be filled with the exact weight (ex. Box1 should get exactly 32 kg fruits)., or it will be skipped and left empty and the next box will be tried to be filled.
    • Program will not use same fruit twice. (ex. If program have used apple for Box1, apple can’t be used again for next boxes).
  •  Program will write each boxes’ last situations to the result.txt file. If a box is full, the names of the fruits that are in that box must be written to the result.txt. If the box is empty, “Box-id: empty” must be written to the result.txt. Output file should look like as follows, note that following is sample output not the solution:

Box1: lemon, mango, …

Box2: empty

Box3: apple, …

solution in C: