Featured Post

The great debacle of healthcare.gov

This is the first time in history when the president of the United States of America, or probably for any head of state around the world,...

Thursday, July 3, 2008

Oracle Tips - DML

1. Connecting through sqlplus from console

:> sqlplus user_name/user_password@database_name (fully qualified name as in tns.ora)

2. To connect to another user while already connected to some other user:

sqlplus> conn user_name/user_password@database_name (fully qualified as in tns.ora)

3. To see the currnet schema name that you're connected to:

select sys_context( 'userenv', 'current_schema' ) from dual

4. To see all the table names of the current user:

select table_name from user_tables;

5. To see the constraints of a table:

select table_name, constraint_name, constraint_type from user_constraints;

6. All table and column names of particular schema

select b.table_name, a.column_name from user_tab_columns a, all_tables b where b.owner = 'SCHEMA_NAME'
order by b.table_name

7. Convert row values into columns

Converting row to column is only possible using the below approach when you've predetermined values in the rows.

Approach-1: using case...when...then statement

select column_name1, column_name2,
max((case when column1 = 1
then value_col end)) as "user_defined_name1",
max((case when column1 = 2
then value end)) as "user_defined_name2"
From
Table_name
[group by column_name1, column_name2]


Approach-2: using left outer join:

select column_name1, column_name2,
t1.column1 as 'user-defined-name1',
t2.column1 as 'user-defined-name2'
from
table_name t1,
able_name t2
where
t1.pk = t2.pk
and t1.column1 (+) = 1
and t2.column1 (+) = 2


6. Update column

Apparently it seems harmless of the below update statement -

update table_name t set column_name =
(select column_name from another_table_name a where t.id = a.id and ....)

The problem is, each and every records would be updated even though the subquery returns few matches. The unmatched records in table_name would be updated with null. So there would be unwanted update that might not be expected. Moreover if the table_name has 10 million records, it would take humongous amount of time if you don't care about that null update.

The similar update can be achieved without getting updated with null for the unmatched records in table_name is -

update (select t.column_name, a.column_name from table_name t, another_table_name a
where t.id = a.id and ....) set t.column_name = a.column_name

The precondition is, the t.id and a.id must have unique constraints enabled or they're primary key in the corresponding tables


7. Check out if any object is currently locked out

select object_name, o.object_id from user_objects o, v$locked_object l
where o.object_id = l.object_id

8. Insert special characters like '&'

Set Define Off;
insert statement e.g. insert 'abc&d' into table_name (col1);
Set Define On;

9. See the comments on column

select table_name,column_name,comments from user_col_comments

10. Print the time difference inside a pl/sql

start_time = dbms_utlility.get_time;
// process data
end_time = dbms_utility.get_time;
dbms_output.put_line('time elapsed: ' || (end_time - start_time)/100 || ' secs');

11. SQL*PLUS options:
SQL> set timing on -- shows the elapsed time at the end
SQL>

12. Custom Order:

Use DECODE function in the order by clause instead of the column directly
e.g. Order By DECODE(column_name, 'AA', 1, 'A', 2, 'BB', 3, 'B',4, 'CC', 5, 'C', 6, 'DD', 7, 'D', 8, 'EE',, 'E',10)

13. Connect By:

TBD

14. using 'WITH':

TBD

15. prompt for variable substitution:

Ampersand (&) is used to prompt for the variable substitution. Also ACCEPT can be used to prompt for the customized prompt text e.g.
ACCEPT variable CHAR PROMPT 'Enter Variable: '


HIDE option can be added at the end to not to echo the entered password, for example.

16. Run Unix commands on the SQL prompot

use HOST before the Unix command
e.g.
SQL>HOST ls
SQL>HOST pwd

17. Explain Plan
use SYS.PLAN_TABLE$ in case you don't have privilege to create a plan table

Resources:
https://www1.columbia.edu/sec/acis/dba/oracle/faq-sql.html

http://www.dba-oracle.com/ - has useful queries

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:969054700346631425 - Bulk move

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:273215737113 - Update columns from other table

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:974699100346868838 - should we use db user or application user?

