Archive

Posts Tagged ‘SQL and Database’

What project you are planning to develop in this semester

January 11th, 2011 No comments

I observed that in most of the colleges, distribution of projects (as a college assignment) is very much similar every year, as it is given below.

Project Name Group size Popularity in %
Library Management 4 20
Hotel Management 3-4 15
HR Management 7-8 30
Cyber cafe Management 2 5
Transportation management 2 5
Other application like antivirus, dictionary etc 2 each 3 – 5
Other Management projects 3-4 each 20-30
Inventory management 3-4 20
Payroll management 2-3 15
School or college management 3-4 20

*This table is applicable only for computer branch students. And it might not be fit for some colleges.

All the projects which I mentioned are generally developed every year. And their understanding and documents flow from seniors to juniors like heirloom.

Ideas for project planning

Why don’t you try something different in this semester?

If you are deeply interested to design only management projects, It is not necessary that your project must have database. You may go beyond of this.

  • Make a network based application like a chat messenger which supports video chatting.
  • Or a FTP client that let you uploads & downloads files from a server.
  • You may either go for your own SMTP server or a mail server.

If you had set up your mind for standalone applications but not network based application then you may refer these topics as well

  • Mobile based dictionary
  • Or mobile based local bus timetable.
  • You also can develop a rich text editor
  • Or a traffic monitor system

Have a look on some web based applications

  • Develop a web based application to manage your appointments. Who can remind you and can present a calendar view.
  • You can develop an application which let user submit their message to various online social networking sites.
  • You can redesign your college website. So that it can be controlled fully from the back end.

Why to develop management type of project

These projects increase your understanding in a particular domain. You can highlight your domain knowledge in your resume. Further you can easily join a firm or an organization belongs to a particular domain like banking, finance or something else. All management based application use database. So their development shall increase your database knowledge as well. Database knowledge is primary to get selected in an offline campus. I’ll support if you do some certification in a particular domain parallel with your study. It’ll definitely help you in project development and campus selection as well.

Why not to develop management type of project

Application like network or mobile based applications, increases your knowledge in a particular technology. They give you confidence to work independently. You can make many of open source applications to weight up your resume. Moreover, If you are working on something different application or project then it’ll definitely fetch attention of your whole class and teachers as well.

SQL:How to fetch starting N rows from a table

October 7th, 2010 1 comment

