How can i reset my id counter to 1 in phpmyadmin? - database

So, I have a database with 10 users in it in which each one of them has an id from 1 to 10. If I delete those 10 users from the database and then create a new user, the new user will start with the id of 11 and not 1. Any Ideas?

Instead of using the DELETE statment (which will keep your auto incrementing indexes, and start them from the last INSERTED index) use TRUNCATE
TRUNCATE TABLE database_name.table_name;
TRUNCATE REFERENCE
Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.

Related

How to reset auto increment after delete using code first?

I want to reset my auto incremented value after I use delete, If I use post after using delete the value will increment and not fill the previously existing ID, for instance if I delete ID 5 and use post to create then ID 5 will be skipped over and ID 6 will be created. I am using code first approach.
There are 2 approaches - You can re-seed the Identity property after your Delete:
EG
USE AdventureWorks2012;
GO
DBCC CHECKIDENT ('Person.AddressType', RESEED, 10);
GO
OR (if you are deleting all rows)
You Can simply TRUNCATE the table. (assuming there are no FKs)
First option require dbo - 2nd option requires Alter Table

will ms sql server reuse deleted primary keys in auto increment mode?

Suppose i have 100 rows in my table, my primary key is integer which is auto incrementing by 1 from 1. I consolidate my data and clear the table once row id reaches 100, will SQL server reuse the deleted primary keys ?
since primary key is incrementing by 1, if the answer of above question is no, what will happen to the next insert, once the primary key reached the biggest possible number an 'Int' can hold ?
MSSQL will not re-use the primary keys that have been deleted using DELETE (I'm assuming you are talking about the identity incrementation.) If you TRUNCATE the table it will reset the seed and reuse them.
If you go over the max for INT it will indeed just fail to make the next row. You can convert your INT column to BIGINT to avoid that.
BIGINT has a max of: 9,223,372,036,854,775,807 and INT has a cap of 2,147,483,647 but note.. you can also use those negative values too!
You can read about those caps here: https://learn.microsoft.com/fr-fr/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017
No, SQL Server won't reuse IDs. If you have 100 records with IDs 1 throught 100 and delete them all, next inserted row will have ID = 101.
But if you want to start from 1 after deleting, you can use this command:
DBCC CHECKIDENT('TableName', RESEED, 0)
It will reseed your identity and make it start from 1 all over again.
It depends on what you mean by
I...clear the table once row id reaches 100
If you're issuing a DELETE statement, then no, the identity column will issue 101 on the next insert.
If you're issuing a TRUNCATE TABLE statement, then yes, the table will reseed to 1 (or whatever your seed value is) on the next insert.

Identity column not incremented by 1?

I've created 4 tables:
`Patient` (Id, Name, ..)
`Donor` (Id, Name, ..)
`BloodBank` (Id, Name, ..)
BloodBankDonors(DonorId, BloodBankId, ..)
And set the Id columns to Identity incremented by 1, seed 1. and made a relationship between (Donor, BloodBank) and (BloodBankDonors).
The problem is when I entered some data in the tables BloodBank and the patient, the auto generated Id column was: 1,3,4 and 1,4,5,8 respectively?!
So many things can cause gaps in an IDENTITY column. For example rollbacks not resetting IDENTITY, deletes, etc.
So, why do you care about gaps? You shouldn't. If you need a contiguous sequence of numbers, stop using IDENTITY.
You might deleting (DELETE command) some records from tables "BloodBank" and the "patient".Deleting record from table holds the log info of column ID(auto generated column) for recovery Purpose. Instead use below mentioned code snippet after "DELETE" command:
DBCC CHECKIDENT('databasename.dbo.tablename', RESEED, number)
if number=0 then in the next insert the auto increment field will contain value 1
if number=101 then in the next insert the auto increment field will contain value 102.
for more clear answer, please share sql script which you are using to create tables and insert records.
Deleting data from table holds the log info holding ID (auto generated columns) for recovery purpose.
Try to truncate table and re-enter the data
truncate table Patient
May this help

SQL Server TRUNCATE

I have a question to the TRUNCATE command.
How does TRUNCATE work in background ?
I read a text that says TRUNCATE creates a copy from the table and then start to delete all rows of the table. Is it correct?
with the information from here
TRUNCATE logs the deallocation of the data pages in which the data
exists which means that your data rows still actually exist in the data pages, but the extents have been marked as empty for reuse. This is what
makes TRUNCATE a faster operation to perform over DELETE.
You cannot TRUNCATE a table that has any foreign key
constraints. You will have to remove the contraints, TRUNCATE the
table, and reapply the contraints.
TRUNCATE will reset any identity columns to the default seed
value. This means if you have a table with an identity column and
you have 264 rows with a seed value of 1, your last record will have
the value 264 (assuming you started with value 1) in its identity
columns. After TRUNCATEing your table, when you insert a new
record into the empty table, the identity column will have a value of
1

Trying to reset Identity value for SQL Azure DB Table column

I am using SQL Azure. I have a deployment DB and a test DB
I would like to add some new lookup records to the Test DB, to test new code.
Initially My Deployment DB's Identity was set to 200000+ for the PKs, and my Test DB to 100000+, to prevent PK collisions when syncing using such tools as Redgates' SQL data compare.
Unfortunately I made a mistake, and copied the Deployment DB as the new Test DB, since we required a more up to date dataset. As a result my Test DB now starts at 200000+. So I now have the risk of conflicts when syncing with the Deployment DB.
I would normallly just use:
DBCC CHECKIDENT('TableName', RESEED, 105000)
However SQL Azure does not support this.
I have come across a workaround:
set identity_insert TableName on -- this basically turns off IDENTITY
INSERT INTO TableName(id, name) VALUES (104999,'Test Reset Identity Start 104999') -- so we can jam any value for column ID
set identity_insert TableName off -- then turn it back on
INSERT INTO TableName(name) VALUES ('Test Reset Identity End') -- ID starts at 105000, in theory, from this point
SELECT * FROM TableName
However the new Identify value always seems to take the highest PK in the table as the last seed, rather than the PK value from the last inserted record.
I would rather not rebuild the table.
Is there another approach to resetting the identity value for my situation, ie to a lesser number ie 104999 rather the current 204999?
Thanks in advance.
EDIT 1
It may be I can only check the identity value to a value greater than the current value ie to say 206000 ?
EDIT 2
Perhaps there is an argument that Identity value should never be reseed to less than the max PK value, even if there is 100,000 spare numbers, as one day you will get a collision.

Resources