You are on page 1of 11

Lab 4

Data Definition and Data Manipulation

CSE 4308

DATABASE MANAGEMENT SYSTEMS LAB

SUBMITTED BY: MD. NAZMUS SADIQ


STUDENT ID: 210041139
SECTION: 1(A)
CSE
**Task Description**:

The task involved creating a SQL database schema to represent a banking system, populating the tables
with sample data, and executing a series of SQL queries to retrieve and manipulate data. Additionally,
the task required providing a detailed report outlining the approach, SQL commands used, code snippets
from the solution, and any challenges encountered during the process.

**Solution Approach**:

1. **Database Cleanup**:

- Initially, existing tables (if any) related to a previous database schema were dropped with cascading
constraints to ensure a clean slate for the new schema.

drop table depositor cascade constraints;

drop table borrower cascade constraints;

drop table account cascade constraints;

drop table loan cascade constraints;

drop table customer cascade constraints;

drop table branch cascade constraints;

2. **Table Creation**:

- The schema for the banking system was created with six tables: `branch`, `customer`, `account`,
`loan`, `depositor`, and `borrower`. Each table was defined with the necessary columns, primary keys,
and foreign key constraints where applicable.
create table branch (

branch_name varchar(15) not null,


branch_city varchar(15) not null,
assets number not null,
primary key(branch_name)
);

create table customer (


customer_name varchar(15) not null,
customer_street varchar(12) not null,
customer_city varchar(15) not null,
primary key(customer_name)
);

create table account (


account_number varchar(15) not null,
branch_name varchar(15) not null,
balance number not null,
primary key(account_number),
foreign key(branch_name) references branch(branch_name)
);

create table loan (


loan_number varchar(15) not null,
branch_name varchar(15) not null,
amount number not null,
primary key(loan_number),
foreign key(branch_name) references branch(branch_name)
);

create table depositor (


customer_name varchar(15) not null,
account_number varchar(15) not null,
primary key(customer_name, account_number),
foreign key(account_number) references account(account_number),
foreign key(customer_name) references customer(customer_name)
);
create table borrower (
customer_name varchar(15) not null,
loan_number varchar(15) not null,
primary key(customer_name, loan_number),
foreign key(customer_name) references customer(customer_name),
foreign key(loan_number) references loan(loan_number)
);
3. **Data Population**:

- Sample data was inserted into the tables to simulate a simplified banking system, including branches,
customers, accounts, loans, depositors, and borrowers.

For Example-

insert into
customer
values
('Jones', 'Main', 'Harrison');
insert into
branch
values
('Redwood', 'Palo Alto', 2100000);
insert into
account
values
('A-102', 'Perryridge', 400);
insert into
depositor
values
('Smith', 'A-215');
insert into
loan
values
('L-15', 'Perryridge', 1500);

insert into
borrower
values
('Hayes', 'L-15');

4. **SQL Queries**:

- A variety of SQL queries were executed to retrieve specific information from the database. These
queries included filtering, joining tables, and calculating aggregates.
SELECT
CUSTOMER_CITY
FROM
CUSTOMER
WHERE
CUSTOMER_CITY LIKE '%f%d';

SELECT
DISTINCT c.CUSTOMER_NAME,
c.CUSTOMER_CITY
FROM
CUSTOMER c,
borrower b,
depositor d
WHERE
c.CUSTOMER_NAME = b.customer_name
AND c.CUSTOMER_NAME != d.customer_name;

SELECT
DISTINCT c.CUSTOMER_NAME,
c.customer_city,
c.CUSTOMER_CITY
FROM
CUSTOMER c,
borrower b,
depositor d
WHERE
c.CUSTOMER_NAME = b.customer_name
OR c.CUSTOMER_NAME = d.customer_name;

SELECT
DISTINCT count(assets) as Count
from
branch;

SELECT
branch_city,
count(assets) as Count
from
branch
group by
branch_city;

SELECT
d.customer_name,
d.account_number
FROM
depositor d,
account a
WHERE
d.account_number = a.account_number
AND a.balance = (
SELECT
MAX(balance)
FROM
account
);

SELECT
b.branch_name,
AVG(a.balance) AS avg_balance
FROM
branch b,
account a
WHERE
b.branch_name = a.branch_name
GROUP BY
b.branch_name
ORDER BY
b.branch_name ASC,
avg_balance DESC;

