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!

Primary Key for table(s) in a Sql Server 2005 Database ?

Status
Not open for further replies.

c0deM0nK424

Programmer
Oct 28, 2007
126
GB
Time is very short - and essentially I'm a newbie to SQL Server 2005...I had to change the settings to enable remote connections so I can run it on my PC.

I am moving from MS Access to SQL Server 2005, and have gotten as far as being able to create tables and populate them with data - however I want to know how I can achieve the equivalent of 'auto number' in SQL Server 2005, in that each time a new record/row is added to a table and I hit tab, a primary key is assigned automatically to that row.

I selected the column I wanted as Primary Key but thats all I've done, oh of course I set it as 'int' type.

Surely that's not enough information for the P.K in this table ? So how do I go abouts making it step ascendingly in that its Auto-Generated (and incremented) by 1 ?


 
Okay...i THINK i've achieved this, Identity specification right ? toggle it to 'yes' and then ensure its values are (1,1) (i.e start at value 1, and increment by 1) ?

what if i wanted the ID to start with two letters and then a number that increments by 1 ?

i.e SKID_1, SKID_2, SKID_3,
 
These are questions for the SQL Server forums. However, from Bools On Line(SQL Server Help), here are the rules for an Identity column:

BOL said:
A table can have only one column defined with the IDENTITY property, and that column must be defined by using a decimal, int, numeric, smallint, bigint, or tinyint data type.


The seed and increment can be specified. The default value for both is 1.


The identifier column must not allow for null values and must not contain a DEFAULT definition or object.


The column can be referenced in a select list by using the $IDENTITY keyword after the IDENTITY property has been set. The column can also be referenced by name.


The OBJECTPROPERTY function can be used to determine whether a table has an IDENTITY column, and the COLUMNPROPERTY function can be used to determine the name of the IDENTITY column.


SET IDENTITY_INSERT can be used to disable the IDENTITY property of a column by enabling values to be explicitly inserted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top