Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server View Primary Key

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.

like image 618
PeteT Avatar asked Aug 19 '10 12:08

PeteT


People also ask

Can SQL view 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.

How do I find primary key in SSMS?

Expand Server- Database – tables – keys – right click key name and click modify, do the same process you did in SQL Server 2000, first you will uncheck allow null on that column, and then you will press control button and you will select all the columns you want to have in primary keys and then right click on those ...

How do I find the primary key of a database?

To add to a response from @Ashraf, you can use select OBJECT_NAME(parent_object_id), * from sys. key_constraints where type = 'PK' . This will give you the table name and their primary key names for the tables that have a primary key.


1 Answers

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.

like image 112
D'Arcy Rittich Avatar answered Sep 28 '22 19:09

D'Arcy Rittich