简体   繁体   English

属性的SQL验证-不同的数据类型

[英]SQL Validation of attributes - Different Data Types

I have a table that holds all the attribute information for a series of codes. 我有一个表,其中包含一系列代码的所有属性信息。 An attribute code is set up to hold only a certain data type. 设置属性代码以仅容纳某种数据类型。 These include numeric, free text and dropdown values from a stored list. 这些包括存储列表中的数字,自由文本和下拉值。

I have created a table that holds all the attribute codes and their allowed values. 我创建了一个表,其中包含所有属性代码及其允许的值。 I am trying to come up with a query that will validate each of the attributes on each of the codes based on the values in the validation table. 我正在尝试提出一个查询,该查询将基于验证表中的值验证每个代码上的每个属性。

I'm trying to stay away from cursors as there is a large amount of codes that need to be checked and I know cursors do not perform very fast with a large number of rows. 我试图远离游标,因为需要检查大量代码,而且我知道游标在大量行中的执行速度不是很快。

Example of Data Table: 数据表示例: 在此处输入图片说明

Example of Validation Table: 验证表示例: 在此处输入图片说明

You will notice from the LIST data types that they have a corresponding LIST_CODE and allowed LIST_VALUE. 您会从LIST数据类型中注意到,它们具有对应的LIST_CODE和允许的LIST_VALUE。

While I'm reading your question again, you could do the following: 当我再次阅读您的问题时,您可以执行以下操作:

  1. In your validation table you do not need the attributes NUMERIC_ATTR, FREE_TEXT_ATTR and FLOAT_ATTR 在验证表中,您不需要属性NUMERIC_ATTR,FREE_TEXT_ATTR和FLOAT_ATTR

You simmply declare the columns in your data-Table as int, nvarchar(300) and float. 您只需将数据表中的列声明为int,nvarchar(300)和float。 A datatype is also a constraint and SQL-Server takes care that only values can be stored in a column that are allowed by the datatype. 数据类型也是一个约束,SQL-Server会注意只能将值存储在该数据类型允许的列中。

You can do the same with your Attribute 0_OR_1. 您可以对属性0_OR_1执行相同的操作。 Just use the datatype bit in your data table. 只需使用数据表中的数据类型位即可。 If you can't use bit for any reason and use tinyint you could add a CHECK CONSTRAINT to allow only the values 0 or 1: 如果由于某种原因不能使用bit并使用tinyint,则可以添加CHECK CONSTRAINT以仅允许值0或1:

ALTER TABLE DataTable
ADD CONSTRAINT CK_allow_only_0_or_1
CHECK ([0_OR_1_ATTR] = 0 OR [0_OR_1_ATTR] = 1);
  1. Use a foreign key constraint for your dropdown You could create a ForeignKey-Constraint to store only allowed values for your dropdown 对下拉列表使用外键约束您可以创建ForeignKey-Constraint来仅存储下拉列表允许的值

    ALTER TABLE DataTable ADD CONSTRAINT FK_DropDownElements FOREIGN KEY (DROPDOWN_ATTR) REFERENCES ValidationTable (LIST_VALUE); ALTER TABLE DataTable添加约束FK_DropDownElements外键(DROPDOWN_ATTR)参考ValidationTable(LIST_VALUE);

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

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