SQL Server - Set Primary Key to SQL View - Asked By Naresh Kumar on 28-Dec-10 02:35 AM

Hi all,

Can we set the primary key to SQL veiw, if it is possible then please help me.

Thanks in advance.
Gorav Handa replied to Naresh Kumar on 28-Dec-10 02:41 AM
You can not add a primary key constraint to views. You can join columns though. In the join condition, you do not have to use a primary key constraint.

SELECT ...
FROM view1 v1
INNER JOIN view2 v2
ON v1.vendor_id = v2.vendor_id

The type of join to use is based upon your data requirement. In the example above, I am using an inner join. But you'd have to tell us what you want in order for us to help.
Reena Jain replied to Naresh Kumar on 28-Dec-10 02:43 AM
hi,

As I know You can not add a primary key constraint to views. You can join columns though. In the join condition, you do not have to use a primary key constraint

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
hope this will help you
Naresh Kumar replied to Reena Jain on 28-Dec-10 02:54 AM
Thanks for your reply,

I am creating a Report model which displays all the columns in the view, but while creating this it is asking for Primary key.

Can you please let me know how can I accomplish this.