http://www.adp-gmbh.ch/ora/misc/integrity_constraints.html - Oracle's Integrity Constraints

http://jenniferlinca.wordpress.com/ - Blog contains good Oracle stuffs

http://forums.oracle.com/forums/message.jspa?messageID=2348825 - Fun to read

http://www.dba-oracle.com/art_9i_indexing.htm - Performance tuning with index

http://www.orafaq.com/node/855 - Common errors using Outer Join

http://marist89.blogspot.com/ - Wonderful quote from the site - "The trouble with the world is that the stupid are cocksure and the intelligent are full of doubt." --Bertrand Russell. A good database blog

http://www.orafaq.com/forum/t/54714/0/ - Batch delete

http://forums.oracle.com/forums/thread.jspa?threadID=620843 - custom order by

http://www.sqlsnippets.com/en/topic-12186.html - Convert Rows to Columns

http://www.dba-oracle.com/t_converting_rows_columns.htm - Convert Rows to Columns

http://www.orafaq.com/node/855 - Error prone Join

Saturday, June 28, 2008

Capability Maturity Model (CMM) goals - quick reference

The goals to achieve a CMM level is summarized for quick reference:

CMM Level 2 (Repeatable)

Projects in Level-2 organizations have installed basic software management controls. Realistic project commitments are based on the results observed on projects and on the requirements of the current project. The software managers for a project track software costs, schedules, and functionality; problems in meeting commitments are identified when they arise. Software requirements and the work products developed to satisfy them are baselined, and their integrity is controlled.

Key Process Areas (KPA)

  • Requirement Management
  • Software Project Planning
  • Software Project Tracking and Oversight
  • Software Subcontract Management
  • Software Quality Assurance
  • Software Configuration Management

Goal 1: System requirements that are allocate to software are controlled to establish a baseline for software engineering and management use

Goal 2: Software plans, products, and activities are kept consistent with the system requirements allocated to software

Goal 3: Software estimates are documented for use in planning and tracking the software project

Goal 4: Software project activities and commitments are planned and documented

Goal 5: Affected groups and individuals agree to their commitments related to the software project

Goal 6: Actual results and performances are tracked against the software plans

Goal 7: Corrective actions are taken and managed to closure when actual results and performance deviate significantly from the software plans

Goal 8: Changes to software commitments are agreed to by the affected group and individuals

Goal 9: The prime contractor selects qualified software subcontractors

Goal 10: The prime contractor and the software subcontractor agree to their commitments to each other

Goal 11: The prime contractor and the software subcontractor maintain ongoing communications

Goal 12: The prime contractor tracks the software subcontractor’s actual results and performance against its commitments

Goal 13: Software quality assurance activities are planned

Goal 14: Adherence of software products and activities to the applicable standards, procedures, and requirements is verified objectively

Goal 15: Affected groups and individuals are informed of software quality assurance activities and results

Goal 16: Noncompliance issues that cannot be resolved within the software project are addressed by senior management

Goal 17: Software configuration management activities are planned

Goal 18: Selected software work products are identified, controlled, and available

Goal 19: Changes to identified software work products are controlled

Goal 20: Affected groups and individual are informed of the status and content of software baselines

Saturday, June 7, 2008

JEE software development estimation

Software estimation is one of the key as well as mystifying subject area in software engineering. There is an ongoing debate between two groups whether developing a software is an engineering discipline or a liberal science. The group that claims that software development is a science often put on the proof of it's inaccuracy in estimating a software development, consistently over the period of time.

There are several well known software estimation techniques or models like Line of Code (LOC), Function Point Analysis (FPA), COCOMO and some others. I've studied few of those estimation process but found overly complex to its value proposition or, to some extent, irrelevant to the projects that I was involved so far in my professional career. Most of my prior software development projects are based on J2EE and to develop Financial and CRM solutions. May be those estimation models are useful for many projects but at least I didn't find those very useful or cost effective in my projects. But I'm no way claiming those models or process as not useful.

