SQL server uses the Identity Specification to add a numbered column that automatically increments. You can add this to a new table you are designing or to an existing one. SQL uses integer as the data type, so you are restricted to integers for your automatic identity. Available integers can be positive or negative and range from -2,147,483,648 to 2,147,483,648.
This is what you will need:
- Column name, such as “RowID”
- Data type: integer (int)
- Identity Seed: This is the starting number. It defaults to 1, but you can set it to any integer in the range listed above.
- Identity Increment: This is the amount the number will auto-increment. It defaults to 1.
Follow these steps to add a new identity column.
- Open SQL Server Management Server
- right-click your table and select “Design”
- Add a new column and give it an appropriate name like ID or RowID
- Set the datatype to “int”
- Turn off “Allow Nulls”
Follow these steps to configure your new column.
If you are not familiar with the interface, there are a few notes added to the end that can help you navigate the Column Properties.
- Open “Identity Specification”
- Change “(Is Identity)” to “Yes”
- If needed, change the default Identity Seed and Identity Increment from 1 to an appropriate number
- Example Identity setting: Seed is 110 and increment is 10. This means the first five IDs are: 110, 120, 130, 140, and 150.
- Example Identity setting: Seed is 10001 and increment is 1. This means the first five IDs are: 10001, 10002, 10003, 10004, and 10005.
Notes to help you navigate the Column Properties
- Make sure your cursor is on your new ID column
- In the Column Properties section, sections are open or closed based on the arrow in front of them. If the arrow points to the right, the section is closed. If the arrow points down, the section is open.
- You can open a section within a section
- You will need to open the “Identity Specification” section, which is inside the “Table Designer” section
- By default, “Table Designer” is open and “Identity Specification” is closed.