Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SQL Server views have primary and foreign keys?

Is it possible to define primary and foreign keys for database Views in Microsoft SQL Server Management Studio? How?

I'm trying to create an ADO.NET Entity Data Model to read from four old, poorly-formed database tables that I cannot modify. I've created views of just the data I need.

The four views should map to a simple three-entity EDMX with one many-to-many relationship.

I get this error when creating my Data Model:

The table/view '...' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity you will need to review your schema, add the correct keys and uncomment it.

It correctly inferred the primary keys of two views. But failed to do so with the other two.

One of my problem views uses aggregate functions:

SELECT MAX(...) ... GROUP BY ...

The other ought to have a compound primary key of two foreign keys.

like image 677
Zack Peterson Avatar asked Apr 13 '09 20:04

Zack Peterson


People also ask

Can SQL views have foreign keys?

Sorry, In the strict sense of the word, no you cannot set foreign keys on views. Here is why: InnoDB is the only built-in storage engine for MySQL that features foreign keys. Any InnoDB table will be registered in information_schema.

Can SQL views have primary key?

You cannot create a primary key on a view. In SQL Server you can create an index on a view but that is different to creating a primary key. If you give us more information as to why you want a key on your view, perhaps we can help with that.

Can a table have both primary key and foreign key?

A table can have only one Primary Key. A table can have any number of Foreign Keys. The primary key is unique and Not Null. A foreign key can contain duplicate values also.


1 Answers

You need to define your view so that it:

  • Includes all the PRIMARY KEY columns
  • Does not use any JOIN's
  • Does not use any aggregate functions or UNION's

Any row from your view should map to exactly one row from the table.

One of my problem views uses aggregate functions

It cannot be updateable. For a readonly entity, a solution from here:

When no key can be inferred, a code comment that contains the corresponding EntityType element (with no Key elements) is added to the SSDL section of the .edmx file.

In your case, since it seems that you want a read only entity, you could:

  1. uncomment the SSDL entity
    • mark one/some properties as Nullable="False"
    • add the appropriate Key elements
    • add a corresponding defining query.

For the second question:

The other ought to have a compound primary key of two foreign keys

From documentation:

A table that represents a many-to-many relationship between two tables in the database may not have an equivalent entity in the conceptual schema. When the EDM tools encounter such a table with no columns other than the two that are foreign keys, the mapping table is represented in the conceptual schema as a many-to-many association instead of an entity.

like image 161
Quassnoi Avatar answered Sep 18 '22 11:09

Quassnoi