Setting Primary Key auto increment with Alter Command [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add auto_increment to a column in SQL Server 2008
Hey all i created the table in gui however i forgot to set the primary key to autoincrement! name of the table is Emp_CV.
Something like this:
alter table Emp_CV alter column Applicant_ID NOT NULL int AUTO_INCREMENT
how can i accomplish this?
Am using SQL Server 2008!

The short answer is...you can't. Here's what you have to do:
Ensure nobody/nothing is using your table.
Add a new integer column that will be the new identity column. It must allow nulls and must have the appropriate identity value.
execute set identity_insert dbo.foo on -- must specify your table name.
Seed it with the values of the existing identity column:
update dbo.foo set new_id = id
Drop any keys/constraints in which the existing identity column participates.
execute set identity_insert dbo.foo off
Drop the existing column.
Alter the new column, changing its nullity to not null.
Execute the command dbcc checkident( {your-table-name-here} , reseed ).
Execute sp_rename and give the new column the same name as the old column.
Recreate the keys/constraints dropped in step #4.
Might want to run sp_recompile on any stored procedures referencing this table.
Easy!

Related

Set IDENTITY to existing column

I have a table which is having a column name ID field of integer type. It was declared IDENTITY. And it has data according to IDENTITY. But recently I removed IDENTITY column from that table. Now I want to change that to IDENTITY again. But this query says incorrect syntax
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);
But I can perform the same task using SQL server designer in properties of the table.
What am I doing wrong here?
I think The identity column will hold the sequence of number thats why it thrown error
better you drop column then create it again and set IDENTITY
Alter Table FuleConsumptions Drop Column TransactionID
Go
ALTER TABLE FuleConsumptions
ADD TransactionID int;
go
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);

How do I alter table and change column to identity(5,10) [duplicate]

This question already has answers here:
SQL Server, How to set auto increment after creating a table without data loss?
(7 answers)
Closed 8 years ago.
I want do SQL query of alter to change column int type from normal to identity with seed 10 and start 5
how I do that?
I mean something like:
ALTER TABLE Persons MODIFY ID Identity(5,10)
ALTER TABLE Table3 ALTER COLUMN AutoINC set Idenity(5,2)
to change he's identity
You can't alter the existing identity column. You have to create new column for that.
ALTER TABLE Persons
ADD NewIdColumn int IDENTITY (5, 10);
Or as you want to put the same name then you can drop the existing column (make sure about your data loss) and then add it again with above statement.
You can't alter existing column as identity column
This link provides the options
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032/how-to-alter-column-to-identity11?forum=transactsql

How to remove auto increment from table in sql server 2012

I have created a table in SQL Server 2012 with primary key as auto increment. But how can I remove that auto increment property from the table using a SQL query?
If you need to keep the data in that column then create a new column on the table which is of the same type (but a different name), copy the data from the column you want to get rid of to the new one, drop the old column and rename the new. Complete example:
CREATE TABLE test(col1 INT IDENTITY (1,1) NOT NULL, col2 VARCHAR(10) NULL);
ALTER TABLE test ADD col3 INT NULL;
UPDATE test SET col3 = col1;
ALTER TABLE test DROP COLUMN col1;
EXEC sp_rename 'dbo.test.col3', 'col1', 'COLUMN';
The easiest way would be:
Open SQL Server Management Studio.
Locate Server > DataBase > Table.
Right Click on the Table > Select Design.
In the design window, Highlight the column you want to modify.
In the Column Properties Window browse to Identity Specification > Is Identity And set to No.
Go to the toolbar menu > Table Designer > Select Generate Change Script...
Walla, you got the requested script.
I Like using this method for getting scripts, since it allows me to generate scripts I'm not sure how to compose from scratch and thus learning and improving my skills...
If it's a primary key column, then you have to drop the PK first. If there's any tables referencing it, then you'll have to drop these FKs to be able to drop the PK. After that, add another column of the same type, update it with values from identity column, drop the identity column, rename the new column to whatever the name of identity column was (with sp_rename procedure), recreate the PK, recreate the FKs, check if everything went right.
I'd be very careful doing it on a production database. Ensure that noone can access the data while you're doing this.
I searched a lot to find a simple solution to remove the auto increment because i should do a lot of work if i drop the column and the primary key which was a foreign key on another table where I should remove the data that are using my foreign ... finally I ended up with a very simple solution that made my life easy:
SET IDENTITY_INSERT <table_name> ON ;
SET IDENTITY_INSERT [TABLE] OFF .. this allows to remove the auto increment to off state.., so that we have to enter the value in thatcolumn

