Domanda

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.

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).

I was trying to constrain the columns products_options_values_id with 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:

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

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.

This is a snippet of the mssql diagram: enter image description here

È stato utile?

Soluzione

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

Composite FK without all composite PK

As long as there's a unique index on column One, Three, then they those two together can be used as 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.

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.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top