CREATE TABLE items (
itemid int not null AUTO_INCREMENT PRIMARY KEY,
itemname varchar(40) not null,
onhand int not null, # current inv based on sales and inv adjustments
price decimal (10,2) not null # default price per item
) engine=innodb;
CREATE TABLE sale (
saleid int not null AUTO_INCREMENT PRIMARY KEY, # basically an invoice #
customerid int not null, # joined to a customer table
saledate timestamp not null
) engine=innodb;
CREATE TABLE sale_detail (
saleid int not null, # invoice #
lineid int not null, # lines 1 and up for this invoice
itemid int not null, # item sold
qty int not null, # quantity sold
price decimal (10,2) not null # price per item can be overriden from items table value
) engine=innodb;
CREATE TABLE inventory_adj (
adjid int not null AUTO_INCREMENT PRIMARY KEY,
itemid int not null,
trans_typ int not null, # 0=purchase stock, 1=inventory adj, 2=shrinkage, 3=return, etc
adjdate timestamp not null, # now()
qty int not null # amt of change, positive is acquisition, negative is depletion
) engine=innodb;
用现有量为您的项目表准备好。这是您的库存水平。从销售量下降,随着购买量上升到inventory_adj 表中,当你盘点时也在那里得到调整,等等。这是一个规范化的数据模型。诚然,在技术上不需要保留现有项目,它可以即时计算,但这有点慢。只要您创建交易并根据销售等进行即时更新并提交交易,您就是安全的。另外,这可以让您更好地了解发生了什么让您到达那里。当然,添加其他列,例如在销售和库存调整中输入的用户名称。