DB2

		select *
		from 	(select rownumber() over(order by <colName>) as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num < N+1 with ur

Oracle

		select *
		from 	(select rowid as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num < N+1

SQL:How to fetch Nth row of a table

October 6th, 2010 No comments

DB2

		select *
		from 	(select rownumber() over(order by <colName>) as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num = N with ur

Oracle

		select *
		from 	(select rowid as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num = N

SQL:How to fetch rows from nth position to mth position

October 6th, 2010 No comments

DB2

		select *
		from 	(select rownumber() over(order by <colName>) as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num between 1003 and 1118  with ur

Oracle

		select *
		from 	(select rowid as row_num, <colName2>
			from <table name>)
			as <alias name>
		where row_num between 1003 and 1118

Another sample inventory database for boutique

September 20th, 2010 2 comments
database for boutique
I had made a project in college on a boutique shop. And this time I am sharing its dummy database with you. You may use its database to build your own application.

This database contains enough data for your practice and to test your application completely. Still if you do some changes or populate it with more data then please share it with me. So I can spread your work.

Download here
This area is protected to registered users only.

Hotel management, an analysis

September 14th, 2010 No comments

Various important components for a hotel,

1. Room
2. Staff
3. Guest/ customer

Main processes

1. Booking status
2. Room availability

Class diagram for hotel reservation system


class diagram for hotel reservation system

Above diagram is suggested by Mr Barry Williams with consideration that the hotel is having various branches across countries. If you are making application for small hotels then you can remove above four classes ie Hotel chains, and classes starting with “Ref”.

For medium size hotels or as per your need, you can add staff rating or any other internal process like their payroll, attendance, leave etc. But if you are making this project as college assignment then you can take these extra processes as a separate project.

I’ll suggest you to make 3-4 groups. Each group can take one part like payroll management, reservation system etc. Attach all of them at last.

You also can download sample database for hotel management application from previously written article.

Class diagram for transportation and shipment application

September 12th, 2010 No comments

I already had shared many sample database and their class diagrams for inventory projects.

  1. Class diagram for Goods Inventory
  2. transport inventory project
  3. Goods Inventory control

I hope the below class diagram for transport & shipment companies, will help you. It is drawn and analyzed by Mr. Barry Williams.


shipment and transportation class diagram

Save above image on your PC for better view.

Sample database for transport inventory project

September 11th, 2010 No comments

I had already provided sample database for inventory of goods and an analysis report too.


truck amty inventory transport shipment

If you are developing an application for some transport company or for your personal practice then this sample file can help you. You can use this database file for personal or commercial purpose. I am providing this file specially to increase understanding about database designing. You can read analysis report & design for transport based application.

Download here
This area is protected to registered users only.

List of 6000+ authors

September 11th, 2010 No comments

If you are building some wiki type of application or a project for library or bookstore then this list may be helpful for you. It contains name of 6000+ authors. It also contains date of birth field. But few entries of date of birth are available. You can complete it.


authors list

Feel free to use it. And please share it with me, if you so some update in it. I had uploaded many database files for your reference and help. You may list them by db files and can download them.

Download here
This area is protected to registered users only.

Inventory control, an analysis

September 10th, 2010 No comments

Students who are making projects for their college or practice purpose can use below class diagram against what he analyzed before.

With this diagram, you can go for developing an application for some small firm, shops, small shopping mall to some limited big firm who deals in selling & buying items.


amty inventory control class diagram

You can also download sample databasee file for practice. This diagram is in more detail than the database file I suggested before.

Thanks to Mr. Barry Williams to suggest this diagram.

Sample database for an inventory project

September 10th, 2010 2 comments

Like other articles of db files, I wrote this to provide free and sample database related to inventory. This database file is related to item and customer relationship. It’ll be very helpful if you are trying to solve previously written article on SQL exercise. Otherwise you can use this database to make any inventory project related to small firm or shops deal in selling and buying items.


article-stack bar code

You may refer my another article for some analysis work with diagrams here.

This database file is available in MS Access format and contains some dummy data for practice. It contains around 4-5 tables. For more data, you would have to populate yourself. Feel free to use this database. And share it with me, if you do some update on it.

Download here
This area is protected to registered users only.

How to handle runtime exception in DB2

September 1st, 2010 No comments

DB2 Err Handling

This post is all about Error handling in DB2 in brief. I am trying to cover everything in only single post. So perhaps some advanced topics are missed or would be discussed in short.

Prerequisites:

  • writing basic & simple stored procedures in DB2.

Write a procedure and use below steps to handle runtime errors.

1. Write this statement in declaration section. Declaration must take place before any cursor declaration.

DECLARE SQLCODE INT DEFAULT 0;

2. Now define Exit Handler in declaration area. It may take place after cursor declaration as well.

DECLARE EXIT HANDLER FOR SQLEXCEPTION
    C1:BEGIN
        SET O_RET_VAL = SQLCODE;
    END C1;

Explanation :
1. All statements which should executed against an exception, take place inside BEGIN..END block.
2. Last word of first line is EXCEPTION code or Name. Read more exception code in last of this post.
3. Inside BEGIN..END block, you may set OUT parameters, ROLLBACK etc.
Sample SP:

CREATE PROCEDURE proc_name(IN var INTEGER,
                            OUT debugStatement varchar(100),
                            OUT O_RET_VAL INTEGER,
                            OUT O_SQLSTATE CHAR(5))
    LANGUAGE SQL
    P1 :BEGIN
        --Declaration Of Local Variables---
        DECLARE SQLCODE INT DEFAULT 0;            --Declare before declaration of any cursor
        DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
        --Cursor Declaration--
    ----------------------------Error Handling--------------------------------
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
             SET O_RET_VAL = SQLCODE;
             SET O_SQLSTATE = SQLSTATE;
             SET debugStatement = 'Exit due to SQL Exception';
             --or in short
             --VALUES (SQLCODE, SQLSTATE, 'Exit due to SQL Exception' )INTO (O_RET_VAL, O_SQLSTATE, debugStatement);
            END;
    ---------------------------Error Handling End-----------------------------
        :
        SET debugStatement = 'Middle of SP';
        :
        :
        SET debugStatement = 'End of SP, completed Successfully.';
    END P1
    @

Compile in UNIX : db2 -td@ -f
Run in UNIX : db2 “call proc_name(23,?,?)

Tip: We can set Output parameters anywhere throughout the SP. When a SP stops normally or abnormally, it returns current value of output parameters automatically.

We have completed basic of exception handling in DB2. Now we are switching to some advanced topics.

Some more Handlers :

SQL PL supports three types of handlers: EXIT, CONTINUE, and UNDO.
The EXIT handler will execute the SQL PL statements in the handler. After that the handler will continue with execution at the end of the compound statement in which it was declared.

An UNDO handler is similar to the EXIT handler and continues with execution at the end of the compound statement in which it was declared. But instead of the EXIT handler each executed statement will be rolled back in this compound statement. UNDO handlers are just possible in ATOMIC compound statements.

In opposite to the EXIT handler, the CONTINUE handler will continue the execution at the statement which is following the statement that raised the exception.
Note: Both CONTINUE, and UNDO are defined in the same way as EXIT handler is defined. Only their working differs.

You can define multiple handlers together.

DECLARE c_duplicate CONDITION FOR SQLSTATE '23505';
    DECLARE FOREIGN_KEY_VIOLATION CONDITION FOR SQLSTATE ‘23503’;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    Begin
        :
    End;
    DECLARE EXIT HANDLER FOR c_duplicate
    Begin
        :
    End;
    DECLARE EXIT HANDLER FOR FOREIGN_KEY_VIOLATION
    Begin
        :
    End;
    DECLARE CONTINUE HANDLER FOR c_duplicate
    Begin
        :
    End;
Please note this
In above code multiple exception handlers are defined for multiple exception conditions. You can declare the condition first then can use it to your exception handler. Or can use it directly. Its all up to you.

Like Java or other programming language you might need to throw or raise an exception manually. For this;

    SIGNAL '23505'
or
    DECLARE c_duplicate CONDITION FOR SQLSTATE '23505';
    :
    SIGNAL c_duplicate;

Once an exception is raised it’ll search for corresponding exception handler block. If no one exist then it’ll terminate the SP abnormally.

You can also set Message with raised exception as follows;

    SIGNAL c_duplicate
    SET MESSAGE_TEXT = 'Records are duplicated for table name';
Please note this

Some conditions which might be suitable to your code.

    DECLARE c_too_many_rows CONDITION FOR SQLSTATE '99001';
    DECLARE c_duplicate CONDITION FOR SQLSTATE '23505';
    DECLARE FOREIGN_KEY_VIOLATION CONDITION FOR SQLSTATE ‘23503’;
    DECLARE overflow CONDITION FOR SQLSTATE ’22003’;
    DECLARE c_error CONDITION FOR SQLSTATE '99999';

Solved SQL exercise for novice to experts

July 7th, 2010 No comments
database

There is an example , some questions, and corresponding queries. These queries are written in Oracle. But it’ll give you clear logic about how to write SQL queries in any sort of SQL like DB2, TSQL, MySql etc. I constructed this exercise during my training. Although Some questions do not having solution. So those will be an exercise for you. If i get time then will update them later.

First of all you need to create tables in your database. Use above “ready to use” code.

--################ DDL #######################
create table stock
(item_no varchar2(5) primary key,
item_name varchar2(15) not null,
Quoted_price number(6,2),
Category varchar2(5));

create table category
(c_code varchar2(5) primary key,
c_name varchar2(15) not null unique);

 alter table stock
 add constraint s_fk Foreign key(category) references Category(c_code) on  delete cascade ;

create table purchase
(p_no varchar2(5) PRIMARY KEY,
item_no varchar2(5) references stock(item_no),
p_price number(6,2) not null,
p_date date,
p_qty number(3));

 create table sale
 (s_no varchar2(5) PRIMARY KEY,
 item_no varchar2(5) references stock(item_no),
 s_price number(6,2) not null,
 s_date date,
 s_qty number(3),
 cust_name varchar2(15)) ;

Now you need to populate your tables with some dummy but meaningful data. So you can test your queries.

--############### DML #####################
BEGIN
insert into CATEGORY VALUES('C101','FOOD');
insert into CATEGORY VALUES('C102','SPORT');
insert into CATEGORY VALUES('C103','ELEX');
insert into CATEGORY VALUES('C104','CLOTH');
insert into CATEGORY VALUES('C105','OTHER');
END
;

BEGIN
insert into stock values('i101','BREAD',17,'C101');
insert into stock values('i102','BUTTER',67,'C101');
insert into stock values('i103','BISCUIT',87,'C101');
insert into stock values('i104','GEM',56,'C101');
insert into stock values('i105','CHOCOLATE',17,'C101');
insert into stock values('i106','EGG',5,'C101');
insert into stock values('i107','SALT',8,'C101');
insert into stock values('i108','BALL',27,'C102');
insert into stock values('i109','BAT',217,'C102');
insert into stock values('i110','FOOTBALL',117,'C102');
insert into stock values('i111','WICKETS',97,'C102');
insert into stock values('i112','CAP',111,'C102');
insert into stock values('i113','TSHIRT',1011,'C104');
END

BEGIN
insert into PURCHASE values('P101','i101',15,'01-nov-2008',16);
insert into PURCHASE values('P102','i102',65,'01-nov-2008',34);
insert into PURCHASE values('P103','i103',85,'01-nov-2008',12);
insert into PURCHASE values('P104','i104',56,'01-nov-2008',35);
insert into PURCHASE values('P105','i105',17,'01-nov-2008',23);
insert into PURCHASE values('P106','i106',4,'01-nov-2008',34);
insert into PURCHASE values('P107','i107',7,'01-oct-2008',23);
insert into PURCHASE values('P108','i108',25,'01-oct-2008',56);
insert into PURCHASE values('P109','i109',201,'01-sep-2008',67);
insert into PURCHASE values('P110','i110',100,'01-oct-2008',54);
insert into PURCHASE values('P111','i111',90,'01-mar-2008',23);
insert into PURCHASE values('P112','i112',100,'01-feb-2008',12);
insert into PURCHASE values('P113','i113',800,'01-mar-2008',6);
END;

BEGIN
insert into SALE values('S101','i101',19,'01-DEC-2008',6,'AMIT');
insert into SALE values('S102','i102',65,'02-DEC-2008',14,'ROHIT');
insert into SALE values('S103','i103',86,'05-DEC-2008',2,'AMIT');
insert into SALE values('S104','i104',63,'27-nov-2008',12,'SHYAM');
insert into SALE values('S105','i105',21,'16-nov-2008',3,'KAMAL');
insert into SALE values('S106','i106',6,'13-DEC-2008',4,'PREETI');
insert into SALE values('S107','i103',88,'17-nov-2008',2,'RAHUL');
insert into SALE values('S108','i104',57,'19-nov-2008',5,'RAHUL');
insert into SALE values('S109','i105',19,'28-nov-2008',2,'SOHAN');
insert into SALE values('S110','i111',115,'12-DEC-2008',3,'SHAALU');
insert into SALE values('S111','i112',145,'17-NOV-2008',8,'SHAALU');
insert into SALE values('S113','i113',1300,'17-FEB-2008',2,'UMESH');
insert into SALE values('S112','i112',120,'01-MAR-2008',4,'SHYAM');
insert into SALE values('S114','i112',130,'06-OCT-2008',2,'KAMAL');
END;

Now this is the time to practice. See given examples and solved queries. First try all queries given below. Then login to this article-stack to find out their solution.

1) LIST OF ITEMS OF ‘SPORT’ CATEGORY

This area is protected to registered users only.

2) LIST OF ITEMS OF ‘SPORT’ & ‘FOOD’ CATEGORIES
This area is protected to registered users only.

3) LIST OF ITEMS OF ‘SPORT’ CATEGORY, whose PRICE IS greater than average PRICE of all items.
This area is protected to registered users only.

4) LIST OF SOLD ‘SPORT’ ITEMS.
This area is protected to registered users only.

5) LIST OF ITEMS SOLD TO ‘AMIT’.
This area is protected to registered users only.

6) LIST OF ITEMS SOLD IN LAST WEEK.
This area is protected to registered users only.

7) LIST OF ‘FOOD’ ITEMS SOLD IN LAST WEEK.
This area is protected to registered users only.

8) LIST OF ITEMS THOSE ARE SOLD TO GAIN MAX PROFIT. 9) LIST OF ‘FOOD’ ITEMS PURCHSED 1 MONTH AGO.
This area is protected to registered users only.

10) LIST OF ‘FOOD’ ITEMS THOSE ARE NOT SOLD IN LAST WEEK.
This area is protected to registered users only.

14) LIST OF CUSTOMERS WHO PURCHASED ‘FOOD’ ITEMS BUT NOT ‘SPORT’ OR ‘CLOTH’ ITEMS. 15) LIST OF CUSTOMERS WHO PURCHASE ITEMS OF SINGLE CATEGORY ONLY. 16) LIST OF SOLD ITEMS WHOESE QUOTED PRICE > 30.
This area is protected to registered users only.

I’ll try to update more queries in my other articles.