









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Student’s Name____Valeed javeed____ Registration Number 1812128 Program: Class/Section: BCS/BS- Course Name: Database Systems Lab Course Code CSCL Instructor’s Name: Naeem Akbar Channar Instructor’s email [email protected]
Date of Sharing: 10 th^ June 2020 Submission Deadline: 11 th^ June 2020 File Type Allowed for submission: (MS Word, MS Excel, pdf, Images, Hand written) IMPORTANT instructions: Read all the questions carefully and then solve in this file, if needed. Marks of each question are mentioned at the end of each question. Solved Assignment must be mailed to the respective faculty by the due date. The answer file must be attached in the email. Regardless of file type (image or document), email must include attachment for faculty to check. Non-submissions of the assignments will be considered as absent for the Final Term and will be marked “0” on ZABDESK Recap Sheet. While checking of the Assignment, SZABIST Plagiarism policy is to be followed. Group assignments are not allowed.
QNo.1 Create following tables in Sql Server with specified constraints. (2 Marks) Faculty Signature
Product P_id Primary Key, Automatically inserted start from 100 & increment of 2 P_name Not duplicate, not null P_price can’t be inserted if price is >100 and < Category Cat_id Primary Key, Automatically inserted start from 1 & increment of 1 Cat_name not duplicate, not null Customer Cus_id Primary Key, Automatically inserted start from 400 & increment of 4 Cus_name Not duplicate, not null Cus_city if city not inserted it will automatically insert ‘Hyderabad’ Shop Sh_id Primary Key Sh_name not duplicate,not null Sh_city Not Null Product_cat
insert into product values('Charger Pin',50) insert into product values('Mouse Pad', 530) insert into product values('H1 Speaker',760) insert into product values('H3 Mic',60) insert into product values('H5 Hand Free',120) insert into product values('Cannon 123',19000) insert into product values('Sony 432',30000) insert into product values('Techo Spark 4',16000) insert into product values('Infinix Hot 5',20000) insert into product values('Iphone 11 Max',299999) insert into product values('Moto Razor',12000) insert into product values('Spark LED',5000) insert into product values('Iphone LED',20000) insert into product values('Infinix LED',4320) select * from product create table category(c_id int identity(1,1) primary key,c_name varchar(50) unique not null) insert into category values('Computer') insert into category values('Mobile') insert into category values('Computer Accessories') insert into category values('Mobile Accessories') insert into category values('Camera') select * from category create table customer(c_id int identity(400,4) primary key, c_name varchar(50) unique not null, c_city varchar(50) DEFAULT 'Hyderabad') insert into customer values ('Ahmed','Karachi'),('Junaid','Hyderabad'), ('Waseem','Lahore'),('Rashid','Lahore'),('Majid','Lahore'),('Sajid','Karachi'), ('Jamshed','Hyderabad'),('Rehan','Hyderabad') select * from customer
create table shop(s_id int primary key, s_name varchar(50) unique not null, s_city varchar(50) not null) insert into shop values(111,'Kiran Electronics','Karachi'),(112,'Memon Communication','Hyderabad'),(113,'Junaid Communication', 'Lahore'), (114,'Zeeshan Computers','Islambad') select * from shop create table Product_category ( p_id int foreign key(p_id) references product(p_id), c_id int foreign key(c_id) references category(c_id)) insert into Product_category values(100,1) insert into Product_category values(102,1) insert into Product_category values(104,3) insert into Product_category values(106,3) insert into Product_category values(108,3) insert into Product_category values(110,3) insert into Product_category values(112,4) insert into Product_category values(114,4) insert into Product_category values(116,3) insert into Product_category values(118,4) insert into Product_category values(120,4) insert into Product_category values(122,4) insert into Product_category values(124,5) insert into Product_category values(126,5) insert into Product_category values(128,2) insert into Product_category values(130,2) insert into Product_category values(132,2) insert into Product_category values(134,2) insert into Product_category values(136,4) insert into Product_category values(138,4) insert into Product_category values(140,4)
insert into customer_order values(408,104,30) insert into customer_order values(412,106,10) insert into customer_order values (416,108,12) insert into customer_order values(420,110,34) insert into customer_order values(424,112,14) insert into customer_order values(428,114,16) insert into customer_order values(400,116,11) insert into customer_order values(404,118,4) insert into customer_order values(408,120,56) insert into customer_order values(412,122,3) insert into customer_order values(416,124,21) insert into customer_order values(420,126,23) insert into customer_order values(424,128,11) insert into customer_order values(428,130,12) insert into customer_order values(400,132,4) insert into customer_order values(404,134,5) insert into customer_order values(408,136,4) insert into customer_order values(412,138,12) insert into customer_order values(416,140,23) insert into customer_order values(420,128,43) insert into customer_order values(424,130,54) insert into customer_order values(428,132,6) select * from customer_order QNo.2 Create Following Simple Quires (3 Marks) I. Select Those Product whose price is greater than 1000 and less than 2000 II. Select those shops whose city is Hyderabad and islambad. III. Select all products whose name end with r. IV. Select all customers whose name contain i (start,end,or middle,anywhere) V. Create following output Product Id 12 | Product Name Mouse | Price 300
select * from product where p_price >1000 and p_price < select * from shop where s_city in ('Hyderabad','Islambad') select * from product where p_name like '%r' select * from customer where c_name like '%i%' select 'Product Id = '+convert (varchar(50),p_id)+ ' | Product Name = '+p_name+ ' | Price = ' +convert (varchar(50),p_price) from product QNo.3 Create Following Join Quires (5 Marks) I. Select Customers That purchase product from Zeeshan computers select cus_name as 'Customer Name' from Customer join Customer_Order on Customer_Order.cus_id = Customer.cus_id join Shop_Products on Shop_Products.p_id = Customer_Order.p_id where Shop_Products.sh_id = 114
IV. Select Ahmed Total Purchasing select cus_name as 'Customer Name', sum(quantityp_price) as 'Total Purchasing' from Product join Customer_Order on Customer_Order.p_id = Product.p_id join Customer on Customer.cus_id = Customer_Order.cus_id where Customer.cus_name = 'Ahmed' group by cus_name V. Join All Tables and Select only those tables whose Total is greater then 5000 p_name p_price Category Cus_name Quantity Total shop_name Keyboard 800 Computer Junaid 20 16000 Memon Communication Hardisk 1000 Computer Waseem 30 30000 Junaid Communication select p_name as 'Product Name',p_price as 'Product Price' ,Category.cat_name as 'Category Name', Customer.cus_name as 'Customer Name', Customer_Order.quantity as 'Quantity', shop.sh_name as 'Shop Name', Product.p_priceCustomer_Order.quantity as 'Total' from Product join Product_Category on Product_Category.p_id = Product.p_id join Category on Category.cat_id = Product_Category.cat_id join Customer_Order on Customer_Order.p_id = Product.p_id join Customer on Customer.cus_id = Customer_Order.cus_id join Shop_Products on Shop_Products.p_id = Product.p_id join Shop on Shop_Products.sh_id = Shop.sh_id where Product.p_price*Customer_Order.quantity > Cus_name Total Purchasing Ahmed 50000
QNo.4 Create Following Advance Quires (15 Marks) I. Count All Products that in Computer Category select count(p_name) from Product join Product_Category on Product_Category.p_id = Product.p_id join Category on Category.cat_id = Product_Category.cat_id where Category.cat_name = 'Computer II. Count All Products That is from Junaid Communication. select count(p_name) as 'Products' from Product join Shop_Products on Shop_Products.p_id = Product.p_id join Shop on shop.sh_id = Shop_Products.sh_id where shop.sh_name = 'Junaid Communication' III. Select All Customer That is Purchase Kiran Electronics Products select * from Customer join Customer_Order on Customer_Order.cus_id = Customer.cus_id join Product on Product.p_id = Customer_Order.p_id join Shop_Products on Shop_Products.p_id = Product.p_id join Shop on Shop.sh_id = Shop_Products.sh_id where Shop.sh_name = 'Kiran Electronics' IV. Count Products with Their Category
Example Execute my_procedure ‘ Techno Spark4 ’ create procedure CustomersProduct @InsertedProduct varchar(50) as select cus_name as 'Customer Name',@InsertedProduct as 'Purchased Product' from Customer join Customer_Order on Customer_Order.cus_id = Customer.cus_id join Product on Product.p_id = Customer_Order.p_id where p_name = @InsertedProduct go execute CustomersProduct'Processor II. Create Stored Procedure which accept input parameter Shop Name and return the Total Product of that shop. ( Must use Output Parameter in procedure) Output will be like Total Product of Kiran Communcation is 50 create procedure CustomersShop (@Output_values varchar(50) output) as select 'Total Product of '+@Output_values+' is '+convert(varchar(10),count(p_name)) from Product join Shop_Products on Shop_Products.p_id = Product.p_id join Shop on shop.sh_id = Shop_Products.sh_id where shop.sh_name = @Output_values go exec CustomersShop'Kiran Electronics' Customer Name Product Purchased Ahmed Techo Spark Junaid Techo Spark
III. Create Stored Procedure that accept one input parameter of category and show all the products, in as shown in below. Example: Execute my_procedure ‘Mobile’ Output will be like create procedure CategoryInput @a varchar(50) as select * from Product join Product_Category on Product_Category.p_id = Product.p_id join Category on Category.cat_id = Product_Category.cat_id where Category.cat_name = @a go exec CategoryInput'Computer Accessories' IV. Create new_product table same as product after create a Insert trigger on product table if any product is inserted first it check if it is not available in new_product table it insert in both product and new_product table if already inserted into new_product just show message ‘Product already inserted into new_product Table’ and only insert in product table. V. Use Merge Statement as product is source and new_product is target table and use all insert,update delete and output statement on merge statement.