SQL Server Management Studio Express crashes when I try to run an ALTER TABLE query to add a PK with auto increment

I have an existing table where I use existing column (type INT) as PK and manually increment its value with each row inserted. I wanted to change it to IDENTITY with auto increment. I found a thread here (http://stackoverflow.com/questions/4862385/sql-server-add-auto-increment-primary-key-to-existing-table) that seems to achieve exactly what I want. But every time I run the ALTER statement, Mgmt Studio crashes.
I had also tried to achieve my above goal by changing the column properties manually (Identity specification/Is Identity:yes) as in this thread (http://stackoverflow.com/questions/3876785/sql-server-cant-insert-null-into-primary-key-field). But every time I close the table after changing properties, I get an error
'Pix' table
Unable to modify table.
Cannot insert the value NULL into column 'picID', table 'photo.dbo.Tmp_Pix'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Not sure what's going on.
You cannot change an existing column to become an IDENTITY column.
What you need to do is:
create a new column with INT IDENTITY
drop the primary key constraint
drop the old column
add the primary key constraint on the new column
The trouble might be - if you already have data in that table - that the new identity values don't necessarily match the old values in your manual ID column.
If you need to preserve those, then it gets even more involved:
create a new table with the proper structure, and make sure that the ID column is INT IDENTITY
turn on IDENTITY_INSERT for that table
insert all the rows from the old table into the new one (and in the process, insert the old ID values into the new ID IDENTITY column)
turn off IDENTITY_INSERT for that table
drop the old table
possibly rename the new table

How to add identity to the column in SQL Server?

I have a table about 10 million rows. We just imported it from another database with SQL Server Management Studio. It creates table but without identity and primary key on primary key column.
I could add the primary key but couldn't add identity. It's all the time timing out when I'm doing it in designer. Even I set time out settings to 0.
I need to create probably another column set primary key and identity, copy data from old, delete old column and rename new one.
Can anyone show me what will be the best way to do it for such big tables, without additional overheating?
You cannot add IDENTITY to an existing column. It just cannot be done.
You'll need to create a new column of type INT IDENTITY and then drop the old column you don't need anymore (and possibly rename the new column to the old name - if that's needed)
Also: I would not do this in the visual designer - this will try to recreate the table with the new structure, copy over all data (all 10 millions rows), and then drop the old table.
It's much more efficient to use straight T-SQL statements - this will do an "in-place" update, non-destructive (no data is lost), and it doesn't need to copy around 10 millions rows in the process...
ALTER TABLE dbo.YourTable
ADD NewID INT IDENTITY(1,1) NOT NULL
When you add a new column of type INT IDENTITY to your table, then it will be automatically populated with consecutive numbers. You cannot stop this from happening, and you also cannot update the values later on.
Neither of those options is really very useful, in the end - you might end up with different ID values.... to do this right, you'd have to:
create the new table ahead of time, with the proper structure and the IDENTITY already in place
then turn on SET IDENTITY_INSERT (yourtable) ON on that table to allow values to be inserted into the identity column
copy over this data from the original source
turn off identity insert again: SET IDENTITY_INSERT (yourtable) OFF
Only with this approach will you be able to get the same ID's in an IDENTITY column in your new table.
Okay. I was able to add identity to the existing primary column with 10 million records. Took me about 30 mins.
The steps:
Change database to single user mode (to make sure no other connections to databse, can cause lock)
Open table in designer mode
Make change. Do not save
Click Generate Change Script button (usually on the left right above the Object Explorer)
Copy generated script
Close designer window (you can run only one instance at the time)
Open new window
Execute script
Done. Now your column has identity :)
You can add IDENTITY to an existing column. It just can be done.
in SSMS Just go to tools-->options-->Designers-->Table And Database Designers and uncheck option Prevent Saving changes that require table re-creation.
Now add identity in designer mode to the required column and save.
One way to set an identity column is during an insert with the option set identity_insert. For example, to change id in this table to identity:
create table YourTable (id int, value varchar(50))
You could use this script:
-- Create empty table as a copy of the original
select top 0 * into YourTable_New from YourTable
-- Drop the old ID column
alter table YourTable_New drop column id
-- Add a new ID column with identity
alter table YourTable_New add id int identity
-- Copy the old values into the identity column
set identity_insert YourTable_New on
insert YourTable_New (id, value) select id, value from YourTable
set identity_insert YourTable_New off
-- Drop the old table and rename the new one
drop table YourTable
exec sp_RENAME 'YourTable_New' , 'YourTable'