计费系统的概念数据模型

Rat*_*r B 11 mysql database-design

我正在处理现有的 MySQL 数据库。我的任务是为该现有应用程序上的计费系统创建一个新的数据模型。

我创建了一个概念数据模型(点击放大):

计费系统的概念数据模型

##要求

  1. 多种付款类型(例如定期、一次性、分期付款)。

  2. 多种支付方式(现金、支票、网上银行)。

  3. 供应商(公司)与我们公司(营销主管)之间的合同

  4. 供应商可以通过从他们的页面充值(类似于预付费充值)将金额添加到他们的帐户中。

  5. 使用不同的包,这取决于包。供应商总余额将除以价值(包裹价值/365)。

  6. 如果余额变为 0,他们将不会使用他们的服务,例如:

    set  t1 = package_value / 365  
    if        
    total_balance / t1 > 0
    set
            service = active  
    else
    set
            service = inactive
    
    Run Code Online (Sandbox Code Playgroud)

##SQL 代码

use database_name;
/*==============================================================*/
/* table: company_account                                       */
/*==============================================================*/
create table company_account
(
   account_id           int not null auto_increment,
   company_id           int not null,
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) default 1,
   primary key (account_id)
);

/*==============================================================*/
/* table: contract                                              */
/*==============================================================*/

create table contract
(
   contract_id          int not null auto_increment,
   account_id           int not null,
   contract_number      int(6) not null default '0',
   contract_date        date not null default '9999-12-31',
   initial_payment      float(5,2) not null default '0.00',
   date_created         date not null default now(),
   date_effective       date not null default '9999-12-31',
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) default 1,
   primary key (contract_id)
);


/*==============================================================*/
/* table: packages                                              */
/*==============================================================*/
create table packages
(
   package_id           int not null,
   package_name         varchar(30) not null default "No Packages Selected",
   package_value        float(5,2) not null default '0',
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (package_id)
);


/*==============================================================*/
/* table: payment_mode                                          */
/*==============================================================*/
create table payment_mode
(
   payment_mode_id      int not null,
   payment_mode         varchar(30) not null default "No Mode Selected",
   description          varchar(100) not null default "No Description Available",
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (payment_mode_id)
);


/*==============================================================*/
/* table: payment_status                                        */
/*==============================================================*/
create table payment_status
(
   payment_status_id    int not null,
   payment_status       varchar(30) not null default "No Status Selected",
   description          varchar(100) not null default "No Description Available",
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (payment_status_id)
);



/*==============================================================*/
/* table: payment_type                                          */
/*==============================================================*/
create table payment_type
(
   payment_type_id      int not null auto_increment,
   payment_type         varchar(30) not null default "No Type Selected",
   decription           varchar(100) not null default " No Description Available",
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (payment_type_id)
);



/*==============================================================*/
/* table: payments                                              */
/*==============================================================*/

create table payments
(
   payment_id           int not null auto_increment,
   payment_type_id      int not null,
   payment_mode_id      int not null,
   payment_status_id    int not null,
   package_id           int not null,
   contract_id          int not null,
   payment_date_time    timestamp not null default current_timestamp,
   payment_amount       float(5,2) not null default '0.00',
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (payment_id)
);



/*==============================================================*/
/* table: transaction                                           */
/*==============================================================*/
create table transactions
(
   transaction_id       int not null,
   payment_id           int not null,
   transaction_number   char(15) not null default '000000000000000',
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (transaction_id, payment_id)
);




/*==============================================================*/
/* table: vendor_invoice                                        */
/*==============================================================*/
create table vendor_invoice
(
   invoice_id           int not null auto_increment,
   transaction_id       int not null,
   payment_id           int not null,
   invoice_date_time    timestamp not null default current_timestamp,
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (invoice_id, transaction_id, payment_id)
);



/*==============================================================*/
/* table: vendor_basket                                         */
/*==============================================================*/
create table vendor_basket
(
   v_basket_id          int not null,
   account_id           int not null,
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (v_basket_id, account_id)
);



/*==============================================================*/
/* table: payment_basket                                        */
/*==============================================================*/
create table payment_basket
(
   basket_id            int not null,
   v_basket_id          int not null,
   account_id           int not null,
   last_credited_value  float(5,2) not null default '0',
   last_credited_date   date not null default '9999-12-31',
   amount_utilized      float(5,2) not null default '0.00',
   total_balance        float(5,2) not null default '0.00',
   days_remaining       smallint not null default '0',
   date_created         date not null default now(),
   date_replaced        date not null default '9999-12-31',
   created_by           int not null,
   is_active            bit(1) not null default 1,
   primary key (basket_id)
);


alter table company_account 
add constraint fk_company_account 
foreign key (company_id)
references company (company_id) 
on delete restrict 
on update restrict;

alter table contract 
add constraint fk_company_contract 
foreign key (account_id)
references company_account (account_id) 
on delete restrict 
on update restrict;

alter table payment 
add constraint fk_contract_payment 
foreign key (contract_id)
references contract (contract_id) 
on delete restrict 
on update restrict;

alter table payment 
add constraint fk_payment_mode 
foreign key (payment_mode_id)
references payment_mode (payment_mode_id) 
on delete restrict 
on update restrict;

alter table payment 
add constraint fk_payment_packages 
foreign key (package_id)
references packages (package_id) 
on delete restrict 
on update restrict;

alter table payment 
add constraint fk_payment_status 
foreign key (payment_status_id)
references payment_status (payment_status_id) 
on delete restrict 
on update restrict;

alter table payment 
add constraint fk_payment_type 
foreign key (payment_type_id)
references payment_type (payment_type_id) 
on delete restrict 
on update restrict;

alter table payment_basket 
add constraint fk_company_paymaent_basket 
foreign key (v_basket_id, account_id)
references vendor_basket (v_basket_id, account_id) 
on delete restrict 
on update restrict;

alter table transactions 
add constraint fk_payment_transaction 
foreign key (payment_id)
references payment (payment_id) 
on delete restrict 
on update restrict;

alter table user 
add constraint fk_user_company 
foreign key (company_id)
references company (company_id) 
on delete restrict 
on update restrict;

alter table vendor_basket 
add constraint fk_company_basket 
foreign key (account_id)
references company_account (account_id) 
on delete restrict 
on update restrict;

alter table vendor_invoice 
add constraint fk_transaction_invoice 
foreign key (transaction_id, payment_id)
references transactions (transaction_id, payment_id) 
on delete restrict 
on update restrict;
Run Code Online (Sandbox Code Playgroud)

注意:我跳过了现有数据库中已经存在的一些表。

##问题

  1. 如何在不使用外部编程语言的情况下对记录进行计算?
  2. 如何在存储过程中设置逻辑,同时插入和更新 payment_basket 表?
  3. 我的概念数据模型是否满足我的要求?
  4. 我(DBA - 初学者)将来执行这些类型的任务需要什么?

我是初学者,所以一些参考资料会很有用。

小智 0

我可能说的是显而易见的事情,但您缺少付款表中的许多外键(ContractID、PaymentTypeID、PaymentModeID、PaymentStatusID、PackageID),并避免将实际文本/值放入付款表中,这会减慢数据库的响应速度。