简体   繁体   English

在多租户应用中需要一些有关发票数据库设计的建议

[英]Need some suggestions for database design for Invoices in a multi-tenant app

I need some guidance on designing the schema for invoices in a multi-tenant application. 我需要有关在多租户应用程序中设计发票架构的一些指导。 I have a table called EmployeePay which holds all the information required to generate an invoice. 我有一个名为EmployeePay的表,其中包含生成发票所需的所有信息。 The invoice table would have the invoice number, invoice created date and VAT rate. 发票表将包含发票编号,发票创建日期和VAT税率。 I am thinking to create a Sequence object for each Tenant to generate an invoice number. 我正在考虑为每个Tenant创建一个Sequence对象以生成发票编号。

EmployeePay Table: EmployeeID, Hours, Rate, InvoiceID (FK)

Invoice Table: InvoiceID (PK) (Identity), InvoiceNumber, InvoiceDate, VATRate, TenantID

Is it okay to have hundreds of Sequence objects in a database, as I'll have to create one for each tenant? 数据库中可以有数百个Sequence对象,因为我必须为每个租户创建一个对象吗? I'll also have to create same amount of stored procedures which returns the next invoice number (I prefer a separate stored procedure for each tenant rather than having one large stored procedure with hundreds of choices in a select case statement). 我还必须创建相同数量的存储过程,该存储过程将返回下一个发票编号(我更喜欢为每个租户分配一个独立的存储过程,而不是在select case语句中使用一个具有数百种选择的大型存储过程)。

Another concern is, is it theoretical to insert into the master table ( Invoice ) based on the transaction table ( EmployeePay ) and then use its primary key( InvoiceID ) to update the transaction table? 另一个需要考虑的问题是,从理论上讲,基于交易表( EmployeePay )插入到主表( Invoice ),然后使用其主键( InvoiceID )更新交易表是吗?

Thanks in advance. 提前致谢。

First make sure the relationship either this is one to many or many to many. 首先要确保这种关系是一对多或多对多的。 If you are considering one employee that will have many invoices then its one to many relationship and you can create your table as under: 如果您正在考虑一位拥有多张发票的员工,那么它就是一对多的关系,则可以如下创建表格:

EmployeePay Table: EmployeeID (PK) (Identity), Hours, Rate

Invoice Table: InvoiceID (PK) (Identity), EmployeeID (FK), InvoiceNumber, InvoiceDate, VATRate, TenantID

EDIT: 编辑:

I don't know which database you are using but for increment sequence check: 我不知道您正在使用哪个数据库,但是要进行增量序列检查:

  1. for MySQL check this LINK . 对于MySQL,请检查此LINK
  2. If you are using Oracle then check this LINK 如果您使用的是Oracle,请检查此链接

I would suggest you to create another table can be called as InvoiceNumber, this will contain InvoiceNumberId(Int) , TenantId (Fk) , CurrentSequenceNumber(Int) . 我建议您创建另一个可以称为InvoiceNumber的表,其中将包含InvoiceNumberId(Int)TenantId(Fk)CurrentSequenceNumber(Int)

Significance of CurrentSequenceNumber is that it will be simple integer number which can be used to generate next Invoicenumber. CurrentSequenceNumber的意义在于它将是简单的整数,可用于生成下一个Invoicenumber。 InvoiceNumberId will be a Identity columns for Primary key purpose (you may or may not have it). InvoiceNumberId将是用于主键用途的Identity列(您可能有也可能没有)。

Structure of the Table will look like below. 表的结构如下所示。

在此处输入图片说明

Now you need to create only One Stored Procedure which will take input parameter as TenantId and will have responsiblity to generate next Invoice number by reading CurrentSequenceNumber from above table. 现在,您只需要创建一个存储过程,该存储过程将输入参数作为TenantId,并且有责任通过从上表中读取CurrentSequenceNumber来生成下一个发票编号。

For example if we need to generate new Invoice Id for Tenant with id as 15 then SP will have your Business logic I am assuming Just creating a String with "Inv-" as prefix with incremented value of CurrentSequenceNumber so output of Procedure will be. 例如,如果我们需要为ID为15的租户生成新的发票ID,则SP将具有您的业务逻辑,我假设仅创建一个以“ Inv-”为前缀的字符串,其前缀为CurrentSequenceNumber的增量值,因此Procedure的输出为。

Inv-0009 INV-0009

Then after generation of this number SP will increment value to 9 for InvoiceNumberId 3. 然后,在生成此数字后,SP会将InvoiceNumberId 3的值增加到9。

So everything will be managed by Single table and Single procedure only. 因此,所有内容将仅由Single表和Single过程管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM