SQL 中不同类别的不同列
Different columns for different categories in SQL
我有一个 SQL table mobile
。对于不同的手机,我想保存不同的数据。例如,在触摸移动设备的情况下,这些列是必需的。
- 刷新率
- 分辨率
- 延迟
对于其他手机,这些列是必需的。
- 是黑白的
- 屏幕尺寸
除此之外,还有一些通用列在两个类别中都是必需的,例如
- 价格
- 型号
那么我该如何设计这个table。我应该创建两个单独的 table 吗?或者只是一个但我如何处理不同类别的必需属性?或者其他方法?
创建一个包含所有这些列的单个 table 以及一个名为 MobileType 的新列。您可以为“Touch Mobile”插入 1,为“Other Mobile”插入 0。
如果您要插入“触摸手机”的信息,则在“MobileType”字段中输入 1,并在相关字段中输入所有与触摸手机相关的信息。将“其他移动”相关字段保留为 NULL。对“其他手机”类型条目执行相反的操作。
使 MobileType、Price、Modile 不是空字段和其他空字段并添加以下约束条件,这将强制您的条件:
ALTER TABLE mobile ADD CONSTRAINT CK_mobile CHECK(
Refreshrate is null and Resolution is not null and Latency is not null and Isblackandwhite is null and Screensize is null
OR
Refreshrate is null and Resolution is null and Latency is null and Isblackandwhite is not null and Screensize is not null
)
我将只创建一个 table 并通过用户界面管理任何必填字段作为数据验证的一部分。我不会通过 table 字段定义来管理它。
如果您真的想创建必填字段,请为它们提供默认值,稍后您可以根据这些值进行过滤。
创建多个 table 是可能的,但管理起来很容易变得麻烦。
我有一个 SQL table mobile
。对于不同的手机,我想保存不同的数据。例如,在触摸移动设备的情况下,这些列是必需的。
- 刷新率
- 分辨率
- 延迟
对于其他手机,这些列是必需的。
- 是黑白的
- 屏幕尺寸
除此之外,还有一些通用列在两个类别中都是必需的,例如
- 价格
- 型号
那么我该如何设计这个table。我应该创建两个单独的 table 吗?或者只是一个但我如何处理不同类别的必需属性?或者其他方法?
创建一个包含所有这些列的单个 table 以及一个名为 MobileType 的新列。您可以为“Touch Mobile”插入 1,为“Other Mobile”插入 0。 如果您要插入“触摸手机”的信息,则在“MobileType”字段中输入 1,并在相关字段中输入所有与触摸手机相关的信息。将“其他移动”相关字段保留为 NULL。对“其他手机”类型条目执行相反的操作。
使 MobileType、Price、Modile 不是空字段和其他空字段并添加以下约束条件,这将强制您的条件:
ALTER TABLE mobile ADD CONSTRAINT CK_mobile CHECK(
Refreshrate is null and Resolution is not null and Latency is not null and Isblackandwhite is null and Screensize is null
OR
Refreshrate is null and Resolution is null and Latency is null and Isblackandwhite is not null and Screensize is not null
)
我将只创建一个 table 并通过用户界面管理任何必填字段作为数据验证的一部分。我不会通过 table 字段定义来管理它。
如果您真的想创建必填字段,请为它们提供默认值,稍后您可以根据这些值进行过滤。
创建多个 table 是可能的,但管理起来很容易变得麻烦。