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

DB Projecct

Uploaded by

dagimnega66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

DB Projecct

Uploaded by

dagimnega66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ADAMA SCIENCE AND TECHNOLOG TECHNOLOGY

UNIVERSITY

SCHOOL OF ELECTRICAL ENGINERING AND COMPUTING


DEPARTMENT OF SOFTWARE ENGGINEERING

GROUP MEMBER ID
1.Getahun Million UGE/27697/14

2.Dagim Nega UGE/27644/14

3.Kirubel Fiseha UGE/27704/14

4.Elias Bikale UGE/27687/14

5.Nafis sefu UGE/27717/14

Database project
wegenu
1. ER-diagram
2 Data Definition Language (DDL) queries using
Microsoft SQL
create database Banking
use Banking
create table customers(
cust_id char(12) primary key,
Fname varchar(20),
Lname varchar(30),
sex varchar(2),
addres varchar(200),
date_of_birth date,
Email varchar(100) ,
tax_identifier varchar(20)
)
create table branch(
B_name varchar(200),
B_code char(12) primary key,
[B_address] varchar(100),
)
create table Employee(
Employe_id char(12) primary key,
Fname varchar(30),
Lname varchar(30),
sex varchar(2),
addres varchar(100),
salary decimal(9,2),
position varchar(100),
B_code char(12) foreign key references branch(B_code )
)
create table Account(
Account_id char(12) primary key,
Account_type varchar(20),
Account_no varchar(20),
Current_Balance decimal(30,2),
Date_opened datetime,
date_closed datetime,
Account_status varchar(40),
Cust_id char(12) foreign key references customers(cust_id),
B_code char(12) foreign key references Branch(B_code )
)
select * from Account

create table loan(


loan_id char(12) primary key,
loan_type varchar(40),
loan_amount decimal(30,2),
Interest_rate decimal(30,2),
Term int ,
[Start_date] date ,
End_date date,
Satatus varchar(30),
Cust_id char(12) foreign key references customers(cust_id)
)
create table Loan_payment(
Loan_payment_id char(12) primary key,
Schedule_payment_date date,
Payment_Amount decimal(30,2),
prinicipal_amount decimal(30,2),
Interest_amount decimal(30,2),
Paid_amount Decimal(30,2),
Paid_date date,
loan_id char(12) foreign key references Loan(loan_id)
)
create table Transactions (
Transaction_id char(12) primary key ,
Transaction_Type varchar(30),
Transaction_amount decimal(30,2),
Transaction_date datetime,
Loan_payment_id char(12) foreign key references Loan_payment(Loan_payment_id)
,
Account_id char(12) foreign key references Account(Account_id),
Employe_id char(12) foreign key references Employee(Employe_id)
)
create table Customer_Account(
Account_id char(12) foreign key references Account(Account_id),
Cust_id char(12) foreign key references customers(cust_id)
)
create table customer_phone(
phone_no varchar(20),
Cust_id char(12) foreign key references customers(cust_id)
)
create table Employe_phone(
phone_no varchar(20),
Employe_id char(12) foreign key references Employee(Employe_id)
)
create table Branch_phone(
phone_no varchar(20),
B_code char(12) foreign key references branch(B_code)
)

-- Insert data into the customers table


