Guest User

Untitled

a guest
Nov 14th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. create table branch
  2. (
  3. branchname varchar(20) primary key,
  4. branchcity varchar(20),
  5. assets int
  6. );
  7.  
  8.  
  9. create table account
  10. (
  11. accno int primary key,
  12. branchname varchar(20),
  13. balance int,
  14. foreign key (branchname) references branch (branchname)
  15. );
  16.  
  17.  
  18.  
  19. create table customer
  20. (
  21. customername varchar(20) primary key,
  22. customerstreet varchar(20),
  23. customercity varchar(20)
  24.  
  25. );
  26.  
  27.  
  28.  
  29. create table depositor
  30. (
  31. customername varchar(20),
  32. accno int,
  33. primary key(customername,accno),
  34. foreign key(customername) references customer(customername),
  35. foreign key(accno) references account(accno) on delete cascade
  36. );
  37.  
  38.  
  39.  
  40. create table loan
  41. (
  42. loanno int,
  43. branchname varchar(20),
  44. amount real,
  45. primary key(loanno),
  46. foreign key(branchname) references branch(branchname)
  47. );
  48.  
  49.  
  50.  
  51. create table borrower
  52. (
  53. customername varchar(20),
  54. loanno int,
  55. primary key(customername,loanno),
  56. foreign key(customername) references customer(customername),
  57. foreign key(loanno) references loan(loanno)
  58. );
  59.  
  60. insert into branch values("basavanagudi","bangalore",25000000);
  61. insert into branch values("greenpark","NewDelhi", 50000000);
  62. insert into branch values("jayanagar", "bangalore", 50000000);
  63. insert into branch values("marinadrive", "mumbai", 55000000);
  64. insert into branch values("Noida", "NewDelhi", 35000000);
  65.  
  66.  
  67.  
  68. insert into account values( 105, "jayanagar",45000);
  69. insert into account values( 205, "jayanagar",50000);
  70. insert into account values( 250, "basavanagudi",40000);
  71. insert into account values( 450, "basavanagudi",20000);
  72. insert into account values( 458, "noida",25000);
  73. insert into account values( 780, "greenpark",40000);
  74. insert into account values( 900, "marinadrive",100000);
  75.  
  76.  
  77. insert into customer values("john","noida","newdelhi");
  78. insert into customer values("kumar","basavanagudi","bangalore");
  79. insert into customer values("raju","jayanagar","bangalore");
  80. insert into customer values("smith","highhills","newdelhi");
  81. insert into customer values("sujal","kuvempu","bangalore");
  82.  
  83.  
  84.  
  85.  
  86. insert into depositor values("john",105);
  87. insert into depositor values("john",250);
  88. insert into depositor values("kumar",250);
  89. insert into depositor values("kumar",450);
  90. insert into depositor values("raju",458);
  91. insert into depositor values("smith",780);
  92. insert into depositor values("sujal",900);
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. insert into loan values( 1002, "jayanagar" ,255000);
  101. insert into loan values( 1003, "basavanagudi",555000);
  102. insert into loan values( 1004, "noida",500000);
  103. insert into loan values( 1005, "marinadrive",400000);
  104. insert into loan values( 1006, "greenpark",600000);
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. insert into borrower values("john",1002);
  116. insert into borrower values("kumar",1003);
  117. insert into borrower values("raju",1004);
  118. insert into borrower values("smith",1005);
  119. insert into borrower values("sujal",1006);
  120.  
  121. 3rd Query:
  122.  
  123. select d.customername from depositor d,account a where d.accno=a.accno group by d.customername having count(d.customername)>=2;
  124.  
  125.  
  126.  
  127.  
  128. 4th Query:
  129.  
  130. create view cityb as (select branchname from branch where branchcity="bangalore");
  131.  
  132.  
  133. select distinct d.customername from depositor d, account a, cityb c where d.accno=a.accno and a.branchname=c.branchname group by d.accno;
  134.  
  135.  
  136. 5th query:
  137.  
  138.  
  139. delete from account where branchname in (select b.branchname from branch b where b.branchcity="bangalore");
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. <?php
  147. $servername = "localhost";
  148. $username = "root";
  149. $password = "jit123";
  150. $dbname = "bank";
  151.  
  152. $conn = new mysqli($servername, $username, $password, $dbname);
  153.  
  154. if ($conn->connect_error)
  155. {
  156. die("connection failed: " . $conn->connect_error);
  157. }
  158. else
  159. {
  160. echo"<p align=\"center\"><b>1.Find all customers who have atleast 2 accounts at the main branch<br><br></b>";
  161.  
  162. echo"<p align=\"center\"><b>QUERY :</b> select d.customername from depositor d,account a where d.accno=a.accno group by d.customername having count(d.customername)>=2;<br><br>";
  163.  
  164. $sqlQuery = "select d.customername from depositor d,account a where d.accno=a.accno group by d.customername having count(d.customername)>=2;";
  165. $result = $conn->query($sqlQuery);
  166. if($result->num_rows > 0)
  167. {
  168. echo "<table align=\"center\"border=10><tr><th>Customer Name</th>";
  169. echo "</tr>";
  170. while($row = $result->fetch_assoc())
  171. {
  172. echo"<tr>";
  173. echo "<td>$row[customername]</td>";
  174. echo "</tr>";
  175. }
  176. echo "</table><br><br><br>";
  177. }
  178. else
  179. echo"Empty set";
  180.  
  181.  
  182. echo "<b><p align=\"center\">2.Find all customers who have an account at all the branches located in a specific city.<br><br></b>";
  183.  
  184. echo "<p align=\"center\"><b>QUERY :</b> select d.customername,count(distinct b.branchname) from account a,depositor d,branch b where a.accno=d.accno and b.branchname=a.branchname and b.branchcity='bangalore' group by d.customername having count(distinct b.branchname)=(select count(distinct b.branchname) from branch b where b.branchcity='bangalore');<br><br>";
  185.  
  186. $sqlQuery = "select d.customername,count(distinct b.branchname) as cnt from account a,depositor d,branch b where a.accno=d.accno and b.branchname=a.branchname and b.branchcity='bangalore' group by d.customername having count(distinct b.branchname)=(select count(distinct b.branchname) from branch b where b.branchcity='bangalore')";
  187. $result = $conn->query($sqlQuery);
  188. if($result->num_rows > 0)
  189. {
  190. echo "<table align=\"center\"border=10><tr><th>Customer Name</th>";
  191. echo "<th>Count branchname</th>";
  192. echo "</tr>";
  193. while($row = $result->fetch_assoc())
  194. {
  195. echo"<tr>";
  196. echo "<td>$row[customername]</td>";
  197. echo"<td>$row[cnt]</td>";
  198. echo "</tr>";
  199. }
  200. echo "</table><br><br><br>";
  201. }
  202. else
  203. echo"Empty set";
  204.  
  205.  
  206. $sqlQuery="delete from account where bname in(select bname from branch where bcity='mumbai')";
  207. $result=$conn->query($sqlQuery);
  208.  
  209. echo "<p align=\"center\"><b>3.Demonstrate how you delete all account tuples at ever branch located in a specific city<br><br><br></b>";
  210.  
  211. echo "<p align=\"center\"><b>QUERY :</b>delete from account where branchname in(select branchname from branch where branchcity='mumbai')<br><br></b>";
  212.  
  213. $sqlQuery="select * from account";
  214. $result=$conn->query($sqlQuery);
  215.  
  216. if($result->num_rows > 0)
  217. {
  218. echo "<table align=\"center\"border=10><tr><th>Account Number</th>";
  219. echo "<th>Branch Name</th>";
  220. echo "<th>Balance</th>";
  221. echo "</tr>";
  222. while($row = $result->fetch_assoc())
  223. {
  224. echo"<tr>";
  225. echo "<td>$row[accno]</td>";
  226. echo"<td>$row[branchname]</td>";
  227. echo"<td>$row[balance]</td>";
  228. echo "</tr>";
  229. }
  230. echo "</table>";
  231. }
  232. else
  233. echo"Empty set";
  234. }
  235. $conn->close();
  236. ?>
Add Comment
Please, Sign In to add comment