Alter DB2 identity to add Cycle Cache and a specific restart value - sql

I want to alter a db2 identity to cycle cache and to make it (re)start with a specific value, something like:
ALTER TABLE ALTER COLUMN
SET GENERATED ALWAYS AS IDENTITY (CYCLE CACHE 100) RESTART WITH 32323
Is there a way of doing this so that I can add cycle cache as well as make it restart from a specific number?

What error are you seeing? What version and platform of DB2?
The following works on DB2 for IBM i 7.1
alter table mytable
alter column mycolumn
set generated always
set cycle
set cache 100
restart with 32323
This worked also
alter table mytable
alter column mycolumn
set generated always as identity
set cycle
set cache 100
restart with 32323

Related

Phoenix alter table add column fails

I am trying to add a column to an existing phoenix table using alter table command as below
ALTER TABLE TABLE1 ADD "db_name" VARCHAR(20);
Its failing with below warning
WARN query.ConnectionQueryServicesImpl: Unable to update meta data repo within 1 seconds for TABLE1
Let me know, If there is any timeout I need to increase to get this working.
When altering a table, Phoenix will by default check with the server to ensure it has the most up to date table metadata and statistics. This RPC may not be necessary when you know in advance that the structure of a table may never change. The UPDATE_CACHE_FREQUENCY property was added in Phoenix 4.7 to allow the user to declare how often the server will be checked for meta data updates. You can set this property on your table like below
ALTER TABLE TABLE1 SET UPDATE_CACHE_FREQUENCY=900000
Please refer this doc for tuning tips.

AS400 DB2 Duplicate Key Error during Insert in Table with PK Identity Column

I got a Table with an Auto Increment Column which looks like:
ALTER TABLE SOMESCHEMA.SOMETABLE
ALTER COLUMN ID
SET DATA TYPE INTEGER GENERATED BY DEFAULT
SET INCREMENT BY 1
SET NO ORDER
SET NO CYCLE
SET MINVALUE 1
SET MAXVALUE 2147483647
SET NO CACHE;
As long as i let the DBMS generate the Ids everything works fine and I can get the generated Id via:
SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1
But sometimes i need to insert a row with an ID of my choice and there i get into trouble.
Lets say we got a single row in the table with ID 1.Now i insert a new row with a manually assigned id of 2. The next time i try to insert a new row without a preset ID i get an error SQL0803 "DUPLICATE KEY".
I assume the internal "NextId" field for that Auto-Increment Column doesnt update itself if the Id of a row is manually set.
So I tried reseting this field with:
ALTER TABLE SOMESCHEMA.SOMETABLE ALTER COLUMN ID RESTART WITH 3
But this causes a permanent Table lock, which i dont know how to unlock.
How can i get this "Mixed-Mode" ID-Column working? Is it possible to get it to work like MySQL where the DBMS automatically updates the "NextID" upon a manually-Id Insert? If not, how can I release that {insert swear-word here} lock that pops up if i try to reset the NextId?
SQL0913 isn't creating a lock - it is reporting that a lock exists. ALTER TABLE needs an exclusive lock on the table in order to reset the ID number. A table can be locked by another process having it open, or it can be locked by this process if there are uncommitted rows.
There is another reason the table is in use - soft close (or pseudo-close). For performance reasons, DB2 for i keeps cursors in memory so that they can be reused as efficiently as possible. So even if you say CLOSE CURSOR, DB2 keeps it in memory. These soft closed cursors can be closed by the command ALCOBJ OBJ((SOMSCHEMA/SOMETABLE *FILE *EXCL)) WAIT(1) CONFLICT(*RQSRLS) The CONFLICT(*RQSRLS) parameter tells DB2 to close all soft closed cursors.
So the root of the issue is that DB2 wants exclusive access to the table. Which is sort of a design question, because typically one doesn't manipulate the table's structure during the work day. It sounds as though this table is sometimes a parent and sometimes a child when it comes to ID numbers. If that is the case, may I suggest that you ALTER the table again?
I think the implementation might be better if you used a trigger rather than auto-increment. Fire the trigger on INSERT. If ID is supplied, do nothing. If ID is not supplied, SELECT MAX(ID)+1 and use that as the actual ID number you commit to the database.
ALTER TABLE table_name ALTER COLUMN column_name RESTART WITH 99999;
Fixed my issue. "99999" is the next ID to be used for example

