SQL Server - autoincrement

Asked By rehu
09-Jul-10 08:39 AM

how to add autoincrement column in MSSQl

ALTER TABLE test ADD idd SMALLINT AUTO_INCREMENT  PRIMARY KEY 

  Mash B replied to rehu
09-Jul-10 08:42 AM

Try below code

 

alter table test
    add idd bigint identity( 1, 1 ) not null
go

  Mash B replied to Mash B
09-Jul-10 08:45 AM

If you want the particular column to be primary key just add PRIMARY KEY to my code as shown below

 

alter table test
    add idd bigint not null identity(1,1) primary key
go

  Sagar P replied to rehu
09-Jul-10 08:57 AM

AUTO_INCREMENT is a MySql keyword. For SQL you need to use IDENTITY like;

 

CREATE TABLE new_employees
(
id_num int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
);

So in your case use it like;

 

ALTER TABLE test ADD idd SMALLINT IDENTITY(1,1) PRIMARY KEY 

  Anoop S replied to rehu
09-Jul-10 11:29 AM
In my SQL AUTO_INCREMENT is used, In Sql its identity
CREATE TABLE tablename (
   id MEDIUMINT NOT NULL AUTO_INCREMENT,
   col2 CHAR(30) NOT NULL,
   PRIMARY KEY (id)
 );

  Super Man replied to rehu
09-Jul-10 12:50 PM

Syntax for MySQL

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

SQL statement:

CREATE TABLE Persons

(

P_Id int NOT NULL AUTO_INCREMENT,

FirstName varchar(255),

PRIMARY KEY (P_Id)

)

ALTER TABLE Persons AUTO_INCREMENT=100

To start value initially from 100.

 

Syntax for SQL Server

 

The following SQL statement defines the "P_Id" column to be an auto-increment primary key field in the "Persons" table:CREATE TABLE Persons

(

P_Id int PRIMARY KEY IDENTITY,

FirstName varchar(255),

)

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.

By default, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.

To specify that the "P_Id" column should start at value 10 and increment by 5, change the identity to IDENTITY(10,5).

Create New Account
help
ms sql & mysql SQL Server can i run both on the same server? SQL Server Discussions SQL Server (1) MySQL (1) SQL Server Books Online (1) Microsoft sql (1) Online (1) Mindspring
SQL Server SQL Server Security Discussions SQL Server (1) Role (1) Privileges (1) Database (1) Hi, The following excerpt is taken from Chapter 5 - Microsoft SQL Server 2000 Security of Microsoft SQL Server 2000 Administrator's Pocket Consultant (ISBN 0-7356-1129
Compound INSERT SELECT FROM WHERE subquery problem MySQL -> SQL Server 2005 conversion SQL Server , SQL, Server, 2005, conversion" / > This SQL language is nuts, just nuts. How does anybody put up with such a crude scripting
What's the go with sql server SQL Server I was just saying today that sql server is full of silly limitations and I seem to hit on a new one every features with more ways to manipulate things than you can poke a stick at. In sql server we don't even have a full range of date functions (a pretty basic feature
SQL Server Migration Assistant Wizard for ACCESS - can't connect to SQL Server SQL Server Hello - - The Wizard leads me through finding and adding the mdb files, and then shows the following error in the step "Connect to SQL Server 2005": an instance of SQL Server 2005." Relevant information about my SQL Server 2005 version