简单的数据库规范化问题

发布于 2024-09-05 15:22:01 字数 666 浏览 5 评论 0原文

我有一个关于我正在设计并确保其规范化的数据库的快速问题...

我有一个客户表,其主键为 customerId。它有一个 StatusCode 列,其中有一个反映客户帐户状态的代码,即。 1 = 打开,2 = 关闭,3 = 暂停等...

现在我想在客户表中添加另一个字段来标记是否允许暂停帐户...某些客户将被自动暂停,如果他们打破那里的交易条款...其他不...所以相关的表字段将如下所示:

Customers (CustomerId(PK):StatusCode:IsSuspensionAllowed)

现在这两个字段都依赖于主键,因为您无法确定状态或是否允许对特定客户进行暂停,除非您知道特定客户,当然,当 IsSuspensionAllowed 字段设置为 YES 时除外,客户的 StatusCode 永远不应为 3(已暂停)。

从上面的表设计来看,除非在我的表中添加检查约束,否则这种情况是有可能发生的。我看不出如何将另一个表添加到关系设计中来强制执行此操作,因为只有在 IsSuspensionAllowed 设置为 YES 且 StatusCode 设置为 3(当两者相互依赖时)的情况下。

因此,在我冗长的解释之后,我的问题是:这是一个规范化问题吗?我没有看到可以强制执行此操作的关系设计……或者它实际上只是一个应该通过检查约束和表实际上仍然是标准化的。

干杯,

史蒂夫

I have a quick question regarding a database that I am designing and making sure it is normalized...

I have a customer table, with a primary key of customerId. It has a StatusCode column that has a code which reflects the customers account status ie. 1 = Open, 2 = Closed, 3 = Suspended etc...

Now I would like to have another field in the customer table that flags whether the account is allowed to be suspended or not... certain customers will be automatically suspended if they break there trading terms... others not... so the relevant table fields will be as so:

Customers (CustomerId(PK):StatusCode:IsSuspensionAllowed)

Now both fields are dependent on the primary key as you can not determine the status or whether suspensions are allowed on a particular customer unless you know the specific customer, except of course when the IsSuspensionAllowed field is set to YES, the the customer should never have a StatusCode of 3 (Suspended).

It seems from the above table design it is possible for this to happen unless a check contraint is added to my table. I can't see how another table could be added to the relational design to enforce this though as it's only in the case where IsSuspensionAllowed is set to YES and StatusCode is set to 3 when the two have a dependence on each other.

So after my long winded explanation my question is this: Is this a normalization problem and I'm not seeing a relational design that will enforce this... or is it actually just a business rule that should be enforced with a check contraint and the table is in fact still normalized.

Cheers,

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-09-12 15:22:01

是的,这是可能的。您可以使用检查约束和 Case 语句来完成此操作:

Create Table Customer   (
        CustomerId <datatype> not null Primary Key
        , StatusCode int not null
        , IsSuspensionAllowed int not null Default( 1 )
        , Constraint CK_Customer_IsSuspensionAllowed 
                    Check ( IsSuspensionAllowed In(0,1) )
        , Constraint CK_Customer_StatusCodeRange 
                    Check ( StatusCode Between 0 And ?? )
        , Constraint CK_Customer_StausCodeValid 
                    Check ( Case
                When StatusCode = 3 And IsSuspensionAllowed = 1 Then 0
                                                                Else 1
                                                                End = 1 )
        , ....
        )

您没有提到 PK 的数据类型,所以我只是插入了一个占位符。如果您使用的是 SQL Server,则可以使用位列代替我上面提到的 int 和检查约束(bit 不是 ANSI 规范的一部分)。

这是一个很好的例子,说明状态代码之类的代理键并不总是能很好地发挥作用。最好使用字符串值来表示状态代码,在这种情况下,Case 语句将显示为 When StatusCode = 'Suspished' And IsSuspishedAllowed = 0...

从正常化的角度来看,我不认为有什么问题。是否允许暂停是特定于客户的属性,而不是其他属性。你所说的检查约束,属性值的某些状态不能存在,这很好。

顺便说一句,当 IsSuspensionAllowed = 0 时,不允许“暂停”状态是不是更有意义?使用您的数据,不允许的状态不应该是 StatusCode = 3 和 IsSuspensionAllowed = 0 吗?

Yes it is possible. You can do it with a check constraint and a Case statement:

Create Table Customer   (
        CustomerId <datatype> not null Primary Key
        , StatusCode int not null
        , IsSuspensionAllowed int not null Default( 1 )
        , Constraint CK_Customer_IsSuspensionAllowed 
                    Check ( IsSuspensionAllowed In(0,1) )
        , Constraint CK_Customer_StatusCodeRange 
                    Check ( StatusCode Between 0 And ?? )
        , Constraint CK_Customer_StausCodeValid 
                    Check ( Case
                When StatusCode = 3 And IsSuspensionAllowed = 1 Then 0
                                                                Else 1
                                                                End = 1 )
        , ....
        )

You did not mention the data type of the PK so I simply inserted a placeholder. If you are using SQL Server, you can use a bit column in lieu of the int and check constraint I mentioned above (bit is not part of the ANSI spec).

This is a good example of where surrogate keys for things like status codes do not always work out well. It would be better to have string values represent the status codes in which case the Case statement would read When StatusCode = 'Suspended' And IsSuspendedAllowed = 0....

From a normalization standpoint, I do not see anything wrong. Whether suspension is allowed is an attribute specific to a Customer and not another attribute. What you are saying with the check constraint, that certain states of attribute values cannot exist which is fine.

Btw, wouldn't make more sense to say that the Status of "Suspended" is not allowed when IsSuspensionAllowed = 0? Using your data, shouldn't the state that is not allowed be StatusCode = 3 and IsSuspensionAllowed = 0?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文