SQL Server:复合键主键约束fk,无法理解SQL架构图

时间:2022-10-17 22:42:39

I was looking for a basic SQL diagram for an e-commerce site and I came across this example: http://addons.oscommerce.com/info/3853/page,23.

我正在寻找电子商务网站的基本SQL图,我看到了这个例子:http://addons.oscommerce.com/info/3853/page,23。

There are a lots of tables with composite primary keys but they seem to be constraint with just one foreign key.

有许多表具有复合主键,但它们似乎只有一个外键约束。

This is a capture of one the tables in subject:

这是主题中的一个表的捕获:

products_options_values_to_products_options and products_options_values
schema: http://sdrv.ms/1bIMx52 (compositkey.jpg).

products_options_values_to_products_options和products_options_values schema:http://sdrv.ms/1bIMx52(compositkey.jpg)。

I was trying to constrain the columns products_options_values_id with products_options_values_id

我试图用products_options_values_id约束products_options_values_id列

I'm using SQL Server 2012, and I've tried to constrain the composite keys with one fk, clearly SQL Server Management Studio gave me an error, stating:

我正在使用SQL Server 2012,并且我试图用一个fk约束复合键,显然SQL Server Management Studio给了我一个错误,说明:

The columns in table 'products_options_values(products)' do not match an existing primary key or UNIQUE constraint.

表'products_options_values(products)'中的列与现有主键或UNIQUE约束不匹配。

or

Both sides of a relationship must have the same number of columns

关系的两侧必须具有相同的列数

I obviously agree with the 2 statements, but how come in the diagram the constraint seems possible?

我显然同意这两个陈述,但图中怎么可能出现约束?

I'd like to replicate the whole database scheme but I would like to understand this point first.

我想复制整个数据库方案,但我想首先理解这一点。

Luther

edit: as pointed out by Allan S. Hansen, I've applied an "unique key" to the primary key and despite what I knew, it's working.

编辑:正如Allan S. Hansen指出的那样,我已经在主键上应用了一个“唯一键”,尽管我知道,它仍然有效。

This is a snippet of the mssql diagram: SQL Server:复合键主键约束fk,无法理解SQL架构图

这是mssql图的一个片段:

1 个解决方案

#1


3  

In context to my comment; this is completely possible in SQL Server:

在我的评论的背景下;这在SQL Server中是完全可能的:

SQL Server:复合键主键约束fk,无法理解SQL架构图

As long as there's a unique index on column One, Three, then they those two together can be used as FK.

只要第一列,第三列上有唯一索引,那么它们一起可以用作FK。

Where to use such a pattern, I'm not sure - because if PK_One can be defined by just One, Three, then I wouldn't think Two should be in that key, but well .... I've not given it much thought and I'm sure something can be thought up.

在哪里使用这样的模式,我不确定 - 因为如果PK_One只能由一,三来定义,那么我不会认为两个应该在那个键中,但是......我没有给它很多想法,我确信可以想到一些事情。

That's also why you received the error message:

这也是您收到错误消息的原因:

The columns in table 'products_options_values(products)' do not match an existing primary key or UNIQUE constraint.

表'products_options_values(products)'中的列与现有主键或UNIQUE约束不匹配。

Remember, Primary Keys and Unique indexes aren't the same.

请记住,主键和唯一索引不一样。

#1


3  

In context to my comment; this is completely possible in SQL Server:

在我的评论的背景下;这在SQL Server中是完全可能的:

SQL Server:复合键主键约束fk,无法理解SQL架构图

As long as there's a unique index on column One, Three, then they those two together can be used as FK.

只要第一列,第三列上有唯一索引,那么它们一起可以用作FK。

Where to use such a pattern, I'm not sure - because if PK_One can be defined by just One, Three, then I wouldn't think Two should be in that key, but well .... I've not given it much thought and I'm sure something can be thought up.

在哪里使用这样的模式,我不确定 - 因为如果PK_One只能由一,三来定义,那么我不会认为两个应该在那个键中,但是......我没有给它很多想法,我确信可以想到一些事情。

That's also why you received the error message:

这也是您收到错误消息的原因:

The columns in table 'products_options_values(products)' do not match an existing primary key or UNIQUE constraint.

表'products_options_values(products)'中的列与现有主键或UNIQUE约束不匹配。

Remember, Primary Keys and Unique indexes aren't the same.

请记住,主键和唯一索引不一样。