identity id column in sql table doesn't start at 1 - sql

I ran a delete script to delete all rows that had an id greater than 0 to clear the table and now when I try to add rows to the table, the id starts off where it left off from before. How can I delete the info in the table so the id starts off at 1 again?

Delete statement doesnt reset the identity value. Use Truncate table command if you want identity value to be reset. something like this..
TRUNCATE TABLE Table_Name
This will empty the table and reset the identity value.
Only use TRUNCATE when you want all the rows to be deleted. As it does not allow you to use WHERE clause.

I don't think that is possible in the same table. Which database are you using? In previous versions of MS Access compacting the database used to work but not any more. I don't think SQL Server allows that either. The only way is to copy the records from this table to a new table, delete the old table and rename the new table to the old table name.

In MS-SQL you can have a play with DBCC CHECKIDENT (yourtable, reseed, 0)

Related

how to Reset AutoIncrement in SQL Server after all data Deleted

i got a function in sql that generate sequential series of alphanumeric
no.like (c000,c0001 .......) , which is working good . but when i deleted all data in table , it starts from last generated no. i want it to reset its value from "c0000" .
code is as follows :-
create table Customers
(
dbID int identity not null primary key,
CustomerName varchar(100)
)
create function CustomerNumber (#id int)
returns char(5)
as
begin
return 'C' + right('0000' + convert(varchar(10), #id), 4)
end
alter table Customers add CustomerNumber as dbo.CustomerNumber(dbID)
thanks in advance....
EDIT 1 -
how to update it to increment based on last value . means if last entry having no. c0053 , and i deleted this record , so when next entry added it should have value "C0053" not "C0054".
thanks
Truncate Table Command is good way to reset Identity, but there is other command also to reset Identity after deletion of records.
DBCC CHECKIDENT (TableName, RESEED, 0)
After Deleting you can use this command to reset Identity to 0.
TRUNCATE TABLE
Removes all rows from a table or specified partitions of a table, without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.
TRUNCATE TABLE Customers
or remove your in build function.
My suggestion is instead of deleting all rows of data why dont you truncate the table.
If you are truncate the table it automatically reset your auto increment to 0
TRUNCATE TABLE your_table_name;
This example would truncate the table and remove all records from that table. and rest your auto increment too.
The SQL TRUNCATE TABLE command is used to delete complete data from an existing table
Try this way. May help you.

Can Identity Column help perform update statement

I need to do updates over a table that contains over 600M lines on sql server, and this table does not contain the identity column, this is how it works by far :
UPDATE myTable set myBitColmun=1 where key='specifickey'
This kind of query takes over 1minute to update this specific line. And imagine the time needed to do updates on the whole table. And I need to do this kind of updates on my table everytime possible.
My question is : can identity column help perform update statement ?
alter table myTable
add id int identity(1,1)
and change my previous query:
UPDATE myTable set myBitColmun=1 where id=specificId

Teradata: How to back up a table that uses an identity column?

In Teradata, the way I've been doing backups for tables is like this:
create table xxx_bak as xxx with data
Works great, but I have just discovered that this doesn't work for tables with identity columns.
I need a backup method that can duplicate a table with its data intact so that I can roll it back in case I mess up some data.
After over a year and a half, I've finally found a slick solution to this issue:
create table mydb.mytablebackup as
(select * from (select * from mydb.mytable) x)
with data;
Be sure to qualify the innermost subquery or it won't work.
If you just want a copy of the table, you can create one with the same structure but without making the key column an identity column. You can then insert into it from the original table. However, you wouuldn't be able to insert back into the old table from the backup while retaining the same keys.
The way to make a backup that you can later restore with the same keys is to use the archive/restore tool ARCMAIN.
Backup like this:
logon my_server/my_user, my_password;
archive data tables (my_database.my_table), release lock, file=backup_file;
Restore like this:
logon my_server/my_user, my_password;
restore data tables (my_database.my_table), release lock, file=backup_file;
This involves 3 steps:
1. SHOW TABLE orig_Table; (*Get the DDL*)
2. Replace orig_Table with bkp_Table name
3. INSERT INTO bkp_Table SELECT * FROM orig_Table;

How to reseed an an auto increment column in a SQLite database?

Is it possible to reseed an auto increment column in a SQLite database, and if so, how is this done?
ie. The equivalent of DBCC CHECKIDENT ('MyTable', RESEED, 1) in SQL Server.
In SQLite there is a table named SQLITE_SEQUENCE, which tracks the largest RowId value that a table has. You can do insert, updates and deletes on this table. For example, to mimic similar functionality as the TRUNCATE TABLE statement SQL Server you could something like:
DELETE FROM MyTableName;
DELETE FROM SQLITE_SEQUENCE WHERE NAME = 'MyTableName';
In the above example all data from MyTableName is removed, and the auto increment rowid is reset by removing the value from the SQLITE_SEQUENCE table. See the documentation for AUTOINCREMENT for more information.
DELETE
FROM MyTableName
select *
from SQLITE_SEQUENCE
update SQLITE_SEQUENCE
set seq = 0
where name ='MyTableName'
Alternatively you could export a SQL file from the SQLite database. Then edit the generated SQL file and update the appropriate entries to the desired IDs or delete the INSERT statements. After this create a new empty database and seed it with the adjusted SQL file. The highest ID + 1 will then be the ID for a new entry...
With this way you can decide, which entries to keep and which entries should be removed as a non-destructive or even a more flexible approach.
For me it worked - clean and easy. ;)

Reset auto-incrementing column

I recently added Items to an ID and the the table got messed up in the transfer process so I deleted the Items from the table. Upon reentering the data instead of the ID starting at one it now starts at 332. I would like to have the table start at one instead of 332. I've removed the data from the data so it's clear. How do I reset the ID to one.
Thanks and sorry if this on here somewhere I wasn't sure how to search for this.
In SQL Server:
DBCC CHECKIDENT (myTable, RESEED, 0)
truncate table yourtable --will reseed
Assuming MSSQL:
DBCC CHECKIDENT('MyTable', RESEED, 0) -- One less than next ID to allocate
If you want to remove the data too you can use
TRUNCATE TABLE MyTable
but you cannot use TRUNCATE TABLE on a table referenced by a Foreign Key, or if the table is part of an indexed view, and unlike DELETE MyTable any trigger(s) on the table won't be activated.
Set the starting identity value to 1
DBCC CHECKIDENT (tableName, RESEED, 1)
If you are using MS Access, delete and recreate the table
you need to truncate the table
but to do so it has be empty, and no foreign keys attached to it at all