Author: tom

  • Size on Oracle

    What is the size of an Oracle table? A very rough approximation is to multiply the number of rows with the average row length in bytes. Fortunately, Oracle directly provide you with such information. Take this query: select table_name , num_rows , avg_row_len ,to_char((num_rows * avg_row_len)/1000000,’9999999D999′,’NLS_NUMERIC_CHARACTERS=”. ”’) as sizeMB , blocks from dba_tab_statistics where owner…

  • Running totals in Oracle

    Oracle has a nice possibility to calculate running totals. Assume we have a sales system where transactions are stored over time. We would like to know the running total – what is sold until a certain transaction. The code to calculate this is: SELECT trans_id,to_char(trans_date,’dd-mm-yyyy hh24:mi:ss’ ) trans_date , product_code ,qty* price amount, sum(qty* price)…

  • A completely useless PL SQL programme

    Once, I wrote a completely useless programme – see below. I like to retain it as I spent quite some time on it. But it is absolutely useless. The idea it that we have some kind of a counter that is incremented by 1 if a subsequent record has the same value for a particular…

  • Ranking in Oracle

    Oracle has 3 analytical functions to rank data. They provide approximately the same results, but with little deviations. First the code: select salary, rank() over (order by salary) rank, dense_rank() over (order by salary) dense_rank, row_number() over (order by salary) row_number from hr.employees order by salary; Inspecting the results shows the deviations. Look at the…

  • Top 10 rows in Oracle

    In previous versions of Oracle, we had to retrieve a top10 rows via a sub query. In the sub query, we ordered and retrieved the data whereas in the main query, we could retrieve 10 top rows. Something like: select * from (select * from hr.employees order by salary desc) where rownum <=10; Oracle 12…

  • Oracle Aggregate and analytic functions

    We all know aggregate functions in Oracle. Let me provide a simple example to recap what the aggregate function is. Let us firse this SQL on a 107 record table: select department_id, listagg( first_name,’-‘) within group (order by hire_date) from employees group by department_id; The result is a 12 record outcome, one record for each…

  • Inserting a BLOB

    Inserting a BLOB in Oracle is slightly different from inserting from inserting a value in an Oracle database. We first have to create a location with n empty BLOB. After that the BLOB can be inserted. In a PL/SQL programme: which call be invoked by (example) exec UPLOAD_FILE_TO_DATABASE(‘TEMP’,’2019-11-15 12.46.30.mov’,’Nora’); Likewise, downloading a BLOB is done…

  • Oracle create loader file

    Creating a loader file is tedious. The syntax is complicated. Moreover, small errors lead to a rejection of records. Luckily, we have the possibility to generate them from SQL Developer. One possibility is to use the SQL Loader. This facility has an import/ export module that allows to create such files. If one would like…

  • Oracle numerical data

    Numbers can be stored in Oracle according to different formats. On one hand, we have the American way, whereby numbers are stored with a dot (.) as decimal indicator. On the other hand, we have a comma (,) as decimal indicator. Let me provide my own Oracle instance. I have the following language settings: select…

  • Copying data with data pump

    Oracle has a brilliant feature to export / import data at great speed. The trick is to use the data pump facility. Let us first introduce the export via data pump. We may export data via data pump as: CREATE TABLE stu_details_ext ORGANIZATION EXTERNAL ( TYPE ORACLE_DATAPUMP DEFAULT DIRECTORY EXTERNORA LOCATION (‘stu_details_ext_table.dmp’) ) AS SELECT…