0

我期待的关于以下设计的建议和评论。预期所有表格都是大型表格(数百万条记录),并经常更新和查询,并允许任何类型的更新(更新,插入,删除)。以下数据库设计的优缺点是什么?

-- product 
create table Product(
productID int not null identity(1,1), 
name varchar(100) not null, 

constraint PK_Product primary key(productID) 
) 

-- client 
create table Client(
clientID int not null identity(1,1), 
name varchar(100) not null, 

constraint PK_Client primary key(clientID) 
) 

-- order 
create table [Order](
orderID int not null identity(1,1), 
clientID int not null, 
orderDateTime datetime not null, 
orderAmount money not null, 
orderNote varchar(max) null, 

constraint PK_Order primary key(orderID), 
constraint FK_Order_Client foreign key(clientID) references Client(clientID) 
) 

exec sp_tableoption 'Order', 'large value types out of row', 0 

create index IX_Order_client on [Order](clientID) 

-- items 
create table OrderItem(
orderItemID int not null identity(1,1), 
orderID int not null, 
productID int not null, 
qty int not null, 
amount money not null, 

constraint PK_OrderItem primary key(orderItemID), 
constraint FK_OrderItem_Order foreign key(orderID) references [Order](orderID), 
constraint FK_OrderItem_Product foreign key(productID) references Product(productID) 
) 

create index IX_OrderItem on OrderItem(orderID) 
+1

你需要做什么样的查询? – 2010-09-04 23:03:55

+0

'OrderItem'在会计/发票中通常被称为'LineItem' – 2010-09-04 23:29:49

+0

这个假设的设计适用于POS系统Mark。有更新,插入,订购历史库存,AR查询等 – 2010-09-04 23:30:00

回答

2

这看起来不错。

您也可以对订单的总费用进行折扣或增加调整金额。您打算如何处理您所显示的订单金额并不十分清楚 - 通常,订单总额可以从包含的部分总和中计算。

此外,如果适用,还考虑用于指示订单如何交付的发运方法,如果订单需要几个步骤来完成,则可能是状态。

+0

感谢兰迪的评论 – 2010-09-04 23:27:05

0

1)对于OrderItem表,我认为最好存储单价,并以 为金额添加计算字段:Amount AS Qty*UnitPrice [PERSISTED]。 此外,UnitPrice &数量字段的数据类型很重要。你确定你需要4位小数?使用2位小数不会更好(例如NUMERIC(8,2))吗?

2)此时,使用提出的设计,可以“复制”命令项(订单ID &产品ID)easyly因为OrderItem的表没有任何限制:

Order (1001, ...) 
OrderItem (1,1001,10,400,800),(2,1001,11,200,1200),(3,1001,10,400,800). 

的解决方案是添加一个唯一索引:

CREATE UNIQUE INDEX IUX_OrderItem_OrderID_ProductID 
ON OrderItem (OrderID, ProductID) 

在某些情况下,订单ID +产品ID可以被复制,但单价将diferent。 如果您遇到这种情况,那么唯一索引将有3场关键: 创造出独特的INDEX IUX_OrderItem_OrderID_ProductID_UnitPrice ON OrderItem的(订单ID,产品ID,单价)

3)如果SQL Server版本为> = 2005,那么你可能使用数据库对象的模式。

CREATE SCHEMA Sales; 
CREATE TABLE Sales.[Order] (...); 
CREATE TABLE Sales.OrderItem (...); 

4)我的建议是没有理由不创建索引(IX_OrderItem):例如查询或限制。他们需要在每个DML操作中更新,并且需要存储空间。如果你想创建索引,尽可能创建唯一的索引。

5)我不明白为Order table中的orderNote字段使用VARCHAR(MAX)数据类型的原因。 VARCHAR(8000)或NVARCHAR(4000)是不够的?您想为每个订单在该字段中插入一本小说吗?

相关问题