Wednesday, June 24, 2009

Normalization Exercise - Question # 2 Solution

Below is the solution of the QuestionNo.2 from the Normalization Exercises that I posted earlier.

QuestionNo2: Following are the attributes required to be stored by an organization during transactions. You are required to identify how many tables are required to store these attribute in Normalized Form.

Quantity Sold,

Date/Time of Sale,

Salesperson,

Product Name,

Product Category,

Product SubCategory,

Manufacturer Name,

Store Location,

Phone Number,

City Name,

State,

Population

You can assume any example data for this exercise.

Solution:

Sales Detail

ProductID

StoreID

SalesPersonID

QuantitySold

Date_Timeof Sale

Product Details

Product id

ProductName

ProductCategoryID

ProductSub CategoryID

ManufacturerID

Product Categories

ProductCategory ID

ProductCategoryName

ParentCategory ID

SalesPerson Details

SalesPersonID

SalespersonName

StoreID

Manufacturer Details

ManufacturerID

ManufacturerName

ManufacturerAddress

Store Details

StoreID

CityID

StoreLocation

PhoneNumber

City Details

CityID

CityName

StateID

Population

State Details

StateID

StateName

Sunday, June 7, 2009

SQL Subqeuries Practice

Assalam-o-Alaikum,

Dear Fellows, here I am sharing Some Queries to practice that we studied in 7th June09 Class. Faisal Sab also gave us some simple tasks to play with queries. Following is the script to generate required database and tables with insert queries;

--create database

create database company

--use company database and create tables then put data using insert queries.

use company

create table dept

(deptno numeric(2) primary key,

dname varchar(10))

create table designation

(desigId numeric(2) primary key,

desigDesc varchar(10))

create table emp

(empno numeric(4) primary key,

ename varchar(10),

sal numeric(6,2),

desigId numeric(2) references designation(desigId),

hiredate datetime,

deptno numeric(2) references dept(deptno))

insert into dept values (10,'sales')

insert into dept values (20,'accounts')

insert into dept values (30,'product')

insert into designation values (1,'Manager')

insert into designation values (2,'Clerk')

insert into emp values (1001,'faisal',1000.50,1,'2007-1-2',10)

insert into emp values (1002,'amir',900.50,2,'2008-2-28',10)

insert into emp values (1003,'javed',1500.50,1,'2009-3-14',20)

insert into emp values (1004,'kamran',1100.15,2,'2006-2-5',20)

insert into emp values (1005,'sohail',2000,1,'2005-1-12',30)

select * from dept

select * from designation

select * from emp

Now play with the queries to accomplish some of the simple tasks;

--Get all employees which are in ‘accounts’ department, using subquery

Select * from emp

Where deptno =

(select deptno from dept where dname='accounts')

--Get all managers, using subquery

Select * from emp

Where desigId =

(select desigId from designation where desigDesc='manager')

--Get manager of sales department using subqeury

Select * from emp

Where desigId =

(select desigId from designation where desigDesc='manager')

And

(select deptno from dept where dname='sales')

--Get employees which have more salary than the average salary of all employees

SELECT * FROM emp

WHERE sal > (SELECT AVG(sal) FROM emp)

--Get Average sal of employees working in sales dept

SELECT avg(sal) FROM emp

WHERE deptno = (SELECT deptno FROM dept WHERE dname='sales' )

--Get the junior most employee

SELECT * FROM emp

WHERE hiredate = (select min(hiredate) FROM emp)


--Get the senior most employee

SELECT * FROM emp

WHERE hiredate = (select max(hiredate) FROM emp)

--Get the who has complete more than one year

SELECT * FROM emp

WHERE datediff(year,hiredate,GETDATE()) > 1

I hope you understand all, in case any qeury feel free to put comments.

Tuesday, June 2, 2009

Database Nomalization Exercises

