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

SQL

The project involves creating a database for managing financial data. It will contain 8 tables: Customers, Accounts, Currency, TransactionTypes, BankTransactions, InvestmentTransactions, Investments, and CreditScores. The BankTransactions and InvestmentTransactions tables are fact tables that contain transaction records, while the others contain reference data like customer, account, and investment details. Sample data is inserted into the Customers and InvestmentTransactions tables. Views are created for CustomerAccountBalances, InvestmentPerformance, and RecentBankTransactions to conveniently present account balances, investment performance metrics, and recent bank transactions to users.

Uploaded by

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

SQL

The project involves creating a database for managing financial data. It will contain 8 tables: Customers, Accounts, Currency, TransactionTypes, BankTransactions, InvestmentTransactions, Investments, and CreditScores. The BankTransactions and InvestmentTransactions tables are fact tables that contain transaction records, while the others contain reference data like customer, account, and investment details. Sample data is inserted into the Customers and InvestmentTransactions tables. Views are created for CustomerAccountBalances, InvestmentPerformance, and RecentBankTransactions to conveniently present account balances, investment performance metrics, and recent bank transactions to users.

Uploaded by

iqbal.junaid9991
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Question 01: Pick any area of interest you wish to create a database system

(example: Energy, Agriculture, Finance, Biomedical, Entertainment, Gaming,


Sports, Education, Sociology, Physics, Gene splicing, Scuba Diving, etc.).

I select the Finance for which I created the database.

Question 02: You will create between 6 to 10 tables (Not more than 10 tables) that
will store information you wish to keep track of.

I create the 8 tables related to the financial database in which 6 tables are dimension table and 2
tables are fact or transactional tables.

Question 03: Provide details about the project, explaining what sort of information
in a database explaining the use of each table and why you selected this project. –
(10 marks).

The project appears to be related to a financial database, and it consists of several tables and
associated operations. Here's a summary of the project: The project involves creating a database
called "Finance." It seems to be designed to manage financial data, possibly for a bank or
financial institution.

Database Tables:

 Customers: This table stores customer information, including their first name, last name,
address, and phone number.
 Accounts: It contains details about the accounts associated with customers, such as the
account type and balance.
 Currency: This table appears to hold currency-related data, such as currency codes and
names.
 TransactionTypes: Stores types of transactions, which could be used to categorize
financial transactions.
 BankTransactions: A fact table for bank transactions, including customer, account,
transaction type, date, and amount.
 InvestmentTransactions: Similar to BankTransactions but for investment-related
transactions. It is also a fact table.
 Investments: Contains data about investments, including type, purchase date, amount,
and current value.
 CreditScores: Records credit scores and ratings for customers.

Question 04: Most of the tables would be dimension (lookup) tables. You must have
at least one or two fact tables (transactional tables).

I create the two fact or transactional tables BankTransactions and IvestnebtTransaction and the
queries are given below:

-- Bank Transactions Fact Table


CREATE TABLE BankTransactions (
TransactionID INT PRIMARY KEY,
CustomerID INT,
AccountID INT,
TransactionTypeID INT,
TransactionDate DATE,
Amount DECIMAL(10, 2)
);

-- Investment Transactions Fact Table


CREATE TABLE InvestmentTransactions (
InvestmentTransactionID INT PRIMARY KEY,
CustomerID INT,
TransactionTypeID INT,
TransactionDate DATE,
Amount DECIMAL(10, 2)
);

Question 05: Populate the table with 10 rows for dimension tables, 20 – 50 rows for
transactional tables.

I insert the 10 rows and 20 rows in the tables and the code is given in the file. Only shows
the customer dimension table and the InvestmentTranscations table and the rest of query
are given in the sql file .

-- Sample data for Customers Table (Dimension)


INSERT INTO Customers (CustomerID, FirstName, LastName, Address, Phone)
VALUES
(1, 'John', 'Doe', '123 Main St', '555-123-45671'),
(2, 'Jane', 'Smith', '456 Elm St', '555-987-65432'),
(3, 'cena', 'tip', '1234 Main St', '555-123-4522'),
(4, 'kurt', 'ko', '1235 Main St', '555-123-4327'),
(5, 'srk', 'ledf', '1236 Main St', '555-123-2367'),
(6, 'samy', 'kjnc', '1237 Main St', '555-123-5567'),
(7, 'Mess', 'youv', '1238 Main St', '555-123-4565'),
(8, 'ripon', 'trice', '1239 Main St', '555-123-4500'),
(9, 'kofa', 'joh', '12399 Main St', '555-123-4533'),
(10, 'doga', 'fore', '123900 Main St', '555-563-4567');

