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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 A quick Question

Author  Topic 

wangyc77
Yak Posting Veteran

65 Posts

Posted - 2007-01-31 : 00:35:18
I used [select into] statement to create a table, but the table has no primary key.
How do I create a primary key for that existing table using SQL and the primary key(called Sno field) should be increamental.
Thank you

Jon

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-31 : 00:45:35
[code]
ALTER TABLE dbo.tbl ADD CONSTRAINT
PK_tbl PRIMARY KEY
(
pk_col
)
[/code]

is Sno an existing column ?


KH

Go to Top of Page

wangyc77
Yak Posting Veteran

65 Posts

Posted - 2007-01-31 : 00:46:16
nope
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-31 : 00:50:31
[code]
alter table dbo.tbl add Sno int not null identity(1,1)

ALTER TABLE dbo.tbl ADD CONSTRAINT
PK_tbl PRIMARY KEY
(
Sno
)
[/code]


KH

Go to Top of Page

wangyc77
Yak Posting Veteran

65 Posts

Posted - 2007-01-31 : 00:56:54
Thanks a lot, save me lots of time

**Jonathan**
Go to Top of Page

wangyc77
Yak Posting Veteran

65 Posts

Posted - 2007-01-31 : 01:03:35
Just curious,
what does that
identity(1,1)
mean?

**Jonathan**
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-31 : 01:06:36
from BOL
quote:

IDENTITY (Property)
Creates an identity column in a table. This property is used with the CREATE TABLE and ALTER TABLE Transact-SQL statements.



Note The IDENTITY property is not the same as the SQL-DMO Identity property that exposes the row identity property of a column.


Syntax
IDENTITY [ ( seed , increment ) ]




KH

Go to Top of Page
   

- Advertisement -