Identity Columns - sql-server-2005

I've an identity column which has multiple deletes now it is not working because it reaches the max limit of the INT data type,
how can I insert records in the place of deleted ones without truncating all the data?

You cannot "recycle" unused IDENTITY values - if you've reached the end of the INT data range, you need to change your ID column to BIGINT.
ALTER TABLE dbo.YourTable
ALTER COLUMN YourIDColumn BIGINT
The IDENTITY property will be preserved - now you have a lot of additional ID values available for the next couple months/years to come!

Excuse the obvious...
Did you start at 1, increment of 1? If so, change the identity to -1, -1. It requires a table rebuild but it's easier than changing to bigint. And gives you another 2 billion IDs.
Then plan your bigint migration...

That seems like a lot of work; why not just change the primary key on the table (and any related tables) from an int to a bigint?
An int will give you a max value of 2,147,483,647.
A bigint will give you a max value of 9,223,372,036,854,775,807.

Related

Update Primary Key with NVARCHAR Sequence

I have a Microsoft SQL Server 2012 database with a table called Lineitem. This table has a primary key field Code and currently has more than 1,000,000 rows.
I need to update the primary key with a NVARCHAR that starts from Li000000001 and that increments like this Li000000002, Li000000003 etc.
How can I do this technically please?
The best solution is to use:
an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value (-> this is probably your existing Code column)
a computed, persisted column to convert that numeric value to the value you need
So try this:
ALTER TABLE dbo.LineItem
ADD AlphaCode AS N'Li' + RIGHT('000000000' + CAST(ID AS NVARCHAR(9)), 9) PERSISTED
Now, every time you insert a row into LineItem, SQL Server will automatically increase your Code value (as it has so far), and AlphaCode will contain values like Li000000001, Li000000002,...... and so on - automatically, safely, reliably, no duplicates.

Function in SQL that will give me a unique int?

I am trying to enter a row into one of my databases, but I want to make sure the primary key I give it is unique. My primary key is an int. I have tried using newid(), but that does not give me an int back. If there is a way I can get newid() to return just an int, or if another function that would give me an unique int I would use it.
You could alter the definition of your column to int IDENTITY(1,1) PRIMARY KEY, then don't specify a value when inserting a row, and the ID will auto-increment.
See https://www.w3schools.com/sql/sql_autoincrement.asp for more info.
Be careful with a random primary key, especially if it's the clustered index too. It's likely to cause allot of filesystem IO.
Use an auto incrementing identity instead.
One object that gives unique ints are the sequences.
You first need to create a sequence :
CREATE SEQUENCE MyNewID START WITH 1 INCREMENT BY 1;
And then you can retrieve your new ID calling NEXT VALUE every time :
SET #MyNewID = NEXT VALUE FOR MyNewID;

If I use SQLite auto increment on a column, does it automatically maintain an index on that column?

I am using auto increment on an integer data column in SQLite. Since it is auto increment, the data is already sorted by that column in ascending order. So, I was wondering if SQLite will perform a binary search over the auto increment column whenever a data is searched by that column.
Effectively yes, but not really.
That is, all AUTOINCREMENT does is add a constraint that requires the value assigned to the column to be higher than any existing value, or higher than any value that has been used, in that column.
But there's more to it than that it is your_column INTEGER PRIMARY KEY (AUTOINCREMENT can only be used on such a column and there can only be 1 such column per table) makes that column an alias of the hidden rowid column.
The rowid is what is indexed, and is basically the most primary index and the most efficient, which always exists unless the table is defined using the WITHOUT ROWID keyword.
So an AUTOINCREMENT column is an alias of the rowid column and uses a differnt, more expensive algorithm than an alias of the rowid without AUTOINCREMENT.
That is without AUTOINCREMENT the value generated for the rowid column will find the maximum value in the table and increment it. Unless that value exceeds 9223372036854775807 in which case SQlite will make some attempts to find an unused lower value (normally between 1 and 9223372036854775807).
With AUTOINCREMENT the algorithm takes the higher value of the maximum value and a value stored in the table sqlite_sequence for the respective table and uses that (thus any deleted higher valueswill not be re-used). However if 9223372036854775807 has been used then an SQLITE_FULL error will be raised.
The following should be noted :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
SQLite Autoincrement you may well want to read this.
Additional
regrading the comment :-
If I don't use AUTOINCREMENT I have to explicitly create unique
integer IDs and then insert them in database each time a new row is
inserted.
The following demonstrates that there is no requirement for AUTOINCREMENT:-
CREATE TABLE IF NOT EXISTS xyz (ID INTEGER PRIMARY KEY);
INSERT INTO xyz VALUES(null);
SELECT * FROM xyz;
After running twice the result is :-

does db2 have identity insert on?

I have table with identity, seed 1 auto increment 1. In that table I have rows with primary key 1,2,4,5 (3 is missing, I deleted it), now I want to insert values in that table but with ID of 3, but I can't find it how in db2...
Any help? Thanks in advance.
The answer depends on how the column is defined. If it is GENERATED BY DEFAULT AS IDENTITY, then you can simply provide an explicit value for it in the INSERT statement. If the column is GENERATED ALWAYS, you could temporarily restart the identity sequence from the value you need, perform the insert, then restart it again with the maximum value + 1. The latter will only work if there is no concurrent insert activity on the table, of course.
Having said all that, I think that if you really require a gapless identity sequence you should not be using autogeneration in the first place.

SQL Identify Auto Increment from a certain number

I am trying to auto increment my primary key, by 0.1 each time. Starting from 0.1. Is this possible?
CREATE TABLE NewTable
(
ID BigInt IDENTITY NOT NULL,
CONSTRAINT PK_ID PRIMARY KEY (ID),
)
No. BigInt is a 8 byte integer value.
Note: Assuming as Microsoft SQL Server.
No, even if you changed the data type of the field, the identity increment 'must be a non-zero integral number containing 18 digits or less'
(Presuming this is sql server)
In SQL Server, the Identity column cannot contain decimal values in its seed or increment.
Why would you need to do this -- for presentation purposes? If so, don't. If needed, one option is to create a trigger. You could also consider using a Computed column instead -- make it 1/10 the value of the Id (your Identity field seeded and incremented at 1 -- Identity(1,1)).
Again, not sure why you'd need to do this though.
Some solution would be a view:
CREATE VIEW v_NewTable AS
SELECT ID/10
FROM NewTable