简体   繁体   English

SQL Server条件外键

[英]SQL Server Conditional Foreign Key

I have two tables in my SQL Server database, Foo and Bar. 我的SQL Server数据库中有两个表Foo和Bar。 Table Foo is like so: Table Foo就像这样:

+-------+
|  Foo  |
+-------+
| Id    |
| Type  |
| Value |
+-------+

The table has values like: 该表的值如下:

+----+--------+-----------+
| Id |  Type  |   Value   |
+----+--------+-----------+
|  1 | Status | New       |
|  2 | Status | Old       |
|  3 | Type   | Car       |
|  4 | State  | Inventory |
|  5 | State  | Sold      |
+----+--------+-----------+

The table Bar is like so: 表格栏是这样的:

+----------+
|   Bar    |
+----------+
| Id       |
| TypeId   |
| StatusId |
| StateId  |
+----------+

Where TypeId , StatusId and StateId are all foreign key'ed to the Foo table. 其中, TypeIdStatusIdStateId都是对Foo表的外键。 But I want to put a condition on each foreign key where they can only key to the Foo ids related to it's type. 但是我想在每个外键上设置一个条件,使它们只能输入与其类型相关的Foo ID。 For example, the TypeId column can ONLY foreign key to id 3 on the Foo table. 例如, TypeId列只能在Foo表上使用ID 3的外键。 Or the StatusId column can ONLY foreign key to ids 1 or 2. StatusId列只能将外键指向ID 1或2。

I know there is a check function in SQL Server but I'm unsure on how to use it correctly. 我知道SQL Server中有一个检查功能,但是我不确定如何正确使用它。 I tried to do something like this: 我试图做这样的事情:

CREATE TABLE TEST.dbo.Bar
(
    Id int PRIMARY KEY NOT NULL IDENTITY,
    TypeId int NOT NULL CHECK (Type='Type'),
    CONSTRAINT FK_Bar_Foo_Type FOREIGN KEY (TypeId) REFERENCES Foo (Id, Type)
)
CREATE UNIQUE INDEX Bar_Id_uindex ON TEST.dbo.Bar (Id)

But this didn't work. 但这没有用。 What am I doing wrong? 我究竟做错了什么?

The check constraints you are referring to are only used to limit the type of information stored in a key or non key column. 您引用的检查约束仅用于限制存储在键或非键列中的信息类型。 So, if you don't want a key column to have a negative value (lets say its a price column, and there is never a negative price) you will use Check constraint. 因此,如果您不希望键列具有负值(可以说它是价格列,并且永远不会有负价),则可以使用Check约束。

To better understand the concept of primary and foreign keys: 为了更好地理解主键和外键的概念:

Primary key uniquely identifies each record in a table. 主键唯一地标识表中的每个记录。 Foreign key is a value in some table which is a unique identifier (and can also be a primary key) in another table. 外键是某个表中的值,它是另一个表中的唯一标识符(也可以是主键)。 This means that Foreign key can repeat many times in the table in which it is a foreign key in, and it will definitely be unique in the table that it is created from ( in the table that gives meaning to it). 这意味着外键可以在其中作为外键的表中重复很多次,并且在创建它的表中(在赋予它含义的表中)绝对是唯一的。

Now coming to your question, you probably need to use the concept of composite keys. 现在提您的问题,您可能需要使用复合键的概念。 A composite key is basically a group of two or more values that uniquely identify a record, because you cannot enforce limitations on foreign keys in the way you are intending to do, because that defeats the very purpose of a key. 组合键基本上是由两个或多个值组成的组,它们唯一地标识一条记录,因为您不能以您打算的方式对外键进行限制,因为这违反了键的用途。 Handle some issues with type of data stored in your keys at the application layer instead of database layer. 处理应用程序层而不是数据库层中存储在密钥中的数据类型的某些问题。

Looking at the problem in this manner will conceptually resolve some design flaws with your tables as as well. 以这种方式查看问题还将从概念上解决表格的一些设计缺陷。

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

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