Thursday, April 21, 2011

Learn "Index" with Example

Index in 7 points :
  1. Index is used for faster retrieval of rows from a table. 
  2. An index can be created explicitly by using the CREATE INDEX statement or implicitly by Oracle.
    The oracle server uses the index to search for a row rather than scanning through the entire table. 
  3. Indexing reduces search time and disk input/output.
  4. Creating and removing an index does not affect the table at all.
  5. When a table is dropped,all indexes based on that table are also removed.
  6. Implicit indexes are created when the primary key or unique constraint is defined. Such indexes get the name of the constraint .
  7. User can create explicit indexes based on non-primary key or non unique columns or on any combination of columns for faster table access.
The general syntax is : 
CREATE INDEX indexname ON tablename(columnname1,columnname1);

By using the above syntax the columns can hold duplicate values. To create unique index,the keyword UNIQUE should be included : 

CREATE UNIQUE INDEX indexname ON tablename(columnname1,columnname1);

EXAMPLE :

CREATE INDEX stu_idx ON student(last,first);
Now suppose we want to see the index for table student :
SELECT index_name, table_name, FROM user_indexes WHERE table_name =
'student';
Exercise :
Create an index to search students faster based on their ID.

CREATE INDEX index02 ON employee24(fname,lname);


Learn "Joins" with Examples(Answers)

create table contact_details02(code_no varchar(30) references emp_master02(emp_no),phone_no number(10),address varchar(30))

emp_master02

branch_master02


1)List the employee details along with branch names to which they belong..

   select * from emp_master02 e inner join branch_master02 b on e.branchno=b.branchno

2)List the employee details of only those employees who belong to the Administration department along with branch names to which they belong.

  select * from emp_master02 e inner join branch_master02 b on e.branchno=b.branchno where e.dept='software'

3)List the employee details along with the contact details using Right outer join.

  select * from contact_details02 c right outer join emp_master02 e on e.emp_no= c.code_no

4)List the employee details along with the contact details using Left outer join.

  select * from contact_details02 c left outer join emp_master02 e on e.emp_no= c.code_no

Learn "JOINS" with Examples(Questions)

EMP_MASTER :

EMP_NO FNAME MNAME LNAME DEPT DESIG BRANCH_NO
BRANCH_MASTER :
NAME BRANCH_NO
CONTACT_DETAIL:
CODE_NO PHONE_NUMBER ADDRESS
  1. List the employee details along with branch names to which they belong..
  2. List the employee details of only those employees who belong to the Administration department along with branch names to which they belong. 
  3. List the employee details along with the contact details using Right outer join. 
  4. List the employee details along with the contact details using Left outer join.

Monday, April 18, 2011

Compilation and Linking

Compilation refers to the processing of source code files (.c, .cc, or .cpp) and the creation of an 'object' file. 
This step doesn't create anything the user can actually run. Instead, the compiler merely produces the machine language instructions that correspond to the source code file that was compiled. 
For instance, if you compile (but don't link) three separate files, you will have three object files created as output, each with the name .o or .obj (the extension will depend on your compiler). 
Each of these files contains a translation of your source code file into a machine language file -- but you can't run them yet! You need to turn them into executables your operating system can use. That's where the linker comes in.
Linking refers to the creation of a single executable file from multiple object files. 
In this step, it is common that the linker will complain about undefined functions (commonly, main itself). 
During compilation, if the compiler could not find the definition for a particular function, it would just assume that the function was defined in another file.
If this isn't the case, there's no way the compiler would know -- it doesn't look at the contents of more than one file at a time. 
The linker, on the other hand, may look at multiple files and try to find references for the functions that weren't mentioned.

Why they are seperated?

First, it's probably easier to implement things that way. The compiler does its thing, and the linker does its thing -- by keeping the functions separate, the complexity of the program is reduced. 
Another (more obvious) advantage is that this allows the creation of large programs without having to redo the compilation step every time a file is changed. Instead, using so called "conditional compilation", it is necessary to compile only those source files that have changed; for the rest, the object files are sufficient input for the linker. 
Finally, this makes it simple to implement libraries of pre-compiled code: just create object files and link them just like any other object file. (The fact that each file is compiled separately from information contained in other files, incidentally, is called the "separate compilation model".)
Knowing the difference between the compilation phase and the link phase can make it easier to hunt for bugs. Compiler errors are usually syntactic in nature -- a missing semicolon, an extra parenthesis. Linking errors usually have to do with missing or multiple definitions. If you get an error that a function or variable is defined multiple times from the linker, that's a good indication that the error is that two of your source code files have the same function or variable.




Tuesday, April 12, 2011

Practice -3 (continued)

Simple Example Program on PL/SQL

5) Using Loop

  set serveroutput on
  DECLARE
  v_count number(2);
  v_sum number(2) :=0;
  v_avg number(3,1);
  BEGIN
  v_count := 1;
  loop
  v_sum :=v_sum+v_count;
  v_count :=v_count+1;
  dbms_output.put_line('v_count is'||v_count);
  exit when v_count>10;
  end loop;
  v_avg :=v_sum/10;
  dbms_output.put_line('average is'||v_avg);
  end;

  Output:

   v_count is2
   v_count is3
   v_count is4
   v_count is5
   v_count is6
   v_count is7
   v_count is8
   v_count is9
   v_count is10
   v_count is11
   average is5.5
   PL/SQL procedure successfully completed