-- Sample data for Investment Transactions Fact Table


INSERT INTO InvestmentTransactions (InvestmentTransactionID, CustomerID,
TransactionTypeID, TransactionDate, Amount)
VALUES
(1, 1, 1, '2023-01-20', 5000.00),
(2, 2, 1, '2023-02-05', 3000.00),
(3, 1, 1, '2023-01-20', 5000.00),
(4, 2, 1, '2023-02-05', 3000.00),
(5, 3, 1, '2023-01-20', 5000.00),
(6, 4, 1, '2023-02-05', 3000.00),
(7, 5, 1, '2023-01-20', 5000.00),
(8, 6, 1, '2023-02-05', 3000.00),
(9, 7, 1, '2023-01-20', 5000.00),
(10, 2, 1, '2023-02-05', 3000.00),
(11, 1, 1, '2023-01-20', 5000.00),
(12, 2, 1, '2023-02-05', 3000.00),
(13, 1, 1, '2023-01-20', 5000.00),
(14, 2, 1, '2023-02-05', 3000.00),
(15, 3, 1, '2023-01-20', 5000.00),
(16, 4, 1, '2023-02-05', 3000.00),
(17, 5, 1, '2023-01-20', 5000.00),
(18, 6, 1, '2023-02-05', 3000.00),
(19, 7, 1, '2023-01-20', 5000.00),
(20, 2, 1, '2023-02-05', 3000.00);

Question 06: Generate the ERD diagram and paste it in a word document.
Accounts * FK_A ccounts_Inv estmentTransactions InvestmentTransactions *
AccountID InvestmentTransactionID

CustomerID CustomerID

AccountType TransactionTypeID

Balance FK_TransactionTy pes_Inv estmentTransactions TransactionDate

Amount

CreditScores * TransactionTypes *
CreditScoreID
FK_C ustomers_C reditScores TransactionTypeID
CustomerID FK_C ustomers_A ccounts FK_Inv estmentTransactions_Inv estments
TypeName
CreditScore

CreditRating Customers *
CustomerID Investments *
FirstName CustomerID
FK_C reditScores_C urrency FK_TransactionTy pes_BankTransactions
LastName InvestmentType

Address InvestmentAmount

Currency * Phone BankTransactions * PurchaseDate


CurrencyID TransactionID CurrentValue
CurrencyCode CustomerID

CurrencyName AccountID

TransactionTypeID

TransactionDate

Amount

Question 07: Create three queries and convert them into views and explain why you
think would be useful to the user.

I will create three sample SQL queries and convert them into views for your financial database
system. There are several views defined, such as "CustomerAccountBalances," which provides a
combined view of customer and account data. "InvestmentPerformance" seems to be related to
investments. "RecentBankTransactions" provides a view of recent bank transactions.

 Customer Account Balances View


-- Create a view for Customer Account Balances

-- Create a view for Customer Account Balances


CREATE VIEW CustomerAccountBalances AS
SELECT
C.CustomerID,
CONCAT(C.FirstName, ' ', C.LastName) AS CustomerName,
A.AccountID,
A.AccountType,
A.Balance
FROM Customers C
JOIN Accounts A ON C.CustomerID = A.CustomerID;
This view provides users with a convenient way to see the account balances of each customer. It
allows them to quickly check the balances for all customer accounts, helping with customer
service and financial planning.

 Investment Performance View

CREATE VIEW InvestmentPerformance AS


SELECT
I.CustomerID,
CONCAT(C.FirstName, ' ', C.LastName) AS CustomerName,
I.InvestmentID,
I.InvestmentType,
I.PurchaseDate,
I.CurrentValue,
CASE
WHEN I.InvestmentAmount = 0 THEN 0 -- To handle division by zero
ELSE ((I.CurrentValue - I.InvestmentAmount) / I.InvestmentAmount) * 100
END AS ROI_Percentage
FROM Investments I
JOIN Customers C ON I.CustomerID = C.CustomerID;

This view provides users with an overview of their investments, including the type of
investment, purchase date, current value, and the calculated Return on Investment (ROI)
percentage. It helps users assess the performance of their investments.

 Recent Bank Transactions View

CREATE VIEW RecentBankTransactions AS


SELECT
B.TransactionID,
CONCAT(C.FirstName, ' ', C.LastName) AS CustomerName,
A.AccountType,
TT.TypeName AS TransactionType,
B.TransactionDate,
B.Amount
FROM BankTransactions B
JOIN Customers C ON B.CustomerID = C.CustomerID
JOIN Accounts A ON B.AccountID = A.AccountID
JOIN TransactionTypes TT ON B.TransactionTypeID = TT.TransactionTypeID
WHERE B.TransactionDate >= DATEADD(DAY, -30, GETDATE());

