本文介绍了从应用程序向表添加新列时出现数据库设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一个项目合作。现在出现的问题是,我必须处理数据库表中的动态列。



我有一个表收费 ,其中将存储与每个客户端相对应的不同费用的数量。假设这张表有以下列 -

  Client_Id Charge_1 Charge_2 Charge_3 Charge_4 
pre>

现在,管理员可以为客户应用更多的新费用。在这种情况下,新费用将作为添加到收费表中。而这需要在应用程序运行时处理不在数据库设计时。这是我的想法。



但是,它不适合我。



有没有更好的想法来处理这个问题?请建议我。



提前感谢。这个数据库设计是新的。

解决方案

创建一个复合表,即ClientCharges



您可以保留原始收费表和您的客户端表,并在客户收费表中包含以下列:



ClientChargeId,ClientId,ChargeId



在您的收费表中,您可以继续添加(需要多少费用),然后在ClientCharges表中引用ChargeId。

  CREATE TABLE ClientCharges 

ClientChargeId INT IDENTITY(1,1)
,ClientId INT
,ChargeId INT



INSERT INTO ClientCharges
(ClientId,ChargeId)
VALUES
(1,1),
(1,2 ),
(1,3),
(1,4),
(2,1),
(3,1),
),
(4,3),
(4,4)




  • 客户1收费1,2,3和4

  • 客户2已收费1

  • 客户3的费用1和2

  • 客户端4有费用3和4



然后在ClientId和ChargeId字段。


I'm currently working with a project. Now the issue arises is, I have to handle dynamic columns in database table.

I've a table Charges, where amount of different charges corresponding to each Client will be stored. Suppose this table has the following columns-

Client_Id     Charge_1     Charge_2     Charge_3     Charge_4

Now, the administrator can apply more new Charges for clients. In that case, new charges will be added to the Charges table as column. And this need to be handled in application run time not in database design time. This is my thought.

But, it doesn't look suitable to me.

Is there any better idea to handle this issue?? Please suggest me.

Thanks in advance. and I'm new with this database design.

解决方案

Make a Composite table, i.e. ClientCharges

You could keep your original Charges Table and your Client table and in the Client Charges table have the following columns:

ClientChargeId, ClientId, ChargeId

In your Charges table you can keep adding (as many Charges are you require) and then reference the ChargeId in the ClientCharges table.

CREATE TABLE ClientCharges
(
    ClientChargeId          INT IDENTITY(1,1)
    , ClientId              INT
    , ChargeId              INT
)


INSERT INTO ClientCharges
(ClientId, ChargeId)
VALUES
(1, 1),
(1,2),
(1,3),
(1,4),
(2,1),
(3,1),
(3,2),
(4,3),
(4,4)

  • Client 1 has Charges 1, 2, 3 and 4
  • Client 2 has Charge 1
  • Client 3 has charges 1 and 2
  • Client 4 has charges 3 and 4

Then add foreign key constraints on the ClientId and ChargeId fields.

这篇关于从应用程序向表添加新列时出现数据库设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 13:34