简体   繁体   English

SQL Server视图主键

[英]SQL Server View Primary Key

Is there a way to give a view a primary key in sql server. 有没有办法在sql server中为视图提供主键。 I know it is possible in oracle. 我知道在甲骨文有可能。

I am not concerned about updates its a read only view but someone else is using it in ms access so I would like the constraint that I know to be correct to be shown. 我不关心更新它的只读视图,但其他人在ms访问中使用它,所以我希望我知道的约束是正确的显示。

Yes, you can create an indexed view , which must have a primary key. 是的,您可以创建索引视图 ,该视图必须具有主键。 Note, this will persist the view data to disk, which may or may not be what you are looking for. 请注意,这会将视图数据保留到磁盘,这可能是您正在寻找的,也可能不是。

Also, creation of indexed views can also impact performance, both positively and negatively. 此外,创建索引视图也会影响性能,无论是积极的还是消极的。 Make sure you read up on the pros, cons, and limitations thoroughly before implementing. 确保在实施之前仔细阅读优缺点和限制。

Within MS Access you can create a Primary-key on the linked view using the SQL statement: MS Access您可以使用SQL语句在链接视图上创建主键:

ALTER TABLE [TableName] ADD CONSTRAINT [PrimaryKeyName] PRIMARY KEY ([FieldName1, FieldName2, ...])

That said, using an Indexed View is the better solution. 也就是说,使用索引视图是更好的解决方案。

Indexed view is the correct choice but a primary key as RedFilter said is not actually required . 索引视图是正确的选择,但实际上并不required RedFilter所说的主键。 Though there are many requirements that you will be forced to have such as a clustered index. 虽然您将被迫拥有许多要求,例如聚集索引。

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

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