Ask the Expert

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?

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: