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
Related
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.
I'm creating a database using SQL for the first time and was wondering if a uniqueidentifier data type is the same as an autonumber in MS Access? If it isn't can anyone show me how to make SQL automatically create an integer value for a primary key?
Uniqueidentifier is a 16 byte GUID. Represented as a 32 character hex string.
Int is an integer.
To create a table with an integer as a primary key in MS SQL like such:
CREATE TABLE TableName(
YourIDColumn INT PRIMARY KEY IDENTITY(1,1))
I have taken the liberty in seeding your identity key starting at 1, and the auto increment by 1. Note that this is also the default in SQL but I have specified it here for clarity sake. The statement will also create a primary key (and by proxy a clustered index) on that column.
Yes it is. In Access, you have to use Autonumber or Long. Not String.
Then, in the lower pane of the design window, specify Field Size: Replication-ID
Now the field will hold a true GUID.
This question already has answers here:
How to to create unique random integer ID for primary key for table?
(8 answers)
Closed 4 years ago.
Right now I have one table of members with table primary key 'id' as auto incremented.
I want that instead of auto increment, it will generate a random integer of 5-6 digits and put that as 'id'. As every 'id' is primary key so generated int will be unique. Any ideas to implement this on sql server will help.
You can use CHECKSUM with NEWID() and use it as a DEFAULT like this.
SELECT ABS(CHECKSUM(NEWID())) % 100000
% 100000 is optional if you want to restrict the number to be max 5 digit.
Note: Having random generated clustered key will cause page splits during inserts.
To avoid guessing the total number of users, just add a random value as initial value when creating the database.
CREATE TABLE users
(
ID int identity (7854, 7),
)
When also specifying an increment value > 1, you loose values of course. Check the value range with the expected number of records.
Another (much better) option would be to hide the primary keys from your users, and if they need to see some identification, they should see a value that is separate from the primary key. Add another value to the table, called "visible ID" or something.
Try using RAND function as below to generate 6 digit random number
select ceiling(RAND()*1000000)
CREATE TABLE NEWID_TEST
(
ID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
TESTCOLUMN CHAR(2000) DEFAULT REPLICATE('X',2000)
)
GO
-- or use
CREATE TABLE NEWSEQUENTIALID_TEST
(
ID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
TESTCOLUMN CHAR(2000) DEFAULT REPLICATE('X',2000)
)
GO
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.
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.