SQL Server 视图可以有主键和外键吗?

发布于 2024-07-17 10:15:39 字数 516 浏览 11 评论 0原文

是否可以在 Microsoft SQL Server Management Studio 中定义数据库视图的主键和外键? 如何?

我正在尝试创建一个 ADO.NET 实体数据模型来读取四个我无法修改的旧的、格式不正确的数据库表。 我已经创建了我需要的数据的视图。

四个视图应映射到具有多对多关系的简单三实体 EDMX。

创建数据模型时出现此错误:

表/视图“...”没有 主键已定义但无效 可以推断出主键。 这 表/视图已被排除。 使用 您需要审查的实体 您的架构,添加正确的键并 取消注释。

它正确推断了两个视图的主键。 但与另外两个人却未能做到这一点。

我的问题视图之一使用聚合函数:

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

另一个应该具有两个外键的复合主键。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-07-24 10:15:39

您需要定义视图,以便它:

  • 包含所有 PRIMARY KEY
  • 不使用任何 JOIN
  • 不使用任何聚合函数或 UNION's

视图中的任何行都应该映射到表中的一行。

我的一个问题视图使用聚合函数

它无法更新。 对于只读实体,来自 此处

当无法推断出键时,包含相应 EntityType 元素(无 Key 元素)的代码注释将添加到 .edmx 文件的 SSDL 部分。

就您而言,由于您似乎想要一个只读实体,因此您可以:

  1. 取消注释 SSDL 实体
    • 将一个/某些属性标记为 Nullable="False"
    • 添加适当的关键元素
    • 添加相应的定义查询。

对于第二个问题:

另一个应该有两个外键的复合主键

来自 文档< /强>

表示数据库中两个表之间的多对多关系的表在概念架构中可能没有等效实体。 当 EDM 工具遇到这样一个表,除了作为外键的两个列之外没有其他列时,映射表在概念模式中表示为多对多关联而不是实体。< /p>

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.

攒一口袋星星 2024-07-24 10:15:39

您可以通过在视图中创建一个 NOT NULL 索引列来更改视图,执行如下操作:

ALTER VIEW [dbo].[ViewName]
AS
    SELECT  ISNULL(CAST(CASE ROW_NUMBER() OVER ( ORDER BY columnNames )
                          WHEN ROW_NUMBER() OVER ( ORDER BY columnNames )
                          THEN ROW_NUMBER() OVER ( ORDER BY columnNames )
                          ELSE 0
                        END AS INT), 0) AS ID 

You can alter your views by creating a NOT NULL index column in your view doing something like this:

ALTER VIEW [dbo].[ViewName]
AS
    SELECT  ISNULL(CAST(CASE ROW_NUMBER() OVER ( ORDER BY columnNames )
                          WHEN ROW_NUMBER() OVER ( ORDER BY columnNames )
                          THEN ROW_NUMBER() OVER ( ORDER BY columnNames )
                          ELSE 0
                        END AS INT), 0) AS ID 
同尘 2024-07-24 10:15:39

实际上,您可以创建一个使用 JOIN 的视图,并从中生成模型中的实体。

Actually, you can create a view that uses JOIN's and generate and Entity in your model from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文