mkottakota

im_q2

Oct 14th, 2018
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. 2)kay mart
  2. Kaymart Departmental Store has many products. A customer can buy many products
  3. from the store on a particular date.
  4. Product has product ID, product name, type, price.
  5. Customer has customer ID, name, contact number, address, email ID.
  6.  
  7. Draw ERD for the above case study
  8. Create tables according to ERD
  9. Write a SQL query to display the customer details for the given contact number.
  10. Write a SQL query display the customer details along with the details of the products bought for the given start date and end date.
  11. Write a Procedure to display the customer details and the total amount paid by the customers.
  12. create table customer(
  13. customer_id number(10)primary key,
  14. customer_name varchar2(20),
  15. customer_contact number(10),
  16. customer_email varchar2(20),
  17. customer_address varchar2(50)
  18. )
  19. insert into customer values (101,'Manikanta',9701641946,'mkottakota@gmail.com','Chennai');
  20. insert into customer values (102,'Hari',9988776655,'hari@gmail.com','Vizag');
  21. insert into customer values (103,'Kartik',7788991122,'kartik@gmail.com','Guntur');
  22. insert into customer values (104,'Sravs',6644558899,'sravs@gmail.com','Tamilnadu');
  23.  
  24. create table product (
  25. product_id number(10),
  26. product_name varchar2(20),
  27. product_type varchar2(20),
  28. product_price number(10),
  29. buy_date date,
  30. product_customer_id number(10) references customer(customer_id) );
  31.  
  32. insert into product values (1,'Pencil','Stationary',5,TO_DATE('19-01-2018','dd-mm-yyyy'),101);
  33. insert into product values (2,'Rexona','Soap',29,TO_DATE('23-01-2018','dd-mm-yyyy'),101);
  34. insert into product values (3,'Colgate','Paste',46,TO_DATE('01-02-2018','dd-mm-yyyy'),102);
  35. insert into product values (4,'Bag','General',230,TO_DATE('20-02-2018','dd-mm-yyyy'),103);
  36. insert into product values (5,'LorialParice','Shampoo',550,TO_DATE('28-03-2018','dd-mm-yyyy'),104);
  37.  
  38.  
  39. select * from product;
  40. PRODUCT_ID PRODUCT_NAME PRODUCT_TYPE PRODUCT_PRICE BUY_DATE PRODUCT_CUSTOMER_ID
  41. 1 Pencil Stationary 5 19-JAN-18 101
  42. 2 Rexona Soap 29 23-JAN-18 101
  43. 3 Colgate Paste 46 01-FEB-18 102
  44. 4 Bag General 230 20-FEB-18 103
  45. 5 LorialParice Shampoo 550 28-MAR-18 104
  46. Select * from customer;
  47. CUSTOMER_ID CUSTOMER_NAME CUSTOMER_CONTACT CUSTOMER_EMAIL CUSTOMER_ADDRESS
  48. 101 Manikanta 9701641946 mkottakota@gmail.com Chennai
  49. 102 Hari 9988776655 hari@gmail.com Vizag
  50. 103 Kartik 7788991122 kartik@gmail.com Guntur
  51. 104 Sravs 6644558899 sravs@gmail.com Tamilnadu
  52. 3)Write a SQL query to display the customer details for the given contact number.
  53. SQL> select * from customer where customer_contact=9701641946;
  54. CUSTOMER_ID CUSTOMER_NAME CUSTOMER_CONTACT CUSTOMER_EMAIL CUSTOMER_ADDRESS
  55. 101 Manikanta 9701641946 mkottakota@gmail.com Chennai
  56.  
  57. 4) Write a SQL query display the customer details along with the details of the products bought for the given start date and end date.
  58. SQL>select product.*,customer_name,customer_email,customer_address
  59. from product
  60. join customer
  61. on product_customer_id=customer_id
  62. where buy_date between TO_DATE('01-01-2018','dd-mm-yyyy') and TO_DATE('28-02-2018','dd-mm-yyyy');
  63. PRODUCT_ID PRODUCT_NAME PRODUCT_TYPE PRODUCT_PRICE BUY_DATE PRODUCT_CUSTOMER_ID CUSTOMER_NAME CUSTOMER_EMAIL CUSTOMER_ADDRESS
  64. 1 Pencil Stationary 5 19-JAN-18 101 Manikanta mkottakota@gmail.com Chennai
  65. 2 Rexona Soap 29 23-JAN-18 101 Manikanta mkottakota@gmail.com Chennai
  66. 3 Colgate Paste 46 01-FEB-18 102 Hari hari@gmail.com Vizag
  67. 4 Bag General 230 20-FEB-18 103 Kartik kartik@gmail.com Guntur
  68. 5)Write a Procedure to display the customer details and the total amount paid by the customers.
  69.  
  70. create or replace procedure manik008
  71. is
  72. cursor mycur is select customer_id,customer_name,sum(product_price) as total_amount
  73. from customer,product where customer_id = product_customer_id group by customer_id,customer_name;
  74. myrow mycur%rowtype;
  75. begin
  76. open mycur;
  77. loop
  78. fetch mycur into myrow;
  79. exit when mycur%notfound;
  80. dbms_output.put_line(myrow.customer_id||' '|| myrow.customer_name||' '||myrow.total_amount);
  81. end loop;
  82. close mycur;
  83. end manik008;
  84. /
  85. exec manik008;
  86. Procedure created.
  87. 104 Sravs 550
  88. 101 Manikanta 34
  89. 103 Kartik 230
  90. 102 Hari 46
  91. CUSTOMER_ID CUSTOMER_NAME TOTAL_AMOUNT
  92. 104 Sravs 550
  93. 101 Manikanta 34
  94. 103 Kartik 230
  95. 102 Hari 46
Add Comment
Please, Sign In to add comment