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;

Answers to practice -1

Questions click here

create table hotel02(hotelno varchar(20),hotelname char(20), city char(20));

create table room02(roomno varchar(5),hotelno varchar(2),type char(10),price varchar(10));

create table booking02(hotelno number(10),guestno number(10),date_from varchar(30),date_to varchar(30),roomno number(10))
insert into booking02 values(005,135,'26-aug-2010','30-sep-2010',212)
insert into booking02 values(005,135,'26-aug-2010',30-sep-2010',212)
insert into booking02 values(004,235,'16-aug-2010','1-sep-2010',112)
select * from booking02
insert into booking02 values(003,035,'12-sep-2010','10-oct-2010',103)
insert into booking02 values(002,025,'22-jul-2010','15-aug-2010',301)
insert into booking02 values(001,023,'23-jun-2010','25-aug-2010',204)
insert into booking02 values(001,023,'23-jun-2010',25-aug-2010',204)



select hotelname from hotel02 where city='london'

Alter table booking02 add PRIMARY KEY(hotelno,guestno,date_from)
Alter table room02 add PRIMARY KEY(roomno,hotelno)
Alter table hotel02 add PRIMARY KEY(hotelno)



create table guest02(guestno varchar(5),guestaddress varchar(50),guestname char(10),
                  primary key (guestno));


update room02 set price =price+price*0.05;


select * from room02




select*from booking02 where date_to IS NULL

select count(*)from hotel02

select avg(price)from room02

SELECT HotelNo, COUNT(RoomNo) AS count FROM Room02 GROUP BY Hotelno;

select r.hotelno,COUNT(roomno)from room02 r, hotel02 h where r.hotelno=h.hotelno AND city='london'GROUP BY r.hotelno 

SELECT MAX(X) AS MostlyBook FROM (SELECT Type, COUNT(Type) AS X FROM Booking02 b, Hotel02 h, Room02 r
          WHERE r.roomno = b.roomno AND b.hotelno = h.hotelno AND h.city LIKE '%london%' GROUP BY Type);

select type,price from hotel02,room02 where hotel02.hotelno=room02.hotelno and hotelname='taj'

SELECT * FROM Guest02 WHERE GuestNo IN
(SELECT GuestNo FROM Booking02 WHERE Date_From <= SYSDATE
AND (Date_To >= SYSDATE OR Date_To IS NULL)
AND HotelNo =(SELECT HotelNo FROM Hotel02
WHERE HotelName = 'Grosvenor'));

SELECT r.* FROM Room r LEFT JOIN
(SELECT g.GuestName,h.HotelNo,b.RoomNo FROM Guest02 g,Booking02 b,Hotel02 h
WHERE g.GuestNo = b.GuestNo AND b.HotelNo = h.HotelNo AND
h.HotelName= 'Grosvenor' AND
b.Date_From <=CURRENT_DATE AND b.Date_To >=CURRENT_DATE) AS X
ON r.HotelNo = X.HotelNo AND r.RoomNo = X.RoomNo;


SELECT SUM(price) FROM Booking02 b, Room02 r, Hotel02 h
WHERE (b.Date_From <= CURRENT_DATE AND
b.Date_To >= CURRENT_DATE) AND
r.Hotelno = h.HotelNo AND r.RoomNo = b.RoomNo;

SELECT * FROM Room02 r
WHERE RoomNo NOT IN
(SELECT RoomNo FROM Booking02 b,Hotel02 h
WHERE (Date_From <= CURRENT_DATE AND
Date_To >= CURRENT_DATE) AND
b.HotelNo = h.HotelNo AND HotelName ='pune');

SELECT SUM(Price) FROM Room02 r
WHERE Room_No NOT IN
(SELECT RoomNo FROM Booking02 b, Hotel02 h
WHERE (Date_From <= CURRENT_DATE AND
Date_To >= CURRENT_DATE) AND
b.HotelNo = h.HotelNo AND HotelName = 'pune');

Monday, April 11, 2011

practice -1

Some Practical Examples which i have done
 

1) Develop Physical schema for following relations:- 
  1. Table name : - Hotel hotelNo hotelName city
  2. Table name : -Room roomNo hotelNo type price
  3. Table name :- Booking hotelNo guestNo dateFrom dateTo roomNo
  4. Table name :-Guest guestNo guestName guestAddress
For the above schema's following primary keys are used:-
  1. Table name Primary key
  2. Hotel hotelNo
  3. Room (roomNo,hotelNo)
  4. Booking (hotelNo,guestNo,dateFrom)
  5. Guest guestNo
Identify the foreign keys in the schema. 
 2 ) Answers the following queries :
  1. Populating tables:
  2. Insert minimum 10 rows into each of these tables.
  3. Update the price of all room by 5%.
Simple queries :
  1. List full details of all hotels.
  2. List full details of all hotels in London.
  3. List the names and addresses of all guests living in London, alphabetically ordered by name.
  4. List all double or family rooms with a price below $40.00 per night, in ascending order of price.
  5. List the bookings for which no dateTo has been specified.
Aggregate functions :
  1. How many hotels are there?
  2. What is the average price of a room?
  3. What is the total revenue per night from all double rooms?
  4. How many different guests have made bookings for August?
Grouping :
  1. List the number of rooms in each hotel.
  2. List the number of rooms in each hotel in London.
  3. What is the average number of bookings for each hotel in August?
  4. What is the most commonly booked room type for each hotel in London?
  5. What is the lost income unoccupied rooms at each hotel today?
 Subqueries and joins :
  1. List the price and type of all rooms at the Grosvenor Hotel.
  2. List all guests currently staying at the Grosvenor Hotel.
  3. List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the room, if the room is occupied.
  4. What is the total income from bookings for the Grosvenor Hotel today?
  5. List the rooms that are currently unoccupied at the Grosvenor Hotel.
  6. What is the lost income from unoccupied rooms at the Grosvenor Hotel?

Answers will be posted Soon..............