SELECT
c.customer_city,
COUNT(DISTINCT c.customer_name) AS num_customers
FROM
customer c,
depositor d,
borrower b
WHERE
c.customer_name = d.customer_name
AND c.customer_name = b.customer_name
GROUP BY
c.customer_city;

SELECT
b.branch_name,
AVG(l.amount) AS avg_loan_amount
FROM
branch b,
loan l
WHERE
b.branch_name = l.branch_name
AND b.branch_name NOT LIKE '%Horse%'
GROUP BY
b.branch_name;

SELECT
DISTINCT c.*
FROM
customer c,
depositor d,
account a,
branch b
where
c.customer_name = d.customer_name
AND d.account_number = a.account_number
AND a.branch_name = b.branch_name
AND c.customer_city = b.branch_city;

SELECT
a.branch_name
FROM
account a
GROUP BY
a.branch_name
HAVING
SUM(a.balance) > (
SELECT
AVG(total_balance)
FROM
(
SELECT
SUM(balance) AS total_balance
FROM
account
GROUP BY
branch_name
)
);

SELECT
b2.branch_name
FROM
branch b2,
account a2,
customer c2,
depositor d2,
loan l2,
borrower br2
WHERE
b2.branch_name = a2.branch_name
AND a2.account_number = d2.account_number
AND d2.customer_name = c2.customer_name
AND b2.branch_name = l2.branch_name
AND l2.loan_number = br2.loan_number
GROUP BY
b2.branch_name
HAVING
SUM(a2.balance) > SUM(l2.amount);

SELECT
DISTINCT c.customer_name
FROM
customer c,
depositor d,
account a,
borrower b,
loan l
where
c.customer_name = d.customer_name
AND d.account_number = a.account_number
AND c.customer_name = b.customer_name
AND b.loan_number = l.loan_number
AND a.balance >= l.amount;

SELECT
c.customer_name,
a.balance,
l.amount
FROM
customer c,
account a,
depositor d,
loan l,
borrower b,
branch br
WHERE
c.customer_name = d.customer_name
AND d.account_number = a.account_number
AND c.customer_name = b.customer_name
AND b.loan_number = l.loan_number
AND a.branch_name != br.branch_name
AND c.customer_city != br.branch_city;

SELECT
DISTINCT c.customer_name
FROM
customer c,
borrower b,
loan l,
account a,
depositor d
WHERE
(
c.customer_name = d.customer_name
OR c.customer_name = b.customer_name
)
AND (
d.account_number = a.account_number
OR b.loan_number = l.loan_number
)
AND ((
a.branch_name
) IN (
SELECT
branch_name
FROM
branch
WHERE
assets = (
SELECT
MAX(assets)
FROM
branch
)
)
OR l.branch_name IN (
SELECT
branch_name
FROM
branch
WHERE
assets = (
SELECT
MAX(assets)
FROM
branch
)
)
);

5. **Committing Transactions**:

- A `COMMIT;` statement was used to finalize any changes made to the database, ensuring data
consistency.

commit;

**Challenges Faced**:

- **Data Integrity**: Ensuring that primary keys and foreign keys were correctly defined and that data
remained consistent and valid throughout the process was a key challenge.

- **Complex Queries**: Crafting SQL queries that involved multiple tables and complex conditions
required careful consideration of table relationships and data types. Debugging such queries can be
challenging.

- **Cascade Constraints**: Managing cascading deletes and updates when working with foreign keys
was crucial to prevent unintended data loss.

- **Data Populating**: While data population was straightforward in this simplified scenario,
maintaining data accuracy and consistency in a real-world system would be more complex.

- **Data Filtering**: Writing SQL queries to filter specific data based on criteria required a clear
understanding of SQL syntax and logical operators.
- **Data Retrieval and Aggregation**: Aggregating data and retrieving distinct records required a good
understanding of SQL functions and clauses.

- **Code Organization**: Keeping the SQL code organized and well-commented to explain the purpose
of each query was important for readability and maintainability.

- **Performance Optimization**: In a real-world scenario with larger datasets, performance


optimization would be essential to ensure efficient query execution.

This task demonstrated the process of designing a database schema, populating it with data, and
performing SQL operations to extract meaningful information, all while considering data integrity and
query efficiency.

You might also like