需要推荐表结构

时间:2010-06-18 07:48:15

标签: sql-server database-design

我有一个实体,它有4种不同类型的属性,每种情况只能有一个值,分别是布尔值,十进制,字符串或文本。我不想用4个boolean,decimal,nvarchar和ntext列定义表。你会建议怎么报道这个案子?

更新

我正在使用MS SQL Server。

这是类定义:

public class Foo
{
    public int Id { get; set; }
    public string Description { get; set; }
    public bool BooleanValue { get; set; }
    public decimal DecimalValue { get; set; }
    public string NVarcharValue { get; set; }
    public string NTextValue { get; set; }
} 

2 个答案:

答案 0 :(得分:1)

我假设您要使用MS SQL Server。

如果是这种情况,那么我就不会使用ntext此数据类型将在SQL Server的未来版本中删除。如果确实需要使用非Unicode数据类型,请使用nvarchar。使用ntext的另一个限制是您无法将其转换为许多其他数据类型。在30种奇数数据类型中,您只能将ntext转换为其中的六种。

在这种情况下你可以做的是具有以下表格结构。

Create Table dbo.PropertyInformation
(
PropertyId int identity(1,1) not null,
PropertyName varchar(50) not null,
PropertyTypeId int not null,
PropertyValue nvarchar(max)
)

然后添加查找表以存储您的属性类型

Create table dbo.PropertyTypes
(
PropertyTypeId int identity(1,1) not null,
PropertyType varchar(50) not null
)

您可以在PropertyValue列中存储任何类型的值,因为nvarchar转换为几乎所有其他数据类型 - 除了图像。从查找表中读取PropertyType,并在App中动态转换PropertyValue的值。

虽然,您是否有理由不想使用4个不同的列来存储属性值?

ntext datatype information

Casting & Converting in SQL Server(允许转化的矩阵大约是页面的一半)

答案 1 :(得分:0)

您可以为每个属性定义4个表,每个表的每一行都有一个到它所属实体的外键。

相关问题