简体   繁体   English

数据库设计:表结构

[英]Database Design: Table structure

I've excel form which contain macro that can pull data from SQL Server and display info on the excel form. 我有一个excel表单,其中包含可以从SQL Server提取数据并在excel表单上显示信息的宏。 Users can update the form and macro will write changed back to SQL Server. 用户可以更新表单,并且宏会将更改写回SQL Server。

This is the excel form. 这是excel表格。 Users need to input the values for those highlighted on yellow color. 用户需要输入以黄色突出显示的值。 Example John enter user field as 'JOHN' follow by region 'LATAM' , year '2016' and month '3'. 示例John在用户字段中输入“ JOHN”,后跟区域“ LATAM”,年“ 2016”和月“ 3”。 Once it done, John would click an 'Retrieve' button to pull all data from SQL Server where Region = LATAM, Year=2016 and Month = 3. All the related info would display on field Area, Region, Mgmt, CompanyCode, AcctUnit, Account and Value (It could have multiple rows of records and users are not allow to edit these records). 完成后,John将单击“检索”按钮从SQL Server中提取所有数据,其中Region = LATAM,Year = 2016和Month =3。所有相关信息将显示在字段Area,Region,Mgmt,CompanyCode,AcctUnit,帐户和值(它可以有多行记录,并且不允许用户编辑这些记录)。 Now John need to input value for field Comments. 现在,约翰需要为字段注释输入值。 Once he completed, John would click 'Save' button and excel macro would write comments value to SQL Server 完成后,John将单击“保存”按钮,而excel宏会将注释值写入SQL Server。 在此处输入图片说明

Here the table structure that excel macro will read and write 这是excel宏将读取和写入的表结构

CREATE TABLE [dbo].[Sales](
[Region] [varchar](10) NOT NULL,
[Area] [varchar](10) NOT NULL,
[Mgmt] [varchar](10) NOT NULL,
[CompanyCode] [int] NOT NULL,
[AcctUnit] [varchar](7) NOT NULL,
[Account] [varchar](10) NOT NULL,
[Comment] [varchar](100) NULL,
[Year] [int] NULL,
[Jan] [float] NULL,
[Feb] [float] NULL,
[Mar] [float] NULL,
[Apr] [float] NULL,
[May] [float] NULL,
[Jun] [float] NULL,
[Jul] [float] NULL,
[Aug] [float] NULL,
[Sep] [float] NULL,
[Oct] [float] NULL,
[Nov] [float] NULL,
[Dec] [float] NULL,
[SYS_CreatedBy] [varchar](15) DEFAULT SYSTEM_USER,
[SYS_CreatedDate] [datetime] DEFAULT GETDATE(),
[SYS_ModifiedBy] [varchar](15) DEFAULT SYSTEM_USER,
[SYS_ModifiedDate] [datetime] DEFAULT GETDATE(),
CONSTRAINT [PK_Sales] PRIMARY KEY NONCLUSTERED
    (
    Region ASC, 
    Area ASC, 
    Mgmt ASC,
    CompanyCode ASC,
    AcctUnit ASC,
    Account ASC,
    )
)

Based on above table structure, how can other users put his/her comments while it doesn't affected those comments that John already updated to SQL Server ? 基于上述表结构,其他用户如何在不影响John已经更新到SQL Server的那些注释的情况下放置他/她的注释? Example, John already input his comments and has been updated to SQL Server, now Alice pull same data from SQL Server where region = LATAM, Year = 2016 and Month = 3. She should get same data as John did except comments field, which in this case should be blank instead of John's comment. 例如,John已经输入了他的注释并已更新到SQL Server,现在Alice从SQL Server中提取了相同的数据,其中region = LATAM,Year = 2016和Month =3。她应该获得与John相同的数据,但注释字段除外,该字段在这种情况下应该为空而不是约翰的评论。

Based on above table design, the comment's value will keep changing by different users. 根据上面的表格设计,评论的价值将由不同的用户不断变化。 How to redesign table structure so comment can be unique to each users? 如何重新设计表结构,以便注释对于每个用户而言都是唯一的?

I would create a Comments table, and relate it to the Sales table through a foreign key. 我将创建一个Comments表,并通过外键将其与Sales表关联。

To that end, I would add a surrogate key to the Sales table, so that you don't need to add all those PK columns to your Comments table. 为此,我将一个替代键添加到Sales表中,这样您就无需将所有这些PK列都添加到Comments表中。

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

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