In my projects I've been using some of the very straight forward and simple techniques to estimate the development effort (not the entire Software Project through) and those estimations were accepted by the project manager with very minor tweaks. The benefits that I get by using my simpler model are:
  • The ball park figure asked by the project manager (PM) can be provided with a 10-15% error margin
  • If PM asks for the base behind your estimation, you've something to defend your number
  • When your project manager pushes you to include additional feature(s) in the middle of development, you can bargain confidently that is backed by your detail estimation
  • People trust documents rather than your verbal explanation (but it may vary person to person and depends on the image in the team)
The driver behind my estimation model is experience and historical data. The factor I use in the model is mostly based on my experience and also supported by the historical data of the project. So it can't be mathematically or statistically proven but has justification. The factors I used in the model are:
  1. Complexity
  2. Familiarity
  3. Comfort level
  4. Implementation's spreading (number of places in the system the feature would've impact)
  5. New feature
  6. Modification of existing feature
  7. Up gradation of existing feature
  8. Change distribution
  9. Implementation items (e.g. Business Logic class, Data Access class, Utility class, Configuration file, Database table, Resource file, User Interface class/files etc.)
  10. Requirement stability (e.g. clarity)
  11. Unit/Integration/Functional test case development
  12. Buffer zone

Using the past experience in the project, I've given a value to each of the factors mentioned above and finally add-up the the numbers to get the time estimated to complete the feature implementation.

Here is an example of estimation calculation using the above estimation technique:

Initially it was little simpler as below:

C: Type of change (New=1/Upgrade=2/Modify=3)
D: Estimated complexity (Low=1/Medium-2/High-3)
E: Level of comfort (1 - Did similar before/2 - Didn't do but know/3 - First time doing)
F: Num of new view file
G: Num of view files to modify
H: Num of New Business methods
I: Num of Bussiness methods to modify
J: Num of new Dao methods
K: Num of Dao methods to modify
L: Num of new tables
M: Num of tables to modify
N: Change is for (e.g. how many products)

Estimation: C/1.5+F*3+G*1+H*3+I*1.5+J*2+K*1.5+L*3+M*1) *N * D/2

But later I found that the estimation that I was getting out of the formula was giving me the value with wide error margin and also didn't cover some fine aspect of estimation items. So I later refined it as below that worked for me almost with no issues for 6 releases (each release time spans 2-3 months of development and implementation) over 1 and half year

J: Type of Change (New=3/Upgrade=2/Modify=2/NoChange=0)
K: Estimated Complexity (Low=1/Medium=2/High=3)
L: Comfort Level (1 - Done similar before/2 - Have conceptual idea/3 - No idea/It can be fraction value)
M: Num of new view file
N: Num of view files to modify
O: Num of New Business methods
P: Num of Bussiness methods to modify
Q: Num of new Dao methods
R: Num of Dao methods to modify
S: Num of new utility methods
T: Num of utility methods to modify
U: Num of configuration files to modify
V: Num of new tables
W: Num of tables to modify
X: Num of places change would happen
Y: No. of Unit Test Cases
Z: Integration Testing? (1/0)
AB: Estimation without testing =(J/2*(M*3+N*1+O5*2+P*1.5+Q*1.5+R*0.75+S*0.5+T*0.25+U*0.1+V*2+W*1)*X*K/3*(L))+Y*1
AC: Estimation with testing (in hr) = =AB*1.5
AD: Estimation with testing (in day) [ceiling] =CEILING(AC/8,1)
AE: Estimation without testing (in day) = =AB/8
AF: Estimation without testing (in day) [ceiling] =CEILING(AE,1)

If a single point estimation needs to be communicated, I use the below famous formula to get a realistic estimation figure:

Estimated Day =(AC+2*(AC+AE)+AE)/6.0

It is certain that the above model would be different for other than Java based web application built for enterprises. In that case the value of factors and criteria would require to be tweaked or twisted

Resources

http://www.stellman-greene.com/aspm/images/ch03.pdf