Q

Creating an autonumber in Access using DDL

I'm writing a query to create a table in Access with an AutoNumber column. I want to do this only using SQL (I won't go into why). However, since Autonumber is not a datatype, but a property, how can I set this using only SQL? Do you know the SQL syntax for creating an autonumber column in Access?

Yes, you can create a Microsoft Access autoincrement column using Data Definition Language (DDL). As described in the document Intermediate Microsoft Jet SQL for Access 2000, you declare it as a COUNTER datatype:

The COUNTER data type is used to store long integer values that automatically increment whenever a new record is inserted into a table. With the COUNTER data type, you can optionally set a seed value and an increment value... You can have only one COUNTER field in a table, and the synonyms are COUNTER, AUTOINCREMENT, and IDENTITY.

The above document also mentions that @@IDENTITY can be used to retrieve the value of the last counter number assigned. To the best of my knowledge, this feature was introduced in Access 2000. Always use this instead of techniques such as querying the table for MAX(id).

According to KB Article Q116145 ACC: Create and Drop Tables and Relationships Using SQL DDL, COUNTER in DDL is valid as far back as Access Version 2.0.

For More Information


This was first published in October 2002

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close