INSERT INTO customers (cust_id, Fname, Lname, sex, addres, date_of_birth,
Email, tax_identifier)
VALUES ('c011', 'Getahun', 'Million', 'M','Ethiopia_adama_goro', 'jun
9,2002', '[email protected]', '123456789')
INSERT INTO customers VALUES ('c012', 'dagim', 'nega',
'M','Ethiopia_adama_Dembela','1994-01-01', '[email protected]',
'123456888')
INSERT INTO customers VALUES ('c013', 'nanati', 'tesfaye', 'F',
'Ethiopia_asela_walkessa', '1990-01-01', '[email protected]',
'123456999');
INSERT INTO customers VALUES ('c014', 'Getahun', 'telila',
'M','Ethiopia_adama_goro', 'jun 9,2002', '[email protected]', '345456789')
INSERT INTO customers VALUES ('c015','Million', 'Getahun' ,
'M','Ethiopia_adama_goro', 'jun 9,2002', '[email protected]', '013456789')

-- Insert data into the Branch table


INSERT INTO Branch ( B_name, B_code, B_address)
VALUES ( 'Main Branch', 'B001', 'ethiopia adama ');
INSERT INTO Branch VALUES ( 'walkssa', 'B002', 'ethiopia asela ');
INSERT INTO Branch VALUES( 'awash', 'B003', 'ethiopia awash ' );
INSERT INTO Branch VALUES( 'dadacha arara', 'B004', 'ethiopia adama ')

-- Insert data into the Employee table


INSERT INTO Employee (Employe_id, Fname, Lname,sex, addres, salary,
position,B_code)
VALUES ('E001', 'teshome' ,'dugo', 'M', 'Ethiopia_asella_ welkessa', 75000.0,
'Manager','B001')
INSERT INTO Employee VALUES
('E002','abiyot','tesema','M','ethiopia_Adama_goro',50000.0,'financial','B001
')
INSERT INTO Employee VALUES
('E003','Biftu','Tuna','F','ethiopia_Adama_dembela'
,15000.00,'accountant','B001')
INSERT INTO Employee VALUES
('E004','abiyot','tesema','f','ethiopia_Adama_boku'
,35000.00,'auditor','B001')
INSERT INTO Employee VALUES
('E005','fenet','reta','f','ethiopia_Adama_daka_adi'
,18000.00,'analyst','B002')

-- Insert data into the Account table


INSERT INTO Account (Account_id, Account_type, Account_no, Current_Balance,
Date_opened, date_closed, Account_status, Cust_id, B_code)
VALUES ('A001', 'Savings', '1000046789534', 50000.0, '2014-01-01', NULL,
'Active', 'C011', 'B001')
INSERT INTO Account VALUES ('A002', 'Savings', '1000075348946', 00.0, '2014-
01-01', '2023-01-11', 'dormant', 'C012', 'B001')
INSERT INTO Account VALUES ('A003', 'Savings', '1000043467895', 12300.0,
'2021-01-11', NULL, 'Active', 'C012', 'B001')
INSERT INTO Account VALUES ('A004', 'Savings', '1000044953678', 50000.0,
'2014-01-01', NULL, 'Active', 'C013', 'B001')
INSERT INTO Account VALUES ('A005', 'Savings', '1000044789536', 2000000.0,
'2014-01-01', NULL, 'Active', 'C013', 'B001')

-- Insert data into the loan table


INSERT INTO loan (loan_id, loan_type, loan_amount, Interest_rate, Term,
[Start_date], End_date, Satatus, Cust_id)
VALUES ('L001', 'Home Loan', 200000.0, 5.0, 240, '2021-01-01', '2041-01-01',
'Active', 'C013');
INSERT INTO loan VALUES ('L002', 'mortugate', 400000.0, 5.0, 240, '2021-01-
01', '2041-01-01', 'Active', 'C014');
INSERT INTO loan VALUES ('L003', 'mortugate', 400000.0, 5.0, 240, '2021-01-
01', '2041-01-01', 'Active', 'C015');
iNSERT INTO loan VALUES ('L005', 'mortugate', 400000.0, 5.0, 240, '2021-01-
01', '2041-01-01', 'Active', 'C013');

-- Insert data into the Loan_payment table


INSERT INTO Loan_payment (Loan_payment_id, Schedule_payment_date,
Payment_Amount, prinicipal_amount, Interest_amount, Paid_amount, Paid_date,
loan_id)
VALUES ('LP01', '2021-02-01', 1000.0, 800.0, 200.0, 1000.0, '2021-02-01',
'L001');
INSERT INTO Loan_payment VALUES('LP02', '2019-10-21', 1000.0, 800.0, 200.0,
1000.0, '2021-08-22', 'L002');
INSERT INTO Loan_payment VALUES('LP03', '2019-10-22', 2000.0, 1600.0, 400.0,
2000.0, '2021-08-22', 'L003');
INSERT INTO Loan_payment VALUES('LP04', '2020-12-21', 1000.0, 800.0, 200.0,
1000.0, '2021-08-22', 'L005');
select*from Loan_payment
-- Insert data into the Transaction table
INSERT INTO Transactions(Transaction_id, Transaction_Type,
Transaction_amount, Transaction_date, Loan_payment_id, Account_id,
Employe_id)
VALUES (' T01', 'Deposit', 500.0, '2021-01-15', NULL, 'A001', 'E001');
INSERT INTO Transactions VALUES (' T02', 'withdraw', 1200.0, '2021-01-15',
NULL, 'A002', 'E004');
INSERT INTO Transactions VALUES (' T03', 'Deposit', 600.0, '2021-01-15',
NULL, 'A003', 'E001');
INSERT INTO Transactions VALUES (' T04', 'Deposit', 5000.0, '2021-01-15',
NULL, 'A004', 'E003');
INSERT INTO Transactions VALUES (' T05', 'tranesfer', 500.0, '2021-01-15',
NULL, 'A005', 'E001');
INSERT INTO Transactions VALUES (' T06', 'Deposit', 200.0, '2021-01-15',
NULL, 'A002', 'E003');

INSERT INTO Customer_Account( Account_id ,Cust_id )


VALUES ('A001','C011')
INSERT INTO Customer_Account VALUES ('A002','C012')
INSERT INTO Customer_Account VALUES ('A002','C013')
INSERT INTO Customer_Account VALUES ('A003','C014')
INSERT INTO Customer_Account VALUES ('A004','C014')
INSERT INTO Customer_Account VALUES ('A005','C015')
-- Insert data into the phone table
Insert into customer_phone( phone_no,
Cust_id)values('+251910907865','c011')
Insert into customer_phone( phone_no,
Cust_id)values('+251918650907','c011')
Insert into customer_phone( phone_no,
Cust_id)values('+251965109078','c012')
Insert into customer_phone( phone_no,
Cust_id)values('+251912907870','c013')
Insert into customer_phone( phone_no,
Cust_id)values('+251938077800','c013')
Insert into customer_phone( phone_no,
Cust_id)values('+25191659078','c014')
Insert into customer_phone( phone_no,
Cust_id)values('+251911907763','c014')
Insert into customer_phone( phone_no,
Cust_id)values('+251910907865','c011')

-- Insert data into the Employe_phone table


Insert into Employe_phone( phone_no,
Employe_id)values('+251911126780','E001')
Insert into Employe_phone( phone_no,
Employe_id)values('+251912709865','E001')
Insert into Employe_phone( phone_no,
Employe_id)values('+251911943285','E002')
Insert into Employe_phone( phone_no,
Employe_id)values('+251970985432','E002')
Insert into Employe_phone( phone_no,
Employe_id)values('+251913981112','E003')
Insert into Employe_phone( phone_no,
Employe_id)values('+251911171717','E004')

select *from customers


select *from customer_phone
select*from Employee
select*from Employe_phone
select*from Account
select *from loan
select * from Loan_payment
select*from Branch
select*from Branch_phone
select*from Account
select*from Transactions
3. Data retrieval from both single and multiple tables
--QUERY1
select max(salary)as avragesalary from Employee
-- Update the Current_Balance in the Account table
UPDATE Account
SET Current_Balance = 750000
WHERE Account_id = 'A001';
--QUERY2 Retrive customers with their phone no

select C.cust_id, C.Fname, C.Lname,P.phone_NO from customers C


JOIN
customer_phone P ON C.CUST_ID=P.CUST_ID

--QUERY3 Retrieve the total customers amount for each customers:


select C.cust_id,SUM(A.current_balance)as total_total from customers C
LEFT JOIN Account A ON C.cust_id=A.cust_id
GROUP BY C.cust_id

4.scenario
Seppose you want to create database for bank , the bank include- The "branch"
table is populated with branch information. Two branches are added with their
names, branch codes, and addresses.
- Employee details are inserted into the "Employee" table. Two employees are
added with their IDs, names, genders, addresses, salaries, positions, and
corresponding branch codes.
- Account information is added to the "Account" table. Two accounts are created
with account IDs, types (such as savings or checking), account numbers, current
balances, opening dates, closing dates (if applicable), account statuses, customer
IDs, and branch codes.
- The "loan" table stores loan details. Two loans are created with loan IDs, types
(e.g., home loan or auto loan), loan amounts, interest rates, loan terms, start dates,
end dates, statuses, and customer IDs.
- Loan payment information is added to the "Loan_payment" table. Two loan
payments are created with payment IDs, scheduled payment dates, payment
amounts, principal amounts, interest amounts, paid amounts, paid dates, and
corresponding loan IDs.
. Sample Scenario:
- The scenario starts by inserting customer information into the "customers"
table. Two customers are added with their respective details, including names,
addresses, dates of birth, email addresses, and tax identifiers.
- Transactions are recorded in the "Transactions" table. Two transactions are
added with transaction IDs, types (such as deposit or withdrawal), transaction
amounts, transaction dates, loan payment IDs (if applicable), account IDs, and
employee IDs.
- Customer-account relationships are established in the "Customer_Account"
table. Two entries are made to associate customer IDs with account IDs.
- Phone numbers of customers, employees, and branches are stored in the
"customer_phone", "Employe_phone", and "Branch_phone" tables, respectively.
Each phone number is linked to the corresponding customer, employee, or branch
through their respective IDs.

This sample scenario showcases the basic usage of the database schema, allowing
you to store and retrieve data related to customers, branches, employees, accounts,
loans, transactions, and phone numbers. You can expand upon this scenario by
adding more data or implementing additional functionalities as per your banking
system's requirements.

5. Views, procedures and database triggers


CREATE VIEW vwcostomer_deteils As
SELECT C.cust_id,C.Lname,C.fname
,C.email,A.account_type,A.account_no,A.current_balance
FROM customers C JOIN account A ON C.cust_id = A.cust_id
SELECT * FROM vwcostomer_deteils
--create procedure
create procedure spgetcustomerbysex
@sex char(2)
as
select * from customers where sex=@sex
exec spgetcustomerbysex 'M'
--to delete procedure
drop procedure spgetcustomerbysex
--trigger
-- we create another table for trigger that stor trigger action
create table audit(
Account_id char(12),
Account_type varchar(20),
Account_no varchar(20),
Current_Balance decimal(30,2),
Date_opened datetime,
date_closed datetime,
Account_status varchar(40)
)

CREATE TRIGGER tginsertintoaccount


ON account
AFTER INSERT
AS
print 'you are inserting data'
INSERT INTO audit (Account_no)
VALUES (getdate());
-- eg when we insert it must disply 'you are inserting data'and despley
under audit and under insert table
insert into account(Account_id,Account_type ,Account_no ,date_opened)
values('A010','active','10005343388107','jun 2,2022')

--6.security

select * from transactions

CREATE LOGIN users WITH PASSWORD = '1234';


CREATE LOGIN Manager WITH PASSWORD = '1234';
CREATE USER kira FOR LOGIN users;
CREATE USER aber FOR LOGIN Manager;
CREATE ROLE usersMangers;
SP_ADDROLEMEMBER'nn',kira;
SP_ADDROLEMEMBER'nn',abera;
-- Grant permission to user
GRANT SELECT, INSERT, UPDATE (first_name,last_name, email) ON coustemer TO
kira;
GRANT SELECT, INSERT, UPDATE (phone_number) ON phone_numbers TO abera;
GRANT SELECT, INSERT ON accont TO kira;
GRANT SELECT, INSERT, UPDATE (valance) ON account TO abera;
GRANT SELECT, INSERT, UPDATE (salary) ON employee TO abera;
GRANT SELECT ON vwcostomer_deteils TO kira;
--revoke
revoke SELECT on vwcostomer_deteils from kira

You might also like