Altering data types within a table using SQL command ALTER

I have researched how to alter table data types and I understand how to do it but I cannot get it to work. I am trying to update my table Person within APP using the following command:
ALTER TABLE APP.PERSON ALTER uName varchar;
What I have tried so far:
using Modify but realised that, after I received errors, this should indeed be ALTER.
changing uName to UNAME.
changing it to a data type of varchar2.
adding in the size of the data type '(20)' and 'NOT NULL' at the end.
Where am I going wrong? I am using Netbeans 7.3 Beta 2 running on Mac OS X, this is all being done within the SQL Commands section of Netbeans, using Java-DB as my database if any of that matters.
It has already been answered here on SO. You basically need to create new column with desired datatype and delete the old one. However, if you take a look into Apache Derby doc, there is a SET DATA TYPE command, so try something like
ALTER TABLE APP.PERSON ALTER UNAME SET DATA TYPE VARCHAR(30)
EDIT
If code above doesn't work, then you just have to recreate the column as I suggested before.
ALTER TABLE APP.PERSON ADD COLUMN UNAME_NEW VARCHAR(30);
UPDATE APP.PERSON SET UNAME_NEW = UNAME;
ALTER TABLE APP.PERSON DROP COLUMN UNAME;
RENAME COLUMN APP.PERSON.UNAME_NEW TO UNAME;
Most databases require specific permissions to use DDL (Data Definition Language) commands like ALTER TABLE. Very often, the DB credentials used in an application tier do not have DDL permissions in the database.
Verify that the connection you are using has permission to run ALTER TABLE. If indeed it does, post the specific code you are using and any specific error messages.
I don't have JavaDB to test on, but according to the documentation it should be;
ALTER TABLE APP.PERSON ALTER uName SET DATA TYPE VARCHAR(20)

How do you UPDATE an identity column in SQL azure? Is it possible?

I have this code:
ALTER TABLE FishSticks
ADD MyNewIdentityColumnId INT IDENTITY(1,1) NOT NULL
SET IDENTITY_INSERT FishSticks ON
UPDATE FishSticks
SET MyNewIdentityColumnId = MyOldColumnId
However it says:
Cannot update identity column 'MyNewIdentityColumnId'.
I assume this is due to SQL Azure, but I didn't have much success googling. It seems that changing a column to be an identity while preserving data is a huge difficulty.
SET IDENTITY_INSERT only works for INSERTS and that error is not specific to SQL Azure. You'll get the same error on SQL Server/Express
to go around the issue, SET IDENTITY_INSERT to ON, reinsert all the column values from the existing row whose identity value you want to replace with a new value, SET IDENTITY_INSERT OFF, then delete the previous row.
I found this Possible solution from Herve Roggero who basically gives a way of turning off the ID and then adding the rows, then turning ID back on.

How do I remove an Identity completely

I'm currently putting together some changes in our data model which include changing a column (that happens to be part of the primary key) so that it is no longer an identity. Is there a way to do this short of actually removing and recreating the entire column or table? The autogenerated code from SSMS does just that but I was wondering if there was perhaps a simpler solution.
You cannot remove the Identity property of a column without droping it.
Possible solution steps are:
(a) Add a new column
(b) Update the column with identity column value
(c) Remove the identity column.
Alter Table Tablename Add newColumnname int
Update Table set newColumnname =IdentityColumn
Alter Table TableName Drop IdentityColumnName
Note : If you are talking about switching off the identity property for the time being, check Set IDENTITY_INSERT TableName ON Command
Make sense?
Cheers, John