I have an HSQLDB database with a generated ID and I want the auto-incrementing values to always be above 100,000. Is this possible with HSQLDB? Is this possible with any database?
According to the HSQL Documentation:
Since 1.7.2, the SQL standard syntax
is used by default, which allows the
initial value to be specified. The
supported form is( INTEGER
GENERATED BY DEFAULT AS IDENTITY(START
WITH n, [INCREMENT BY m])PRIMARY KEY,
...). Support has also been added for
BIGINT identity columns. As a result,
an IDENTITY column is simply an
INTEGER or BIGINT column with its
default value generated by a sequence
generator.
...
The next IDENTITY value to be used can
be set with the
ALTER TABLE <table name> ALTER COLUMN <column name> RESTART WITH <new value>;
Here's how to do it in HSQLDB:
The next IDENTITY value to be used can be changed with the following statement. Note that this statement is not used in normal operation and is only for special purposes, for example resetting the identity generator:
ALTER TABLE ALTER COLUMN <column name> RESTART WITH <new value>;
As far as I know, all SQL databases allow you to set a seed value for the auto-increment fields.
Update: Here's a list of identity/auto-increment implementations in the major SQL databases.
It is possible with SQL Server. When defining an auto number column you can define the starting number and the increment:
IDENTITY(100000, 1)
I know it's possible with SQL Server, and I imagine it's possible with others.
With SQL Server you can set the ID column seed (starting number) and increment value.
You can do it with databases that use sequences, like Oracle and PostgreSQL. You specify a start value when you create the sequence.
This suggests that you can do it with HSQL as well.
Not sure about HSQL, but in MS SQL yes it's possible. Set the ID to auto increment and set the seed value to 100,000.
Related
I'm porting a SQL Server based app to Oracle. Our Oracle DBA has given me a schema that was supposed to be identical to the original SQL Server schema (and generated from it), but the auto generated keys are missing. I am trying to alter these table PK's from a normal INT to incrementing. I am doing so with Oracle SQL Developer 4.0.3 and Oracle 12c.
The error I receive is ORA-01442: column to be modified to NOT NULL is already NOT NULL
I get this after editing the table, selecting the column and setting it's Identity dropdown to 'Generated as Identity'. I am not sure why SQl Developer is attempting to make it not null when it's already a PK.
My questions are: Is this the proper way to setup a generated key? How can I get around this? If I go alter all the required columns, can the DBA use the schema to regenerate whatever procedure he used to create it in the first place to allow proper generated keys and is there a better solution for creating a good schema to go forward with?
Thanks.
If the column is already definied as NOT NULL there is no need to re-defined it as NOT NULL. Therefore you get the error ora-01442.
The best way to obtain sequence values, such as identity in SQL Server, is define the column with default sequence, before inserting row:
CREATE SEQUENCE SEQ_NAME
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
ALTER TABLE table_name MODIFY column_name INT DEFAULT SEQ_NAME.NEXTVAL;
PD: This DEFAULT works with 12 c. To 11g or less, you must create a trigger
I inherited a SQL Server database and for some reason the primary key is not being incremented properly.
For example, the PK for the last record in the table is 120350
Then, when I enter a new record into the table, it gets the PK 48377
What would be the best way to reorder or set the initial PK for new records? This is for Microsoft SQL Server 2008 R2.
DBCC CHECKIDENT('<tablename>');
This will reseed the identity column to where it should be (in this case 120351). If you want to reorder the existing value then you'll need to use SET IDENTITY_INSERT '<tablename>' ON as part of a script to move the rows around. How you do that depends on what exactly you're trying to accomplish. Keep in mind that you'll also need to keep any foreign keys in sync.
If you have some other requirements on the column that defines, "incremented properly" then you might need something besides an IDENTITY column.
When I use
DBCC CHECKIDENT('UPS_Worldship_Export');
I got this result: Checking identity information: current identity value '48377', current column value '120351'.
Somehow, the value was offtrack. Thanks to the comments above, I just reseeded the value to the highest value and it works correctly now.
DBCC CHECKIDENT (',<table name>', RESEED, <current column value>);
I am using Sql server 2012(Amazon RDS). I have a table which has an identity column in it.At the beginning Identity column starts from 1,2 and so on and adding identity smoothly, but suddenly it jumps from 17018 to 27011. What could be the reason. Please assist.
thanks,
Sella
Restarting server instance may cause this.
See this
Any of these things can cause a jump in the identity column:
An insert into the table that is later rolled back
An error when inserting into the table (like unique constraint violation)
A delete from the table
Someone use IDENTITY INSERT ON to set a value for the identity column. If that value is bigger than the current value, the sequence will resume at that.
A server restart
In general, you shouldn't expect identity columns to increment by 1. Treat the value as random. The only difference between identity and a true random is that it's guaranteed to be increasing in value.
I am inserting data into a table with an identity column called unique_id. I am leaving the unique_id column out of the insert, assuming that the value will then be seeded from the identity configuration on the column.
Insert statement:
INSERT INTO table_name (column_1, column_2, column_3, processed_dt)
VALUES ('A', 'B', 'C', GETDATE());
Error:
Cannot insert the value NULL into column 'unique_id', table 'db.dbo.table_name'; column does not allow nulls. INSERT fails.
When I double click on the unique_id column in SQL Server Management Studio, I see that the following values are set:
Identity: true
Identity Seed: 1
Identity Increment: 1
What other configuration may be wrong to cause the identity seed to not be used automatically?
UPDATE:
Based on some of the recommendations I am seeing, I want to add that this schema was converted by our vendor from Oracle into SQL Server. I'm guessing that part of that process must of included converting Oracle Sequences into SQL Server Identity columns. Something being "broken" with the identity column is certainly a possibility.
I'm definitely on the correct database, table, and column. The Identity on this column was not created today, it was created weeks ago.
Is there any configuration the vendor could have put in place that would disable the auto assignment of the seed value and force the developer to "fetch" the next seed value manually?
SQL Server stores the seed/nextID value to be used. I'm wondering if the conversion from Oracle neglected to set that value. Try using the the following command on that table to check what it's seed value is:
DBCC CHECKIDENT ( table_name, NORESEED )
Maybe it actually is null, which is causing the error. Then you can use a variation of the same command to 'reseed' or set the next value to be used.
For more information and options:
http://msdn.microsoft.com/en-us/library/ms176057(v=sql.110).aspx
This is a pretty old question, but I was lead here when one of my developers had this very issue and wanted to share what caused/fixed it for us.
Basically, on another tab in SQL server "Edit Top 200 Rows" was open for the specified table.
Once this tab was closed, the insert worked without issue.
Hope this helps someone!
When I add a new record I want SQL Server to automatically add a fresh ID.
There are already some records which have been migrated over (from Access) and until I finish preparing the server for other required functionality I will be manually migrating further records over (if this affects any possible answers).
What are the simplest ways to implement this.
The simplest way would be to make the column an IDENTITY column. Here is an example of how to do this (it's not as simple as ALTER TABLE).
Make use of the Identity field type. This will automatically create a value for you using the next available number in the sequence.
Here is an example of how to create an Identity column (add a new column) on an existing table
ALTER TABLE MyTable ADD IdColumn INT IDENTITY(1,1)