Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you create unique 1

Status
Not open for further replies.

Painkiller

Programmer
May 18, 2001
97
0
0
NL
Hi all,

I have the following question: I need to store data in a table on a SQL server database using a VB app. Is there a settign in SQL server that creates a unique primary key (a number) every time I add a new row to the table?? I know this setting exists in MS Access, but I can't find it in the SQL server enterprice manager.

Thanx in advance.

Sujesh
 
Try to make use of the uniqueidentifier datatype and NEWID function:

create table test(
ID uniqueidentifier NOT NULL default NEWID(),
...
 

SQL BOL states, "The uniqueidentifier data type stores 16-byte binary values that operate as globally unique identification numbers (GUID). A GUID is a binary number that is guaranteed to be unique; no other computer in the world will generate a duplicate of that GUID value. The main use for a GUID is for assigning an identifier that must be unique in a network that has many computers at many sites."

Typically when people refer to a "unique primary key (a number)" they mean an identity column. Identity columns are generated sequentially which as simple integers.

To create the identity column with T-SQL:

Create Table test(ID INT IDENTITY (1,1), ....

In Enterprise Manager, create the column as Integer data type and check on IDENTITY. Set the starting and increment values. Terry L. Broadbent
faq183-874 contains some tips and ideas for posting questions in these forums. Please review it and comment if you have time.
NOTE: Reference to the FAQ is part of my signature and is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top