6) Cursor
  set serveroutput on
  DECLARE
  v_first employee.fname%type;
  v_lname employee.lname%type;
  v_role employee.role%type;
  cursor employee_cur is
  select fname,lname,role from employee where employee_id='ADM001';
  BEGIN
  if not employee_cur%isopen then
  open employee_cur;
  end if;
  loop
  fetch employee_cur into v_first,v_lname,v_role;
  exit when not employee_cur%found;
  dbms_output.put_line('firstname' || v_first || 'lastname' ||v_lname || 'role' || v_role);
  end loop;
  end;

  Output:

Here we use the same table we have created earlier (employee24)

   fnameAdam lnameEve roleClient
   PL/SQL procedure successfully completed

 
7) Creating a procedure
  create procedure sw_emp9(emp_id in employee.employee_id%type,o_first out employee.fname %type,o_last out employee.lname%type)
  is
  begin
  select fname,lname into o_first,o_last from employee where employee_id=emp_id;
  end;

  Output:

8) Calling a procedure
  declare
  v_fname employee.fname%type;
  v_lname employee.lname%type;
  v_id employee.employee_id%type := '&emp_id';
  begin
  search_emp(v_id,v_fname,v_lname);
  dbms_output.put_line('firstname' || v_fname || 'lastname' ||v_lname);
  end;

  Output:


  old 4: v_id employee24.emp_id%type := '&employee_id';
  new 4: v_id employee24.emp_id%type := 'ADM001';
 

9) Creating a trigger
  create trigger employee_insert_before
  before insert on employee
  for each row
  begin
  dbms_output.put_line('before insert of ');
  end;

  Output:  Trigger created

Practice - 3

Simple Example Program on PL/SQL

1) Simple program

   set serveroutput on
   DECLARE
   num1 number;
   num2 number;
   BEGIN
   num1 := 5;
   num2 :=num1*2;
   dbms_output.put_line('double of num1 is'||num2);
   end;

  Output:  double of num1 is10

2) Calculating the area
   set serveroutput on
  DECLARE
  area number;
  radius number;
  pi constant number := 3.14;
  BEGIN
  radius := 5;
  area := pi*radius*radius;
  dbms_output.put_line('area is'||area);
  end;

  Output:  area is  78.5
 
3) Using IF
  set serveroutput on
  DECLARE
  v_day varchar2(10) :='&day';
  BEGIN
  if(v_day='sunday') then
  dbms_output.put_line('sunday is a holiday');
  else
  dbms_output.put_line('another day');
  end if;
  end;

  Output:
  
old 2: v_day varchar2(10) :='&day'; 
  new 2: v_day varchar2(10) :='mon'; 
  another day 
  PL/SQL procedure successfully completed.

4) Using SELECT
  set serveroutput on
  DECLARE
  v_first employee.fname%type;
  v_lname employee.lname%type;
  v_role employee.role%type;
  BEGIN
  select fname,lname,role into v_first,v_lname,v_role from employee where
  employee_id='ADM001';
 dbms_output.put_line('firstname'||v_first||'lastname'||v_lname||'role'||v_role);
  end;

  Output:
 
To get the output we now first create and insert data into table.
Here we are creating employee24

create table employee24
(
emp_id varchar(20),
fname char(20),
lname char(20),
role char(20)
);

insert into employee24
values
(
'&emp_id',
'&fname',
'&lname',
'&role'
);

select * from employee24

Final Output:

firstnameAdam lastnameEve roleClient
PL/SQL procedure successfully completed.


Practice - 2

1) Create a Pl/SQL block to declare a cursor to select last name,first name,salary and hire date from the Employee table. Retrieve each row from the cursor and print the employee's information if the employee's salary is greater than $50,000 and the hire date is before 31-DEC-1997.
 

Declare
  Cursor cur1 Is
Select lastname,firstname,salary,hiredate;
From emp_tbl02;
Begin
If emp.sal>50000 and hire date<31-dec-1997;
emp_rec  emp_cur1%rowtype;
begin
open emp_cur1;
loop
fetch emp_cur1 into emp_rec;
exit when emp_cur1%notfound;
dbms_output.put_line(emp_rec.firstname||' '||emp_rec.lastname||' '||emp_rec.salary||' '||emp_rec.hiredate);
end loop;
end;

End;


2) Create a cursor named as emp_cur that contains employee's last name,first name,salary and commission. Use the while loop to work with one row at a time. Within the while loop,employee's salary and commission are added together to find the total income. Print the total company wages (the total of all employee salaries and commissions).

declare
cursor emp_cur is
select f_name,l_name,emp_sal,commision from emp_tabl;
emp_rec emp_cur%rowtype;
emp_income number(20);
v_counter number := 1;
cmp_wages number(20) := 0;
begin
open emp_cur;
while(v_counter <= 3) loop
fetch emp_cur into emp_rec;
emp_income := emp_rec.emp_sal + emp_rec.commision;
cmp_wages := cmp_wages + emp_income;
v_counter := v_counter + 1;
end loop;
dbms_output.put_line('cmpny wages are '||cmp_wages);
end;