Assalam-o-Alaikum,

                Dear Fellows, I was trying to post this one on Sunday 31st but due to a bit tough schedule I could find enough time to do this, and I was also looking for a bit tougher questions so that we can test ourselves and do a little bit hard work to solve the normalization problems. As we also have to do some homework we got in 31st may class. We can solve these questions as our homework and bring the solutions as the script of normalized database i.e tables script and can show in the class going to held on 7th June. Here are the Normalization problems;

QuestionNo1: It’s can be said as the data of a “tuition center” where different students study different subjects and different subject are taught by different teachers, field list ans some sample data is as below.

StudentId

StudentName

Class_Name

CalssTime

Location

AdmissionDate

Gardian

GardianContact

StudentContactNos

Professor

ProfessorAddress andContact

Sample data in every field would be (Deu to longer column width I distributed the table in three chunks);

student_id

StudentNAme

class_name

calss time

location

999-40-9876

Sarmad Ali

Math 148

11:30:00

Qaid Abad, New Street, Karachi

999-43-0987

Syed Kamran

Physics 113

1:00:00

Jimkhana, Street 1, Mohallah II, Karachi

999-43-0988

Sameena Bibi

Urdu 101

16:30:00

Flat#2/4,Block A, GorakhLines, Hyderabad

999-43-0985

NoorUllah Awan

CS 222

17:30:00

seasam colony, Malir III, Karachi

999-43-0987

SanaUllah

Botany 142

16:00:00

Street1, New Housing Scheme, Mir pur Khas

999-43-0988

Muhammad Kamran

Math 148

11:30:00

House# 31, StreetIV, Latifabad, Hyderabad

999-43-0990

Shabnam Hasan

CS 222

17:30:00

houseNo. 333, Pak Collony, Quetta


Admission date

Gardian

GardianContact

StudentContactNos

9/23/2008

Muhammad Ali

0322-2311333

43432221,0300-8878999,3321113

9/21/2009

Arbab Rahim

093382212,098-3121123

22242334,9902342

8/2/2008

Nusrat Salman

0321-3323113,021-3234113

443-55423422

3/6/2008

Karim Ali

0333-1234551,031-4666432

554-4322221

5/9/2009

Abdullah

44231-82212,551-4522222

021-03313223

9/30/2009

Jahanzaib Fareed

0554-82212,098-3121123

2131222-31231232

10/1/2009

Hamza Dawood

0332-3382212,902-3121123

0332-3382212,902-3121123


Professor

ProfessorAddress and Contact

Salman Ahmed

New hawks colony, near Cantt. Karachi, 0988-23211444

Muhammad Saleem

House No. 22-3, talha blocks, malir karachi, 021-3343222

Rafiuddin Ali

FlatNo.4/3, latifabad, kamran hights, Hyd, 9877-443323

Abdul Majid

House no. 2/3, Near PNS Shifa, cantt karachi, 03333-3333333

Miss Zaibunisa

flat no.111, newland towers, creak area karachi, 021-344221

Salman Ahmed

New hawks colony, near Cantt. Karachi, 0988-23211444

Abdul Majid

House no. 2/3, Near PNS Shifa, cantt karachi, 03333-3333333

Dear fellows if there is any problem in getting this due to table layout, you can email me at abdulmajid11@hotmail.com to get this, i will send you pdf or excel file containing complete question.

QuestionNo2: Following are the attributes required to be stored by an organization during transactions. You are required to identify how many tables are required to store these attribute in Normalized Form.

Quantity Sold,

Date/Time of Sale,

Salesperson,

Product Name,

Product Category,

Product SubCategory,

Manufacturer Name,

Store Location,

Phone Number,

City Name,

State,

Population

 You can assume any example data for this exercise.

Best of Luck.. I hope this would be an easy go for you all...

Last but not least. I will be posting the solution till next class may be after the class but every one is welcome to discuss anything related to it.. your comment will be highly appreciated on this or you can also share you thoughts and ask any thing via email to me at abdulmajid11@hotmail.com.