How to add a PK column to a View?

How to add a PK column to a View?

am 05.11.2007 22:31:31 von cjeffwang

The new table I need for an ASP project includes a PK. After creating
a View, how can I add a new PK column, which will add the increaing
integer numbers automatically? Or how do I code to
create a PK column automatically with the new View?

TIA,
Jeffrey

Re: How to add a PK column to a View?

am 05.11.2007 23:40:46 von Erland Sommarskog

cjeffwang@gmail.com (cjeffwang@gmail.com) writes:
> The new table I need for an ASP project includes a PK. After creating
> a View, how can I add a new PK column, which will add the increaing
> integer numbers automatically? Or how do I code to
> create a PK column automatically with the new View?

I'm not really sure what you and what you have not. And where the
view comes into the picture.

If you have a an existing table add need to extend the PK with a new column,
it depends on what you want to put into that column. You talk about
increasing integers automatically, but I don't really see the point
with adding such a column to an existing PK.

To add an IDENTITY column you can do

ALTER TABLE tbl ADD ident int IDENTITY

and you will be all set up. Then again, think twice if this is really
what you need. Lots of people put IDENTITY on their tables, and then
need to deal with all sorts of kludges when the IDENTITY gets in the
way.

The prime reason to use IDENTITY is that the insertion ratio is expected
to be high.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx