1 一个银行数据库应用 开发实例. 2 银行数据库需求 requirements...

Post on 27-Dec-2015

489 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

一个银行数据库应用开发实例

2

银行数据库需求

Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户( customer )有 0 个或多个账户 一个支行 有多个信贷( loan )记录 客户可以存钱( deposit )或取钱( withdraw ) 客户可以转账( transfer ) 客户可以借贷( borrow )或还贷( payback )( 为简化问题,不考虑利息 )

3

Use caseBank System

CustomerManagement

AccountManagement

QueryAndReport

LoanManagement

Manager

Clerk

BankManagement

<<uses>>

<<uses>>

<<uses>>

<<uses>>

<<uses>>

<<uses>>

注意:此处忽略了数据库管理部分

4

功能分析BankManagement

Add Branch

Delete Branch

Modify Branch

CustomerManagement

Add Customer

Delete Customer

Modify Customer

AccountManagement

Add Account

Delete Account

Deposit

Withdraw

Transfer

LoanManagement

Add Loan

Delete Loan

Borrow

Payback

QueryAndReport

Customer Info

Customer Account

Customer Loan

Bank Info

Bank Account

Bank Loan

5

如果某个功能比较复杂,画一个活动图

input account A

display info of account A

input tannsfer amount

display info of account B

input account B

balance of account A > transfer amount ?

bagin a tranastion

subtract amount from A

Add amount into B

Success ?

commitrollback

Y

N

Y

N

report error

6

使用 E-R 图设计

branch account customer

loan

has depositor

has borrower

10..nn1

11

0..nm

account_access

access

loan_access

access

1..2

n

n

1

7

branch account customer

loan

has depositor

has borrower

10..nn1

11 0..nm

account_access

access

loan_access

access

1..2

n

n

1

Branch name

Branch city

assets

Account number

balance

Customer name

Customerstreet

Customer city

Loan number amount

access number Access type amounttime

access type can be can be deposit, withdraw or transfer.No primary key – weak entityset

access type can be can be deposit, withdraw or transfer.No primary key – weak entityset

access numberAccess type amount

time

access type can be can be borrow or payback.No primary key – weak entityset

access type can be can be borrow or payback.No primary key – weak entityset

8

branch account customer

loan

has depositor

has borrower

10..nn1

11 0..nm

account_access

access

loan_access

access

1..2

n

n

1

Branch name

Branch city

assets

Account number

balance

Customer name

Customerstreet

Customer city

Loan number amount

access numberAccess type amount

time

access number Access type amounttime

9

1. branch (branch_name, branch_city, assets)2. customer (customer_name, customer_street, customer_city)3. account (account_number, branch_name, balance)4. loan (loan_number, branch_name, amount)5. depositor (customer_name, account_number)6. borrower (customer_name, loan_number)7. account_access (account_number, access_number, access_type, amount, transfer_account_number, access_date, access_time)8. loan_access (loan_number, access_number, access_type, amount, access_date, access_time)

转换为模式

Primary key foreign key

10

检查模式设计

使用设计范式检查 如果模式设计的不够好,返回上一步

进行改进

考虑以下设计 :Account-branch (account_number,branch_name,branch_city, branch_assets,account_balance)

11

角色和权限Role table

privilege

select insert delete update

Manager

branch √ √ √ √

customer √

account √

loan √

depositor √

borrower √

account_access √

loan_access √

Clerk

branch √

customer √ √ √ √

account √ √ √ √

loan √ √ √ √

depositor √ √ √ √

borrower √ √ √ √

account_access √ √ √ √

loan_access √ √ √ √

12

建数据库 Logon to pgAdmin as

administrator (postgres) Create a new DB

13

建登录角色 Create role manager

and clerk

14

写建表脚本create table account (account_number varchar(15) not null unique, branch_name varchar(15) not null, balance numeric not null, primary key(account_number));

create table branch (branch_name varchar(15) not null unique, branch_city varchar(15) not null, assets numeric not null, primary key(branch_name));

Save this file as make-bank.sql so that you can use it later.

15

create table customer (customer_name varchar(15) not null unique, customer_street varchar(12) not null, customer_city varchar(15) not null, primary key(customer_name));

create table loan (loan_number varchar(15) not null unique, branch_name varchar(15) not null, amount numeric not null, primary key(loan_number));

16

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));

17

