Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table branch
- (
- branchname varchar(20) primary key,
- branchcity varchar(20),
- assets int
- );
- create table account
- (
- accno int primary key,
- branchname varchar(20),
- balance int,
- foreign key (branchname) references branch (branchname)
- );
- create table customer
- (
- customername varchar(20) primary key,
- customerstreet varchar(20),
- customercity varchar(20)
- );
- create table depositor
- (
- customername varchar(20),
- accno int,
- primary key(customername,accno),
- foreign key(customername) references customer(customername),
- foreign key(accno) references account(accno) on delete cascade
- );
- create table loan
- (
- loanno int,
- branchname varchar(20),
- amount real,
- primary key(loanno),
- foreign key(branchname) references branch(branchname)
- );
- create table borrower
- (
- customername varchar(20),
- loanno int,
- primary key(customername,loanno),
- foreign key(customername) references customer(customername),
- foreign key(loanno) references loan(loanno)
- );
- insert into branch values("basavanagudi","bangalore",25000000);
- insert into branch values("greenpark","NewDelhi", 50000000);
- insert into branch values("jayanagar", "bangalore", 50000000);
- insert into branch values("marinadrive", "mumbai", 55000000);
- insert into branch values("Noida", "NewDelhi", 35000000);
- insert into account values( 105, "jayanagar",45000);
- insert into account values( 205, "jayanagar",50000);
- insert into account values( 250, "basavanagudi",40000);
- insert into account values( 450, "basavanagudi",20000);
- insert into account values( 458, "noida",25000);
- insert into account values( 780, "greenpark",40000);
- insert into account values( 900, "marinadrive",100000);
- insert into customer values("john","noida","newdelhi");
- insert into customer values("kumar","basavanagudi","bangalore");
- insert into customer values("raju","jayanagar","bangalore");
- insert into customer values("smith","highhills","newdelhi");
- insert into customer values("sujal","kuvempu","bangalore");
- insert into depositor values("john",105);
- insert into depositor values("john",250);
- insert into depositor values("kumar",250);
- insert into depositor values("kumar",450);
- insert into depositor values("raju",458);
- insert into depositor values("smith",780);
- insert into depositor values("sujal",900);
- insert into loan values( 1002, "jayanagar" ,255000);
- insert into loan values( 1003, "basavanagudi",555000);
- insert into loan values( 1004, "noida",500000);
- insert into loan values( 1005, "marinadrive",400000);
- insert into loan values( 1006, "greenpark",600000);
- insert into borrower values("john",1002);
- insert into borrower values("kumar",1003);
- insert into borrower values("raju",1004);
- insert into borrower values("smith",1005);
- insert into borrower values("sujal",1006);
- 3rd Query:
- select d.customername from depositor d,account a where d.accno=a.accno group by d.customername having count(d.customername)>=2;
- 4th Query:
- create view cityb as (select branchname from branch where branchcity="bangalore");
- 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;
- 5th query:
- delete from account where branchname in (select b.branchname from branch b where b.branchcity="bangalore");
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "jit123";
- $dbname = "bank";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- {
- die("connection failed: " . $conn->connect_error);
- }
- else
- {
- echo"<p align=\"center\"><b>1.Find all customers who have atleast 2 accounts at the main branch<br><br></b>";
- 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>";
- $sqlQuery = "select d.customername from depositor d,account a where d.accno=a.accno group by d.customername having count(d.customername)>=2;";
- $result = $conn->query($sqlQuery);
- if($result->num_rows > 0)
- {
- echo "<table align=\"center\"border=10><tr><th>Customer Name</th>";
- echo "</tr>";
- while($row = $result->fetch_assoc())
- {
- echo"<tr>";
- echo "<td>$row[customername]</td>";
- echo "</tr>";
- }
- echo "</table><br><br><br>";
- }
- else
- echo"Empty set";
- 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>";
- 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>";
- $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')";
- $result = $conn->query($sqlQuery);
- if($result->num_rows > 0)
- {
- echo "<table align=\"center\"border=10><tr><th>Customer Name</th>";
- echo "<th>Count branchname</th>";
- echo "</tr>";
- while($row = $result->fetch_assoc())
- {
- echo"<tr>";
- echo "<td>$row[customername]</td>";
- echo"<td>$row[cnt]</td>";
- echo "</tr>";
- }
- echo "</table><br><br><br>";
- }
- else
- echo"Empty set";
- $sqlQuery="delete from account where bname in(select bname from branch where bcity='mumbai')";
- $result=$conn->query($sqlQuery);
- 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>";
- echo "<p align=\"center\"><b>QUERY :</b>delete from account where branchname in(select branchname from branch where branchcity='mumbai')<br><br></b>";
- $sqlQuery="select * from account";
- $result=$conn->query($sqlQuery);
- if($result->num_rows > 0)
- {
- echo "<table align=\"center\"border=10><tr><th>Account Number</th>";
- echo "<th>Branch Name</th>";
- echo "<th>Balance</th>";
- echo "</tr>";
- while($row = $result->fetch_assoc())
- {
- echo"<tr>";
- echo "<td>$row[accno]</td>";
- echo"<td>$row[branchname]</td>";
- echo"<td>$row[balance]</td>";
- echo "</tr>";
- }
- echo "</table>";
- }
- else
- echo"Empty set";
- }
- $conn->close();
- ?>
Add Comment
Please, Sign In to add comment