Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 create view with primary key constraint

Author  Topic 

Jadexy
Starting Member

3 Posts

Posted - 2010-06-18 : 08:47:40
Hallo,

I want to create a view with a primary key constraint similar to oracle's

ALTER VIEW V_VIEW
ADD CONSTRAINT PK_V_VIEW PRIMARY KEY(ID)
DISABLE NOVALIDATE;

Can this be done in SQL Server 2005?

Thanks in advance, Jade

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-06-18 : 09:06:59
You can't create primary key constraint for a view in sql server.

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-06-18 : 09:30:08
You can however create unique indexes on views.
Go to Top of Page

Jadexy
Starting Member

3 Posts

Posted - 2010-06-18 : 09:51:50
And how do you mark the primary key columns?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-06-18 : 10:19:15
It's documented in Books Online under "CREATE INDEX".

Keep in mind it's not an actual primary key but a unique constraint, as it may allow nulls. It accomplishes the same goal however.
Go to Top of Page

Jadexy
Starting Member

3 Posts

Posted - 2010-06-18 : 10:21:56
Books online? Where?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-06-18 : 10:34:19
They're installed with SQL Server in the same Start Menu group as SQL Server Management Studio (Documentation and Tutorials). They're also available online at MSDN.
Go to Top of Page
   

- Advertisement -