This view displays recent bank transactions for the past 30 days, providing users with insight into
their recent financial activities

Question 08: Create an audit table for one of the lookup tables and demonstrate
data saved to that audit table when data in the original table is inserted, modified,
or deleted. Include an additional column in the audit table that will have a datetime
field when the data was changed in the original table.

-- Create an audit table for Currency


CREATE TABLE CurrencyAudit (
AuditID INT IDENTITY(1,1) PRIMARY KEY,
CurrencyID INT,
CurrencyCode VARCHAR(3),
CurrencyName VARCHAR(50),
ChangeType VARCHAR(10),
ChangeDate DATETIME
);

go
-- Create a trigger to log changes in Currency to CurrencyAudit
CREATE TRIGGER CurrencyAuditTrigger
ON Currency
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @ChangeType VARCHAR(10);

IF EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted)


SET @ChangeType = 'UPDATE';
ELSE IF EXISTS (SELECT * FROM inserted)
SET @ChangeType = 'INSERT';
ELSE IF EXISTS (SELECT * FROM deleted)
SET @ChangeType = 'DELETE';

INSERT INTO CurrencyAudit (CurrencyID, CurrencyCode, CurrencyName, ChangeType,


ChangeDate)
SELECT
COALESCE(inserted.CurrencyID, deleted.CurrencyID),
COALESCE(inserted.CurrencyCode, deleted.CurrencyCode),
COALESCE(inserted.CurrencyName, deleted.CurrencyName),
@ChangeType,
GETDATE()
FROM inserted
FULL OUTER JOIN deleted ON inserted.CurrencyID = deleted.CurrencyID;
END;

Question 09: Demonstrate a use of the two stored procedures for your database. Create
and drop script for two stored procedures.

-- Create the InsertCustomer stored procedure


CREATE PROCEDURE InsertCustomer
@FirstName VARCHAR(50),
@LastName VARCHAR(50),
@Address VARCHAR(100),
@Phone VARCHAR(15)
AS
BEGIN
INSERT INTO Customers (FirstName, LastName, Address, Phone)
VALUES (@FirstName, @LastName, @Address, @Phone);
END;

go
-- Create the GetCustomersByLastName stored procedure
CREATE PROCEDURE GetCustomersByLastName
@LastName VARCHAR(50)
AS
BEGIN
SELECT *
FROM Customers
WHERE LastName = @LastName;
END;
go

Question 10: Demonstrate the use of two cursors for your database. Create and drop script
for two cursors.

-- Declare the cursor


DECLARE @CustomerCursor CURSOR;

-- Define the query to retrieve customer data


SET @CustomerCursor = CURSOR FOR
SELECT CustomerID, FirstName, LastName
FROM Customers;

-- Declare variables to store cursor data


DECLARE @CustomerID INT;
DECLARE @FirstName VARCHAR(50);
DECLARE @LastName VARCHAR(50);

-- Open the cursor


OPEN @CustomerCursor;

-- Fetch the first row


FETCH NEXT FROM @CustomerCursor INTO @CustomerID, @FirstName, @LastName;
-- Loop through the cursor and print customer data
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'CustomerID: ' + CAST(@CustomerID AS VARCHAR) + ', Name: ' + @FirstName + ' ' +
@LastName;

-- Fetch the next row


FETCH NEXT FROM @CustomerCursor INTO @CustomerID, @FirstName, @LastName;
END;
-- Close and deallocate the cursor
CLOSE @CustomerCursor;
DEALLOCATE @CustomerCursor;

Audit Table and Trigger: The "CurrencyAudit" table is created to log changes in the "Currency"
table. A trigger, "CurrencyAuditTrigger," is defined to capture and log changes in the currency
table.

Stored Procedures: Two stored procedures, "InsertCustomer" and "GetCustomersByLastName,"


are created. These procedures allow the insertion of customer data and retrieval of customer data
by last name, respectively.
Cursor Operations: Cursors are declared and used to fetch and process data from the
"Customers" and "Accounts" tables. This could be part of a data processing or reporting
operation.

Project Rationale: The project appears to be aimed at managing financial data, including
customer information, accounts, transactions, investments, and currency data. It includes views
for data retrieval and an audit mechanism for tracking changes to currency data. Stored
procedures and cursors may be used for specific data manipulation and reporting tasks.

The purpose of this project might be to build a financial database system for a bank or financial
institution, allowing them to manage customer data, financial accounts, transactions,
investments, and currency information efficiently. The database's schema and associated objects
are designed to support data management, analysis, and reporting in a financial context.

You might also like