SQL Server View Primary Key

五迷三道 提交于 2020-01-02 03:36:05

问题


Is there a way to give a view a primary key in 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.


回答1:


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.




回答2:


Indexed view is the correct choice but a primary key as RedFilter said is not actually required. Though there are many requirements that you will be forced to have such as a clustered index.




回答3:


Within MS Access you can create a Primary-key on the linked view using the SQL statement:

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

That said, using an Indexed View is the better solution.



来源:https://stackoverflow.com/questions/3521609/sql-server-view-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!