SQL Server Identity (Sequencing issue) - sql-server

Why does SQL Server sometimes not provide sequence no in identity columns? For example we were inserting records into the table; until 300 it was ok, but for the next record the id which was assign it 318 - an 18 numbers gap in identity column.
Can anyone tell me why? And I have seen this for other tables as well one number or two number gap between the before and after.
No records deleted
Thanks

Related

SQL Server : primary key auto increment - what about deleted rows and free key values?

I'm kind of new to SQL and databases and there's one thing that bothers me.
I'm using SQL Server for my ASP.NET MVC project and my database and its tables were auto-generated by Entity Framework using a code-first approach.
I have a table for book collections - just CollectionId and Name columns.
During development I've made many inserts and deletes in this table and right now it has 10 rows with Id's 1 to 10 (the initial entries). But when I add a new one it has the Id set to 37. Obviously in the past there were entries with Id up to 36, but there are now gone and these numbers seem to be free.
Then why a new entry does not have the Id set to 11? Is it a kind of limitation or maybe a security feature?
Thank you for answers.
This is default behavior when we define identity column. Whenever we perform delete operations there will be gaps in records for identity column.
Remarks from MSDN
If an identity column exists for a table with frequent deletions, gaps can occur between identity values. If this is
a concern, do not use the IDENTITY property. However, to ensure that
no gaps have been created or to fill an existing gap, evaluate the
existing identity values before explicitly entering one with SET
IDENTITY_INSERT ON.
IDENTITY
In addition to the other answer, it also has to do with performance of the server. The server typically cache's a group of ID's in memory to make assignment much faster, since the next number has to be stored on disk somewhere. So if the server allocates 100 numbers at a time, it only has to write to disk 1 out of every 100 usages (inserts) of the identity.
Trying to maintain gaps in the sequence would suck up a lot of time.
If you create a new table, insert a single row, kill the server and restart, you'll find the next insert will most likely contain a gap of whatever that number of cached values contains.

SQL Server 2012 Sequence auto-increment on success insert [duplicate]

I have a table and its primary key is an identity column and I was using it as my "folio" to display its value on reports.
If an insert statement fails, SQL Server's default behavior is to auto-increment the seed of my identity column and because of that the user get confused when they have a report with folio 1000 and the next one is folio 1004 (because maybe 3 inserts fails).
So, what is the best way to create a folio column and manage it within a web application?
Is it a good practice that before doing a save I go to my table and get my folio column, auto-increment that on server side and then save it to db for the new record? I'm worried about concurrency because what happens if in the same second 2 users get the same folio number, the issue will be that I will have 2 reports with the same number.
Appreciate any advice.

Avoiding gaps in an identity column

I have a table in MS SQL SERVER 2008 and I have set its primary key to increment automatically but if I delete any row from this table and insert some new rows in the table it starts from the next identity value which created gap in the identity value. My program requires all the identities or keys to be in sequence.
Like:
Assignment Table has total 16 rows with sequence identities(1-16) but if I delete a value at 16th position
Delete From Assignment Where assignment_id=16;
and after this operation when I insert a new row
Insert into Assignment(assignment_title)Values('myassignment');
Rather than assigning 16 as a primary key to this new value it assigns 17.
How can I solve this Problem ?
Renaming or re-numbering primary key values is not a good database management practice. I suggest you keep the primary key as is, and create a separate column index with the values you require to be re-numbered. Then simply create a trigger to run a routine that will re-number every row in the order you expect, obviously by seeking the "gaps" and entering them with values incremented from their previous value.
This is SQL Servers standard behaviour. If you deleted a row with ID=8 in your example, you would still have a gap.
All you could do, is write a function getSmallestDreeID in SQL Server, that you called for every insert and that would get you the smallest not assigned ID. But you would have to take great care of transactions and ACID.
The behavior you desire isn't possible without some post processing logic to renumber the rows.
Consider thus scenario:
Session 1 begins a transaction, inserts a row (id=16), but doesn't commit yet.
Session 2 begins a transaction, inserts a row (id=17) and commits.
Session1 rolls back.
Whether 16 will or will not exist in the table is decided after 17 is committed.
And you can't renumber these in a trigger, you'll get deadlocked.
What you probably need to do is to query the data adding a row number that is a sequential integer.
Gaps in identity values isn't a problem
well, i have recently faced the same problem: i need the ID values in an external C# application in order to retrieve files named exactly as the ID.
==> here is what i did to avoid the identity property, i entered id values manually because it was a small table, but if it is not in your case, use a SEQUENCE SQL Server 2014.
Use the statement UPDATE instead of delete to keep the id values in order.

SQL Server table row

Just a SQL Server 2008 generic question, I have a table that has around 15 columns and they are string, int and bool types only, I am not storing any binary data, and I have auto generated PK column "ID" with ##IDENTITY enabled to generate unique ID on every entry, my question is that a table like this how many rows can have, is there any row limitation in SQL Server table?
thanks in advance.
There are limits defined in "Maximum Capacity Specifications for SQL Server" on MSDN
Some relevant ones
1024 columns per standard table
Rows per table: Limited by available storage
Bytes per row: 8060 (except for row overflow data)
Basically, don't worry...

SqlBulkCopy Max Number of Columns

I have a SQL Server 2008 database with a table that has 575 columns in it. I have a CSV file that matches the table.
When I use SqlBulkCopy (.NET 4), only the first 256 columns get populated. The rest get nulls inserted into them. Has anyone else experienced this issue?
Thanks,
ts3
I'm going to guess that your primary key column is autoincrementing from zero and it is of type tinyint. If so then you should change your primary key on that column to be an integer or something that can hold more values.

Resources