数据库课程设计金融数据库设计与实现
Posted BkbK-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库课程设计金融数据库设计与实现相关的知识,希望对你有一定的参考价值。
数据库课程设计
文章目录
- 数据库课程设计
1.E-R图
2.E-R图转换为关系模式
样式说明:外码,主码
2.1 实体集的处理
普通实体集转换为关系模式:
- 支行 (支行ID,支行名称,系统密码,城市,街道);
- 理财经理 (工号,姓名,系统密码,身份证号,电话,邮箱);
- 贷款 (贷款号,贷款金额,贷款日期,还款日期);
- 账户 (账号,金额)
一般化/特殊化实体集的处理:
由于所有一般化/特殊化实体集均为全部特殊化,且不相交,采取只将每个最低层实体集转化为关系模式
- 个人客户 (个人客户ID,客户密码,姓名,身份证号,邮箱,手机号码);
- 企业客户 (企业客户ID,客户密码,企业名称,企业法人,注册地址,营业执照号);
- 信用卡 (信用卡号,支付密码,透支额度,剩余额度);
- 结算卡 (结算卡号,支付密码,上限金额,剩余金额);
- 保险 (保险名称,保险编号,保险价格,适用人群,保险年限,保障项目);
- 基金 (基金名称,基金编号,基金类型,基金价格,风险等级,基金管理者);
2.2 联系集的处理
一对多联系:
- 客户-账户(账号,客户ID);
- 支行-理财经理 (支行ID,工号);
- 支行-账户 (账号支行ID);
- 支行-贷款 (贷款号,支行ID);
- 客户-贷款 (贷款号,客户ID);
- 理财经理-客户(工号,客户ID);
- 个人客户- 信用卡(个人客户ID,信用卡号);
- 企业客户-结算卡 (企业客户ID,结算卡号);
多对多联系:
- 购买 (客户ID,产品编号,购买份额);
2.3 整理合并
- 支行 (支行ID,支行名称,系统密码,城市,街道);
- 理财经理 (工号,姓名,系统密码,身份证号,电话,邮箱,工作银行ID);
- 贷款 (贷款号,贷款金额,贷款日期,还款日期,放贷银行ID,贷款客户ID);
- 账户 (账号,金额,开户行ID,客户ID)
- 个人客户 (个人客户ID,客户密码,姓名,身份证号,邮箱,手机号码,理财经理工号);
- 企业客户 (企业客户ID,客户密码,企业名称,企业法人,注册地址,营业执照号,理财经理工号);
- 信用卡 (信用卡号,支付密码,透支额度,剩余额度,个人客户ID);
- 结算卡 (结算卡号,支付密码,上限金额,剩余金额,企业客户ID);
- 保险 (保险名称,保险编号,保险金额,适用人群,保险年限,保障项目);
- 基金 (基金名称,基金编号,基金类型,基金金额,风险等级,基金管理者);
- 存取 (客户ID,账号,存取金额,存取日期,存/取);
(注:客户ID多态关联)
- 购买基金 (客户ID,基金产品编号,购买份额);
- 购买保险 (客户ID,保险产品编号,购买数量);
3.数据模型
3.1对象及属性对应的编号
- Branch (
branch_ID
, branch_name, system_password, city, street); - Manager (
work_number
, name, system_password, ID_number, telephone, email, branch_ID); - Loan (
loan_no
,loan_amount,loan_date, repayment_date, branch_ID, customer_ID); - Account (
account_no
, account_amount, branch_ID,Customer_ID); - PersonalCustomer(
PersonalCustomer_ID
,system_password,name, ID_number,email, telephone,work_number); - EnterpriseCustomer (
EnterpriseCustomer_ID
,system_password, name, legal_person, address, business_number,work_number); - CreditCard (
CreditCard_no
, payment_password, count_limit, remain, PersonalCustomer_ID); - SettlementCard (
SettlementCard_no
, payment_password, count_limit ,remain ,EnterpriseCustomer_ID); - Fund(f_name,
f_id
,f_type,f_amount,risk_level,f_manager); - Insurance(i_name,
i_id
,i_amount,i_person,i_year,i_project); - Access (
Customer_ID,account_no
,count,access_date,state); - Buy_F (
Customer_ID,f_id
,count); - Buy_I (
Customer_ID,i_id
,count);
3.2 数据项说明
3.2.1 Branch(支行)表
- Branch (
branch_ID
, branch_name, system_password, city, street); - 支行 (支行ID,支行名称,系统密码,城市,街道);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
branch_ID | VARCHAR(20) | PRIMARY KEY | 支行ID |
branch_name | VARCHAR(100) | UNIQUE | 支行名称 |
system_password | VARCHAR(20) | 系统密码 | |
city | VARCHAR(100) | 城市 | |
street | VARCHAR(100) | 街道 |
3.2.2 Manager(理财经理)表
- Manager (
work_number
, name, system_password, ID_number, telephone, email, branch_ID); - 理财经理 (工号,姓名,系统密码,身份证号,电话,邮箱,工作银行ID);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
work_number | VARCHAR(20) | PRIMARY KEY | 工号 |
name | VARCHAR(20) | NOT NULL | 姓名 |
system_password | VARCHAR(20) | NOT NULL | 系统密码 |
ID_number | VARCHAR(20) | UNIQUE | 身份证号 |
telephone | VARCHAR(20) | 电话 | |
VARCHAR(50) | 邮箱 | ||
branch_ID | VARCHAR(20) | FOREIGN KEY | 工作银行ID |
3.2.3 Loan(贷款)表
- Loan (
loan_no
,loan_amount,loan_date, repayment_date, branch_ID, customer_ID); - 贷款 (贷款号,贷款金额,贷款日期,还款日期,放贷银行ID,贷款客户ID);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
loan_no | VARCHAR(20) | PRIMARY KEY | 贷款号 |
loan_amount | DOUBLE PRECISION | NOT NULL | 贷款金额 |
loan_date | DATE | NOT NULL | 贷款日期 |
repayment_date | DATE | NOT NULL | 还款日期 |
branch_ID | VARCHAR(20) | FOREIGN KEY | 放贷银行ID |
customer_ID | VARCHAR(20) | FOREIGN KEY | 贷款客户ID |
3.2.4 Account(账户)表
- Account (
account_no
, account_amount, branch_ID,Customer_ID); - 账户 (账号,金额,开户行ID,客户ID);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
account_no | VARCHAR(20) | PRIMARY KEY | 账号 |
account_amount | DOUBLE PRECISION | NOT NULL | 金额 |
branch_ID | VARCHAR(20) | FOREIGN KEY | 开户行ID |
Customer_ID | VARCHAR(20) | FOREIGN KEY | 客户ID |
3.2.5 PersonalCustomer(个人客户)表
-
PersonalCustomer(
PersonalCustomer_ID
,system_password,name, ID_number,email, telephone,work_number); -
个人客户 (个人客户ID,客户密码,姓名,身份证号,邮箱,手机号码,理财经理工号);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
PersonalCustomer_ID | VARCHAR(20) | PRIMARY KEY | 个人客户ID |
system_password | VARCHAR(20) | NOT NULL | 客户密码 |
name | VARCHAR(20) | NOT NULL | 姓名 |
ID_number | VARCHAR(20) | UNIQUE | 身份证号 |
VARCHAR(50) | 邮箱 | ||
telephone | VARCHAR(20) | 手机号码 | |
work_number | VARCHAR(20) | FOREIGN KEY | 理财经理工号 |
3.2.6 EnterpriseCustomer(企业客户)表
- EnterpriseCustomer (
EnterpriseCustomer_ID
,system_password, name, legal_person, address, business_number,work_number); - 企业客户 (企业客户ID,客户密码,企业名称,企业法人,注册地址,营业执照号,理财经理工号);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
EnterpriseCustomer_ID | VARCHAR(20) | PRIMARY KEY | 企业客户ID |
system_password | VARCHAR(20) | NOT NULL | 客户密码 |
name | VARCHAR(20) | NOT NULL | 企业名称 |
legal_person | VARCHAR(20) | 企业法人 | |
address | VARCHAR(100) | 注册地址 | |
business_number | VARCHAR(20) | 营业执照 | |
work_number | VARCHAR(20) | FOREIGN KEY | 理财经理工号 |
3.2.7 CreditCard(信用卡)表
- CreditCard (
CreditCard_no
, payment_password, count_limit, remain, PersonalCustomer_ID); - 信用卡 (信用卡号,支付密码,透支额度,剩余额度,个人客户ID);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
CreditCard_no | VARCHAR(20) | PRIMARY KEY | 信用卡号 |
payment_password | VARCHAR(20) | NOT NULL | 支付密码 |
count_limit | DOUBLE PRECISION | CHECK | 透支额度 |
remain | DOUBLE PRECISION | CHECK | 剩余额度 |
PersonalCustomer_ID | VARCHAR(20) | FOREIGN KEY | 个人客户ID |
3.2.8 SettlementCard(结算卡)表
- SettlementCard (
SettlementCard_no
, payment_password, count_limit ,remain ,EnterpriseCustomer_ID); - 结算卡 (结算卡号,支付密码,上限金额,剩余金额,企业客户ID);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
SettlementCard_no | VARCHAR(20) | PRIMARY KEY | 结算卡号 |
payment_password | VARCHAR(20) | NOT NULL | 支付密码 |
count_limit | DOUBLE PRECISION | CHECK | 上限金额 |
remain | DOUBLE PRECISION | CHECK | 剩余金额 |
EnterpriseCustomer_ID | VARCHAR(20) | FOREIGN KEY | 企业客户ID |
3.2.9 fund(基金)表
- Fund(f_name,
f_id
,f_type,f_amount,risk_level,f_manager); - 基金 (基金名称,基金编号,基金类型,基金金额,风险等级,基金管理者);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
f_name | VARCHAR(100) | NOT NULL | 基金名称 |
f_id | INTEGER | PRIMARY KEY | 基金编号 |
f_type | VARCHAR(20) | 基金类型 | |
f_amount | INTEGER | 基金金额 | |
risk_level | VARCHAR(20) | NOT NULL | 风险等级 |
f_manager | INTEGER | NOT NULL | 基金管理者 |
3.2.10 insurance(保险)表
- 保险 (保险名称,保险编号,保险金额,适用人群,保险年限,保障项目);
- Insurance(i_name,
i_id
,i_amount,i_person,i_year,i_project);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
i_name | VARCHAR(100) | NOT NULL | 保险名称 |
i_id | INTEGER | PRIMARY KEY | 保险编号 |
i_amount | INTEGER | 保险金额 | |
i_person | VARCHAR(20) | 适用人群 | |
i_year | INTEGER | 保险年限 | |
i_project | VARCHAR(200) | 保障项目 |
3.2.11 Buy_I(购买保险)
- Buy_I (
Customer_ID,i_id
,count); - 购买保险 (客户ID,基保险产品编号,购买数量);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
Customer_ID | VARCHAR(20) | PRIMARY KEY | 客户ID |
i_id | VARCHAR(20) | PRIMARY KEY | 保险产品编号 |
count | INTERGER | NOT NULL、CHECK | 购买数量 |
3.2.12 Buy_F(购买基金)
- Buy_F (
Customer_ID,f_id
,count); - 购买基金 (客户ID,基金产品编号,购买份额);
字段名称 | 字段类型 | 约束 | 说明 |
---|---|---|---|
Customer_ID | VARCHAR(20) | PRIMARY KEY | 客户ID |
f_id | VARCHAR(20) | PRIMARY KEY | 保险产品编号 |
count | INTERGER | NOT NULL、CHECK | 购买份额 |
4.创建数据表
4.1 创建数据库
4.1.1 登录数据库
切换到omm用户,以操作系统用户omm登录数据库主节点。
su - omm
启动数据库服务
gs_om -t start
使用gsql工具登陆数据库
gsql -d postgres -p 26000 -r
4.1.2 创建金融数据库
创建数据库finance
CREATE DATABASE finance ENCODING 'UTF8' template = template0;
连接finance数据库
\\connect finance
创建名为finance的schema,并设置finance为当前的schema
CREATE SCHEMA finance;
将默认搜索路径设为finance
SET search_path TO finance;
4.2 信息表的创建
4.2.1 Branch(支行)表
- Branch (
branch_ID
, branch_name, system_password, city, street); - 支行 (支行ID,支行名称,系统密码,城市,街道);
删除表Branch
DROP TABLE IF EXISTS finance.Branch;
创建表Branch
CREATE TABLE finance.Branch
(
branch_ID VARCHAR(20) PRIMARY KEY ,
branch_name VARCHAR(100) UNIQUE ,
system_password VARCHAR(20) ,
city VARCHAR(100) ,
street VARCHAR(100)
);
使用命令查看Branch表详细信息: \\d+ finance.Branch
Table "finance.branch"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------+------------------------+-----------+----------+--------------+-------------
branch_id | character varying(20) | not null | extended | |
branch_name | character varying(100) | | extended | |
system_password | character varying(20) | | extended | |
city | character varying(100) | | extended | |
street | character varying(100) | | extended | |
Indexes:
"branch_pkey" PRIMARY KEY, btree (branch_id) TABLESPACE pg_default
"branch_branch_name_key" UNIQUE CONSTRAINT, btree (branch_name) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no
4.2.2 Manager(理财经理)表
- Manager (
work_number
, name, system_password, ID_number, telephone, email, branch_ID); - 理财经理 (工号,姓名,系统密码,身份证号,电话,邮箱,工作银行ID);
删除表Manager
DROP TABLE IF EXISTS finance.Manager;
创建表Manager
CREATE TABLE finance.Manager
(
work_number VARCHAR(20) PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
system_password VARCHAR(20) NOT NULL ,
ID_number VARCHAR(20) UNIQUE ,
telephone VARCHAR(20) ,
email VARCHAR(50) ,
branch_ID VARCHAR(20),
FOREIGN KEY (branch_ID) REFERENCES finance.Branch (branch_ID) ON UPDATE CASCADE ON DELETE CASCADE
);
使用命令查看Manager表详细信息: \\d+ finance.Manager
Table "finance.manager"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------+-----------------------+-----------+----------+--------------+-------------
work_number | character varying(20) | not null | extended | |
name | character varying(20) | not null | extended | |
system_password | character varying(20) | not null | extended | |
id_number | character varying(20) | | extended | |
telephone | character varying(20) | | extended | |
email | character varying(50) | | extended | |
branch_id | character varying(20) | | extended | |
Indexes:
"manager_pkey" PRIMARY KEY, btree (work_number) TABLESPACE pg_default
"manager_id_number_key" UNIQUE CONSTRAINT, btree (id_number) TABLESPACE pg_default
Foreign-key constraints:
"manager_branch_id_fkey" FOREIGN KEY (branch_id) REFERENCES branch(branch_id) ON UPDATE CASCADE ON DELETE CASCADE
Has OIDs: no
Options: orientation=row, compression=no
4.2.3 Account(账户)表
- Account (
account_no
, account_amount, branch_ID,Customer_ID); - 账户 (账号,金额,开户行ID,客户ID);
删除表Account
DROP TABLE IF EXISTS finance.Account;
创建表Account
CREATE TABLE finance.Account
(
account_no VARCHAR(20) PRIMARY KEY ,
account_amount DOUBLE PRECISION NOT NULL ,
branch_ID VARCHAR(20) ,
Customer_ID VARCHAR(20),
FOREIGN KEY (branch_ID) REFERENCES finance.Branch (branch_ID) ON UPDATE CASCADE ON DELETE CASCADE
);
使用命令查看Account表详细信息: \\d+ finance.Account
Table "finance.account"
Column | Type | Modifiers | Storage | Stats target | Description
----------------+-----------------------+-----------+----------+--------------+-------------
account_no | character varying(20) | not null | extended | |
account_amount | double precision | not null | plain | |
branch_id | character varying(20) | | extended | |
customer_id | character varying(20) | | extended | |
Indexes:
"account_pkey" PRIMARY KEY, btree (account_no) TABLESPACE pg_default
Foreign-key constraints:
"account_branch_id_fkey" FOREIGN KEY (branch_id) REFERENCES finance.branch(branch_id) ON UPDATE CASCADE ON DELETE CASCADE
Has OIDs: no
Options: orientation=row, compression=no
4.2.4 PersonalCustomer(个人客户)表
-
PersonalCustomer(
PersonalCustomer_ID
,system_password,name, ID_number,email, telephone,work_number); -
个人客户 (个人客户ID,客户密码,姓名,身份证号,邮箱,手机号码,理财经理工号);
删除表PersonalCustomer
DROP TABLE IF EXISTS finance.PersonalCustomer;
创建表PersonalCustomer
CREATE TABLE finance.PersonalCustomer
(
PersonalCustomer_ID VARCHAR(20) PRIMARY KEY ,
system_password VARCHAR(20) NOT NULL ,
name VARCHAR(20) NOT NULL ,
ID_number VARCHAR(20) UNIQUE ,
email VARCHAR(50) ,
telephone VARCHAR(20) ,
work_number VARCHAR(20) ,
FOREIGN KEY (work_number) REFERENCES finance.Manager (work_number) ON UPDATE CASCADE ON DELETE CASCADE
);
使用命令查看PersonalCustomer表详细信息: \\d+ finance.PersonalCustomer
Table "finance.personalcustomer"
Column | Type | Modifiers | Storage | Stats target | Description
---------------------+-----------------------+-----------+----------+--------------+-------------
personalcustomer_id | character varying(20) | not null | extended | |
system_password | character varying(20) | not null | extended | |
name | character varying(20) | not null | extended | |
id_number | character varying(20) | | extended | |
email | character varying(50) | | extended | |
telephone | character varying(20) | | extended | |
work_number | character varying(20) | | extended | |
Indexes:
"personalcustomer_pkey" PRIMARY KEY, btree (personalcustomer_id) TABLESPACE pg_default
"personalcustomer_id_number_key" UNIQUE CONSTRAINT, btree (id_number) TABLESPACE pg_default
Foreign-key constraints:
"personalcustomer_work_number_fkey" FOREIGN KEY (work_number) REFERENCES manager(work_number) ON UPDATE CASCADE ON DELETE CASCADE
Has OIDs: no
Options: orientation=row, compression=no
4.2.5 EnterpriseCustomer(企业客户)表
- EnterpriseCustomer (
EnterpriseCustomer_ID
,system_password, name, legal_person, address, business_number,work_number); - 企业客户 (企业客户ID,客户密码,企业名称,企业法人,注册地址,营业执照号,理财经理工号);
删除表EnterpriseCustomer
DROP TABLE IF EXISTS finance.EnterpriseCustomer;
创建表EnterpriseCustomer
CREATE TABLE finance.EnterpriseCustomer
(
EnterpriseCustomer_ID VARCHAR(20) PRIMARY KEY ,
system_password VARCHAR(20) NOT NULL ,
name VARCHAR(20) NOT NULL ,
legal_person VARCHAR(20) ,
address VARCHAR(100) ,
business_number VARCHAR(20) ,
work_number VARCHAR(20) ,
FOREIGN KEY (work_number) REFERENCES finance.Manager (work_number) ON UPDATE CASCADE ON DELETE CASCADE
);
使用命令查看EnterpriseCustomer表详细信息: \\d+ finance.EnterpriseCustomer
Table "finance.enterprisecustomer"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------------+------------------------+-----------+----------+--------------+-------------
enterprisecustomer_id | character varying(20) | not null | extended | |
system_password | character varying(20) | not null | extended | |
name | character varying(20) | not null | extended | |
legal_person | character varying(20) | | extended | |
address | character varying(100) | | extended | |
business_number | character varying(20) | | extended | |
work_number | character varying(20) | | extended | |
Indexes:
"enterprisecustomer_pkey" PRIMARY KEY, btree 金融产品设计:还款