0% found this document useful (0 votes)
8 views

Experiment8 Dbms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Experiment8 Dbms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name - Sourabh Suresh Bandgar

Roll no - 10
Title: Views, Constraints and Subqueries

mysql> create database Exp8;


Query OK, 1 row affected (0.01 sec)

mysql> use Exp8;


Database changed
mysql> create table loan(loan_no int primary key,branchname
varchar(50), amount int);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into loan values


-> (363,'Kolhapur',35000),
-> (797,'Sangli',8578),
-> (825,'Latur',7845);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> create table depositor(customerName varchar(50),accountNumber


int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into depositor values


-> ('Shreyas',12),
-> ('Sourabh',53),
-> ('Sharad',47),
-> ('Uddhav',53),
-> ('Narendra',45),
-> ('Rahul',49),
-> ('Arvind',54);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> create table borrower(CustomerName varchar(50),loanNumber


int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into borrower values


-> ('Omkar',345),
-> ('Ayush',797),
-> ('Avishkar',586),
-> ('Raviraj',825),
-> ('Viraj',363);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> create table account(accountNumber int primary Key,branchname


varchar
(50),balance int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into account values
-> (12,'Kolhapur',5454),
-> (16,'Latur',8792),
-> (32,'Rajarampuri',2525),
-> (45,'Latur',5647),
-> (47,'Latur',3853),
-> (49,'Latur',5315),
-> (53,'Shahupuri',4586),
-> (67,'Ichalkaranji',7474);
Query OK, 8 rows affected (0.04 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> select * from account;


+---------------+--------------+---------+
| accountNumber | branchname | balance |
+---------------+--------------+---------+
| 12 | Kolhapur | 5454 |
| 16 | Latur | 8792 |
| 32 | Rajarampuri | 2525 |
| 45 | Latur | 5647 |
| 47 | Latur | 3853 |
| 49 | Latur | 5315 |
| 53 | Shahupuri | 4586 |
| 67 | Ichalkaranji | 7474 |
+---------------+--------------+---------+
8 rows in set (0.01 sec)

mysql> select * from borrower;


+--------------+------------+
| CustomerName | loanNumber |
+--------------+------------+
| Omkar | 345 |
| Ayush | 797 |
| Avishkar | 586 |
| Raviraj | 825 |
| Viraj | 363 |
+--------------+------------+
5 rows in set (0.00 sec)

mysql> select * from depositor;


+--------------+---------------+
| customerName | accountNumber |
+--------------+---------------+
| Shreyas | 12 |
| Sourabh | 53 |
| Sharad | 47 |
| Uddhav | 53 |
| Narendra | 45 |
| Rahul | 49 |
| Arvind | 54 |
+--------------+---------------+
7 rows in set (0.00 sec)

mysql> select * from loan;


+---------+------------+--------+
| loan_no | branchname | amount |
+---------+------------+--------+
| 363 | Kolhapur | 35000 |
| 797 | Sangli | 8578 |
| 825 | Latur | 7845 |
+---------+------------+--------+
3 rows in set (0.00 sec)

1.Create a view ‘all-customers’ consisting of branch names and the


names of customers who have either an account or a loan or both.

mysql> create view all_customers as select


depositor.customerName,account.branchname from depositor natural
join account union select borrower.customername,loan.branchname from
borrower natural join loan;
Query OK, 0 rows affected (0.03 sec)

mysql> select * from all_customers;


+--------------+------------+
| customerName | branchname |
+--------------+------------+
| Shreyas | Kolhapur |
| Sourabh | Shahupuri |
| Sharad | Latur |
| Uddhav | Shahupuri |
| Narendra | Latur |
| Rahul | Latur |
| Omkar | Latur |
| Omkar | Sangli |
| Omkar | Kolhapur |
| Ayush | Latur |
| Ayush | Sangli |
| Ayush | Kolhapur |
| Avishkar | Latur |
| Avishkar | Sangli |
| Avishkar | Kolhapur |
| Raviraj | Latur |
| Raviraj | Sangli |
| Raviraj | Kolhapur |
| Viraj | Latur |
| Viraj | Sangli |
| Viraj | Kolhapur |
+--------------+------------+
21 rows in set (0.01 sec)

2.Using view ‘all-customers’, list in alphabetic order, the


customers of ‘Latur’ branch.

mysql> SELECT customerName


-> FROM all_customers
-> WHERE branchName = 'Latur'
-> ORDER BY customerName;
+--------------+
| customerName |
+--------------+
| Avishkar |
| Ayush |
| Narendra |
| Omkar |
| Rahul |
| Raviraj |
| Sharad |
| Viraj |
+--------------+
8 rows in set (0.01 sec)

3.Create a view consisting of sum of the amounts of all the loans


for each branch.

mysql> CREATE VIEW Total AS


-> SELECT branchName, SUM(amount)
-> FROM Loan
-> GROUP BY branchName;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from Total;


+------------+-------------+
| branchName | SUM(amount) |
+------------+-------------+
| Kolhapur | 35000 |
| Sangli | 8578 |
| Latur | 7845 |
+------------+-------------+
3 rows in set (0.01 sec)

You might also like