create table account_access (account_number varchar(15) not null, access_number varchar(15) not null, access_type varchar(15) not null, amount numeric not null, transfer_account_number varchar(15), access_date date, access_time time, primary key(account_number, access_number), foreign key(account_number) references account(account_number), foreign key(transfer_account_number) references account(account_number), check (access_type in ('deposit','withdraw','transfer')));

18

create table loan_access (loan_number varchar(15) not null, access_number varchar(15) not null, access_type varchar(15) not null, amount numeric not null, access_date date, access_time time, primary key(loan_number, access_number), foreign key(loan_number) references loan(loan_number), check (access_type in ('borrow','payback')));

19

授权grant select on branch, customer, account, loan, depositor, borrower, account_access, loan_access to manager;

grant insert, delete, update on branch to manager;

grant select on branch, customer, account, loan, depositor, borrower, account_access, loan_access to clerk;

grant insert, delete, update on customer, account, loan, depositor, borrower, account_access, loan_access to clerk;

20

为每个功能准备 SQL 语句 例:查询用户名为 'custerName‘的用户的信息

SELECT customer.customer_name, account.account_number, account.branch_name, account.balanceFROM customer,account,depositorWHERE customer.customer_name = depositor.customer_name AND account.account_number = depositor.account_number AND customer.customer_name = 'custerName';

21

在数据库中建表 Click “open file” and open the

“make-bank.sql” Select the create and insert commands

and execute

22

配置 ODBC 数据源

23

24

创建访问界面 为了简化,忽略了

系统登录部分,并且把 manager 和 clerk 的页面合在一起。

25

Select “design” panel

Right-click on the page and select “properties”

Modify the title of the web page

26

Draw a label into the page text : “Bank Demo”Font -> zise: “xx-large”

Draw a “horizontal rule”

27

Add HyperlinkText: “Edit Branch”NavigateUrl: EditBranch.aspxTarget: _blank (open in a new page)

28

In the solution explorer, right-click the solution name -> add -> new itemEditBranch.aspx

29

Draw a GridView control into the EditBranch.aspx and set the data source

30

Select the bank connection

31

32

33

34

Set gridview properties

35

Click source Remove the “[“ and “]” from the source

code since it is not supported by PostgreSQL

36

Add text box and button to support inserting a new branch

Double-click button to add code: string connectString = "Dsn=PostgreSQL_manager;uid=manager;pwd=manager";

System.Data.Odbc.OdbcConnection myConn = new System.Data.Odbc.OdbcConnection(connectString); myConn.Open();

System.Data.Odbc.OdbcCommand insertBranch = myConn.CreateCommand(); insertBranch.CommandText = "INSERT INTO branch (branch_name,"; insertBranch.CommandText += " branch_city, assets) VALUES ('"; insertBranch.CommandText += (this.TextBox1.Text + "','" + this.TextBox2.Text + "'," + this.TextBox3.Text + ")");

insertBranch.ExecuteNonQuery(); insertBranch.Dispose(); myConn.Close(); this.GridView1.DataBind(); //refresh the gridview

37

Add a new page CheckCustomer.aspx

Draw a ListBox into the page

Choose data source

38

39

40

Add a new connection

41

42

43

select customer_name from customer

44

Add a new button named query Add a new GridView and create new

data source

45

46

47

select customer.customer_name, account.account_number, account.branch_name, account.balancefrom customer,account,depositorwhere customer.customer_name = depositor.customer_name and account.account_number = depositor.account_number

48

Set the “Visible” to be false so that it will not display before querying

49

Add code for the “query” button

string custerName = this.ListBox1.Items[this.ListBox1.SelectedIndex].Text;

this.SqlDataSource2.SelectCommand += ("and customer.customer_name = '" + custerName + "'");

this.GridView1.Visible = true; this.GridView1.DataBind();

50

小结 在本例中

有些问题没有考虑,如安全、异常处理、数据备份等。 图形界面的设计和实现,有多种方法,没有绝对的标准。

应用开发 在开发一个项目之前,你不可能什么都知道。遇到问题后去

研究解决,最好的方法是在网上搜索。 参考资料

For some specific issues about DB development, you can search on Code Project: http://www.codeproject.com/search.aspx?q=database&sbo=kw&pgnum=3

For real DB application, you can search on sourceforge: http://sourceforge.net/search/?q=erp (ERP is the common

DB application)

51

End of Chapter

top related