Tuesday, April 12, 2011

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.............. 


Monday, March 21, 2011

MySQL and PostgreSQL database

The MySQL database has become the world's most popular open source  database because of its high performance, high reliability and ease of use. 

  1. It has 2 layers upper SQLlayer and set of storage engines
  2. The most commonly used storage engines in MySQL are InnoDB for ACID support and higher performance on large workloads
  3. It provides concurrency and MyISAM for Concurrency workloads.
  4. It has been developed with a focus on speed
  5. It supports 9 storage engines
  • MyISAM
  • InnoDB
  • NDB Cluster
  • MERGE
  • MEMORY (HEAP)
  • FEDERATED
  • ARCHIVE
  • CSV
It is also the database of choice for a new generation of applications built on the LAMP stack (Linux, Apache, MySQL, PHP / Perl / Python.) Many of the world's largest and fastest-growing organizations including Facebook, Google, Adobe, Alcatel Lucent and Zappos rely on MySQL to save time and money powering their high-volume Web sites, business-critical systems and packaged software.
The latest version available in the market can be downloaded to go to downloaded by clicking here

  1. It has been developed from developers who came from an oracle or SQL server backgreoun.
  2. Powerful for object relational database systems.
  3. Runs on all Operating Systems.
  4. Nativeprogramming interfaces for C/C++,Java,Tcl,Ruby etc.,
  5. It boasts features like Multi-Version Concurreny Control(MVCC).
  6. It has full support for subqueries (including subselects in the FROM clause), read-committed and serializable transaction isolation levels.
  7. GiST (Generalized Search Tree) indexing is an advanced system which brings together a wide array of different sorting and searching algorithms including B-tree, B+-tree, R-tree, partial sum trees, ranked B+-trees and many others. It also provides an interface which allows both the creation of custom data types as well as extensible query methods with which to search them. 
  8. Some general Limits

    The latest version available in the market can be downloaded to go to downloaded by clicking here
     

Friday, March 18, 2011

Download Open Source Software

Here are the links to various Open Source Software:

  • Operating Systems:

Ubuntu: this offers free downloadable operating system click here to download
for more information click here

Fedora: another version of Linux operating system offers free download of Operating system click here to download fedora 

Many versions are available in the market now some of them are:
  1.  Centos
  2. Redhat

  • Database
Coming to the data base though oracle still the leading database software provider and Microsoft still trying to dominate with its licensed version of SQL server 20XX. The open Source databases available in the world some of them are .... provided with the downloadable links.
MySQL and PostgreSQL: To Download MySQL click here

MongoDB:  One of MongoDB's best capabilities is its support for dynamic (adhoc) queries. It doesnot require any special indexing to find data,users can find data using any criteria. Best for relational queries. 
Click here to download  

Oracle BerkelyDB: It provides a collection of well-proven building-block technologies that can be configured to address any application need from the hand-held device to the data center, from a local storage solution to a world-wide distributed one, from kilobytes to petabytes.click here to download




Thursday, March 17, 2011

Biggest Open Source Projects

This tending towards didn't happened overnight. It was rather a painful journey. 'Sharing' was never a good thing for Corporates. These changes are due to success stories of largest Open Source Companies.
List of Companies started the change of Open Source Industry:
  1. GNU/Linux
  2. Redhat
  3. Debian
  4. Ubuntu
  5. Wordpress
  6. Drupal
  7. MySql
  8. Apache
 

So,The Linux Version(GNU) had made a revolutionary change in the World of Open Source Business.MySQL  in the business of Data Bases, DRUPAL WordPress in the world of Website Designing every open source software developed made changes and had a great impact on the business world.