问题
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