Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. create database buyerdb;
  2. use buyerdb;
  3. CREATE TABLE buyer
  4. (
  5. buyer_id int not null key auto_increment,
  6. buyer_name varchar(20) not null
  7. );
  8. CREATE TABLE product
  9. (
  10. product_id int not null primary key auto_increment,
  11. product_name varchar(20) not null
  12. );
  13. CREATE TABLE orders
  14. (
  15. order_id int not null primary key auto_increment,
  16. buyer_id int not null,
  17. product_id int not null,
  18. qty int not null
  19. );
  20. alter table orders
  21. add foreign key (buyer_id)
  22. references buyer (buyer_id);
  23. alter table orders
  24. add foreign key (product_id)
  25. references product (product_id);
  26.  
  27. insert into buyer values (1,'ABC Trade');
  28. insert into buyer values (2,'DEF Logistics');
  29.  
  30. insert into product values (1,'Watermelon');
  31. insert into product values (2,'Cabbage');
  32. insert into product values (3,'Banana');
  33.  
  34. insert into orders values (1,1,1,20);
  35. insert into orders values (2,1,1,15);
  36. insert into orders values (3,1,2,150);
  37. insert into orders values (4,1,3,300);
  38. insert into orders values (5,1,3,100);
  39. insert into orders values (6,2,1,30);
  40. insert into orders values (7,2,2,100);
  41. insert into orders values (8,2,2,120);
  42. insert into orders values (9,2,3,150);
  43. insert into orders values (10,2,3,50);
  44.  
  45. select * from buyer;
  46. select * from product;
  47. select * from orders;
  48.  
  49. select *
  50. from buyer cross join orders;
  51.  
  52. select
  53. buyer.buyer_id as 'Buyer ID',
  54. buyer.buyer_name,
  55. orders.order_id,
  56. orders.qty
  57. from orders
  58. inner join buyer
  59. on orders.buyer_id=buyer.buyer_id;
  60.  
  61. select
  62. orders.order_id,
  63. buyer.buyer_name,
  64. product.product_name,
  65. orders.qty
  66. from orders
  67. inner join buyer on orders.buyer_id=buyer.buyer_id
  68. inner join product on orders.product_id=product.product_id;
  69.  
  70. select
  71. product.product_id,
  72. product.product_name,
  73. orders.order_id,
  74. orders.buyer_id,
  75. orders.qty
  76. from product
  77. left outer join orders
  78. on product.product_id=orders.product_id;
  79.  
  80. select
  81. product.product_id,
  82. product.product_name,
  83. orders.order_id,
  84. orders.buyer_id,
  85. orders.qty
  86. from product
  87. left outer join orders
  88. on product.product_id=orders.product_id
  89. and product.product_name<>'Watermelon';
  90.  
  91. select product_id, buyer_id, qty
  92. from orders
  93. order by buyer_id, product_id;
  94.  
  95. select buyer_id, product_id, sum(qty)
  96. from orders
  97. group by buyer_id, product_id
  98. order by buyer_id, product_id;
  99.  
  100. select buyer_id, product_id, sum(qty)
  101. from orders
  102. group by buyer_id, product_id
  103. having sum(qty)>=200
  104. order by buyer_id, product_id;
  105.  
  106. -- drop table orders;
  107. -- drop table buyer;
  108. -- drop table product;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement