C# 设计SQL表(带可变列?)
我正在创建一个在数据库中存储客户付款的程序 一个产品可以多次付款,但我不想为客户的每次付款创建单独的列 我曾想过通过在每次付款后添加一个新的列来改变列数,但这对我来说似乎是一个糟糕的解决方案 有什么建议吗 我认为桌子应该是这样的:C# 设计SQL表(带可变列?),c#,sql,C#,Sql,我正在创建一个在数据库中存储客户付款的程序 一个产品可以多次付款,但我不想为客户的每次付款创建单独的列 我曾想过通过在每次付款后添加一个新的列来改变列数,但这对我来说似乎是一个糟糕的解决方案 有什么建议吗 我认为桌子应该是这样的: ID ClientID ProductID Payment????..... 我认为可能有必要跟踪每一笔单独的付款。从技术角度来看,这将使您的生活更加简单,在现实生活中的交易中,这可能是必要的 我认为可能有必要跟踪每一笔单独的付款。从技术角度来看,这将
ID ClientID ProductID Payment????.....
我认为可能有必要跟踪每一笔单独的付款。从技术角度来看,这将使您的生活更加简单,在现实生活中的交易中,这可能是必要的 我认为可能有必要跟踪每一笔单独的付款。从技术角度来看,这将使您的生活更加简单,在现实生活中的交易中,这可能是必要的 您应该为
付款创建一个单独的表
,然后您可以为每个订单进行多次付款。与此类似:
create table payments
(
paymentid int,
paymentamount int,
orderid int,
paymentType varchar(50)
)
create table orders
(
orderid int,
customerid int,
)
create table customers
(
customerid int,
customername varchar(10)
)
create table orderdetails
(
orderid int,
productid int
)
create table products
(
productid int,
productname varchar
)
以这样的方式设置它将允许您对每个订单进行多次付款
那么您的查询将是:
select *
from customers c
left join orders o
on c.customerid = o.customerid
left join orderdetails od
on o.orderid = od.orderid
left join products p
on od.productid = p.productid
left join payments ps
on o.orderid = ps.orderid
您应该为
付款创建一个单独的表
,然后您可以为每个订单进行多次付款。与此类似:
create table payments
(
paymentid int,
paymentamount int,
orderid int,
paymentType varchar(50)
)
create table orders
(
orderid int,
customerid int,
)
create table customers
(
customerid int,
customername varchar(10)
)
create table orderdetails
(
orderid int,
productid int
)
create table products
(
productid int,
productname varchar
)
以这样的方式设置它将允许您对每个订单进行多次付款
那么您的查询将是:
select *
from customers c
left join orders o
on c.customerid = o.customerid
left join orderdetails od
on o.orderid = od.orderid
left join products p
on od.productid = p.productid
left join payments ps
on o.orderid = ps.orderid
在关于的注释中,您应该有一个链接到用户表的付款表
Customer
{
ID,
Name etc...
}
Payment
{
Amount
CustomerId, (foreign key to customer table)
ProductId,
etc...
}
然后,要查看客户的付款情况,请执行以下操作:
Select *
From Customer
Inner join Payment on Customer.Id = Payment.CustomerId
where customer.id == ?
在关于的注释中,您应该有一个链接到用户表的付款表
Customer
{
ID,
Name etc...
}
Payment
{
Amount
CustomerId, (foreign key to customer table)
ProductId,
etc...
}
然后,要查看客户的付款情况,请执行以下操作:
Select *
From Customer
Inner join Payment on Customer.Id = Payment.CustomerId
where customer.id == ?
产品销售场景的典型数据库规范化如下所示:
Table: Product
(pkey) ProductID
BarCode
ProductName
Price
etc... (product description, and stuff)
Table: InvoiceItem
(pkey) InvoiceItemID
(pkey) InvoiceID
(fkey) ProductID
Quantity
Table: Invoice
(pkey) InvoiceID
*some people like InvoiceTotal here, but then you end up with multiple authorities for the total of the invoice, I prefer to derive the InvoiceTotal from the sum of InvoiceItems*
Table: Payments
(pkey) PaymentID
(fkey) InvoiceID
PaymentAmount
PKEY是表中该行的主键。FKEY指与另一实体的外键关系
最后(当您加入所有这些时),您将得到一个垂直表,该表有一张包含一个或多个付款的发票,并在不同的列中包含贷方和depits。这允许一张发票在同一总额上有多个产品,并允许对同一张发票进行多笔付款。如上所述,动态添加列将比其他任何事情更令人困惑。关系数据库设计是关于一组已知的列,在未知数量的行上连接数据。产品销售场景的典型数据库规范化如下所示:
Table: Product
(pkey) ProductID
BarCode
ProductName
Price
etc... (product description, and stuff)
Table: InvoiceItem
(pkey) InvoiceItemID
(pkey) InvoiceID
(fkey) ProductID
Quantity
Table: Invoice
(pkey) InvoiceID
*some people like InvoiceTotal here, but then you end up with multiple authorities for the total of the invoice, I prefer to derive the InvoiceTotal from the sum of InvoiceItems*
Table: Payments
(pkey) PaymentID
(fkey) InvoiceID
PaymentAmount
PKEY是表中该行的主键。FKEY指与另一实体的外键关系
最后(当您加入所有这些时),您将得到一个垂直表,该表有一张包含一个或多个付款的发票,并在不同的列中包含贷方和depits。这允许一张发票在同一总额上有多个产品,并允许对同一张发票进行多笔付款。如上所述,动态添加列将比其他任何事情更令人困惑。关系数据库的设计都是关于一组已知的列,在未知数量的行上连接数据。想想行,而不是列-SQL/RA适用于固定元组/记录(已知列和静态列),其中每个元组/记录可以在一个集合/关系中存在任意次数。每一行都是用户进行的事务。列描述transactionThink行中发生的情况,而不是列-SQL/RA适用于固定元组/记录(已知列和静态列),其中每个元组/记录可以在集合/关系中存在任意次数。每一行都是用户进行的事务。这些列描述了交易过程中发生的情况,但OP是正确的,因为没有必要(事实上,这是一种不好的做法)为每一笔付款设置单独的列。当然,我并没有以任何方式建议(单独的列)。同意,但OP是正确的,因为它没有必要(事实上是一种不好的做法)每个付款都有一个单独的栏。当然,我不是建议(单独的栏)任何方式。