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)
 Auto Inrementing ID

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-12 : 09:08:49
Dimitri writes "When creating a table using SQL, I create a a field called ID, that stores a unique number to identify each record in the table. My question is how to modify the SQL statement to create an ID field that will automatically increment for each new record that is added to the table.
(could this have anything to do with creating a Primary Key?)

my current SQL statement is:
CREATE TABLE table1 (ID INTEGER, body TEXT, category TEXT, title TEXT);"

Nazim
A custom title

1408 Posts

Posted - 2002-02-12 : 09:49:07
Check for Identity in BOL.

this should do it
CREATE TABLE table1 (ID INTEGER identity(1,1), body TEXT, category TEXT, title TEXT);"

--------------------------------------------------------------
"Happiness is not something you experience, it's something you remember."
Go to Top of Page

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-02-12 : 09:49:25
The identity property was created just for this purpose.

Your table would look like...

create table foo
(
Id integer identity(1,1)
...
)

See books online for more information.

Justin

Go to Top of Page

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-02-12 : 09:50:30
Crap! Nazim sniped me. A pox on you (just kidding).

Justin

Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-02-12 : 10:00:36
This has nothing to do with your question, but I highly recommend you reconsider using datatype TEXT for all those fields... I bet that title and category can easily be accomodated by VARCHAR datatype, which is faster, less resource-intensive, and MUCH easier to work with.

Go to Top of Page
   

- Advertisement -