Jan 12 2009
Transact-SQL – Auto Increment Primary Key
If like me you learnt SQL using MySQL, and then gone into a job and found yourself using Microsoft’s SQL Server, you’ll probably be as irritated as me to find that Transact-SQL does not feature the ‘AUTO INCREMENT’ keyword that MySQL provides. Fortunately, the solution is far easier here than it is in PostgreSQL.
All you need is to add IDENTITY(1,1) in your column definition. I’m not going to pretend to understand exactly what this does, only that it will Automatically increment and ID field for you. If anyone wants to enlighten me on exactly what it does, then feel free :)
An Example is below:
1: CREATE TABLE dbo.myTable(
2: ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
3: ColumnA VARCHAR(200),
4: ColumnB BIT
5: )
6:
No responses yet