DB2 create table fail - database

I create a table with Liqibase on our test Databse we have no Problem to create following table:
CREATE TABLE ICEM_DEP.T_APP_UNIT_ENV_INST_OBJ (
ENVIRONMENT_ID INTEGER NOT NULL,
INSTANCE_ID INTEGER NOT NULL,
OBJECT_NAME VARCHAR(4000) NOT NULL,
APP_UNIT_ID INTEGER NOT NULL,
CREATION_DATE TIMESTAMP DEFAULT current timestamp(0),
LAST_CHANGE_DATE TIMESTAMP DEFAULT current timestamp(0),
CREATION_USER INTEGER NOT NULL,
LAST_CHANGE_USER INTEGER NOT NULL,
ACTION_FLAG VARCHAR(1)
)
If I run this statement on the costumer database the is following Error:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=(;LT current
timestamp;DEFAULT, DRIVER=4.13.127
Any Suggestion?

did you try with
CREATION_DATE TIMESTAMP WITH DEFAULT CURRENT TIMESTAMP,
LAST_CHANGE_DATE TIMESTAMP WITH DEFAULT CURRENT TIMESTAMP,

The format for passing the lowest possible value to the Timestamp Function is
Either
TIMESTAMP('0001-01-01',0)
or
TIMESTAMP('0001-01-01-00.00.00.00000')

Related

Create a table in Jupyter Notebook that connects to a database in Beekeeper and AWS

I am trying to generate some tables in a database that I have already created in AWS and connected through Beekeeper. I am using Jupyter notebook with Python 3 to accomplish this.
However, when I run the cell for my first table (which is after my sell where I connect to the database using psycopg2) I get a Syntax Error. Here is the code I have been trying:
CREATE TABLE table(
tabID SERIAL PRIMARY KEY NOT NULL,
tabName VARCHAR(30) NOT NULL,
Breed VARCHAR(5) NOT NULL,
Gender VARCHAR(2) NOT NULL,
Weight SMALLINT NOT NULL,
Age NUMERIC(2,1) NOT NULL
);
conn.commit()
The error is showing up with the ^ (carat) under the T in first table. I've tried moving around commas and parenthesis.
File "C:\Users\AppData\Local\Temp\ipykernel_10756\4115600721.py", line 1
CREATE TABLE table(
^
SyntaxError: invalid syntax
I was able to figure it out, and I'm posting the answer for anyone who might run into something similar.
table = ("""CREATE TABLE table (
tabID SERIAL PRIMARY KEY NOT NULL,
tabName VARCHAR(30) NOT NULL,
Breed VARCHAR(5) NOT NULL,
Gender VARCHAR(2) NOT NULL,
Weight SMALLINT NOT NULL,
Age NUMERIC(2,1) NOT NULL);
""")
cursor.execute(table)
conn.commit()

Error 9628 : Type (timestamp) not found in informix while creating new table

I'm trying to create a new database in informix using following query.
CREATE TABLE IDN_AUTH_SESSION_STORE (
SESSION_ID LVARCHAR (100) DEFAULT NULL,
SESSION_TYPE LVARCHAR(100) DEFAULT NULL,
SESSION_OBJECT BLOB,
TIME_CREATED TIMESTAMP,
PRIMARY KEY (SESSION_ID, SESSION_TYPE)
);
When I ran this query, I am getting following error.
9628: Type (timestamp) not found.
What is the reason for this? How can I fix this?
AFAIK, timestamp doesn't exist in Informix. Use datatype 'datetime year to fraction' instead.

How can I add a timestamp column to my SQL Server table when I create it?

I am trying to use the following:
CREATE TABLE [dbo].[Application] (
[ApplicationId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
timestamp
CONSTRAINT [PK_dbo.Application] PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
);
Can someone confirm if this is the correct way. Also can or should I give that column a name of its own?
* Note that I am using Entity Framework. So is it okay to add a column like this but to not add it to the Application object?
I think that timestamp is a poor name for that datatype (it does not store time) and somewhere along the way Microsoft did too and has deprecated the use of timestamp since SQL Server 2008 in favor of rowversion introduced in SQL Server 2000.
Your code uses a behavior of timestamp that it gives the column a default name, rowversion does not do that so you have to give the column a name.
CREATE TABLE [dbo].[Application] (
[ApplicationId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
VerCol rowversion
CONSTRAINT [PK_dbo.Application] PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
);
Ref:
rowversion (Transact-SQL)
timestamp SQL Server 2000
* Note that I know nothing about using Entity Framework.
CREATE TABLE [dbo].[Application] (
[ApplicationId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
timestamp DATETIME NULL DEFAULT GETDATE()
CONSTRAINT [PK_dbo.Application] PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
);
To add the timestamp / rowversion to an existing table you can do this.
ALTER Table OrderAction ADD [RowVersion] rowversion not null
It will automatically assign timestamps, you don't need to anything like UPDATE rowversion = getdate()
Please note that if your table is large it can take a while since it needs to add a timestamp for every row. If you have a huge table and you're using a scalable database like Azure SQL you might want to increase capacity first and/or do it during off hours.
timestamp data type is identical to rowversion datatype - it's just up to you what you call the column.
It also doesn't need to be in your data model to be updated by an UPDATE or INSERT. However if it isn't in your data model then you won't actually benefit from the whole point of it which is to get a simplified UPDATE like this:
WHERE ([OrderId] = #p0) AND ([RowVersion] = #p1)

Multiple timestamp columns in SQL Server 2000

I need to create a table in SQL Server 2000.
create table TABLE (
DBID_ bigint not null,
CLASS_ varchar(255) not null,
DBVERSION_ integer not null,
HPROCI_ bigint,
TYPE_ varchar(255),
EXECUTION_ varchar(255),
ACTIVITY_NAME_ varchar(255),
START_ timestamp,
END_ timestamp,
DURATION_ bigint,
TRANSITION_ varchar(255),
NEXTIDX_ integer,
HTASK_ bigint,
primary key (DBID_)
);
An error occurs when I run it.
A table can only have one timestamp column. Because table TABLE
already has one, the column END_ cannot be added.
What is the best alternative for timestamp for SQL Server? How to fix this issue?
A timestamp is not a datetime datatype as the name suggests. It is an internal value that is relative to the server's clock, but an actual time cannot be derived from it's value. It is simply used to evaluate whether a row has been updated, and thus a table can only have one column of this type. The timestamp syntax is actually deprecated and is now named rowversion which makes a lot more sense.
Given your column names (Start, End) I assume you are trying to store actual timestamps, and should instead be using datetime as your datatype.
In Sql Server timestamp is a data type and it's not a time.
It's basically a way of versioning a record and it's used for optimistic locking in a disconnected database model
When you load up the record, you pick up the timestamp column. You only write it back if the value in the timestamp column is the same, as that means no one else has changed it since you got it.
If you want a real datetime value, add a datetime either not null, or with a default of GetDate() and remember to update every update.

What to use in DB2 for CURRENT_TIMESTAMP?

I am converting some of my MySQL statements to DB2 database, but I faced a problem on the following query
CREATE TABLE RFX_EVENT_MAPPING (
EVENT_TYPE varchar(4) NOT NULL,
EVENT_DESC varchar(50) NOT NULL,
EVENT_CLASS varchar(50) default NULL,
OWNER varchar(6) default NULL,
LAST_UPDATE_TIME timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
LAST_UPDATE_USER varchar(20) NOT NULL
);
As you can see there is
LAST_UPDATE_TIME timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
Which is not working so how can I achieve the same functionality with db2?
In DB2 9.7 for Linux, UNIX, Windows, IBM added the concept of a row change timestamp.
create table rcttest (
c1 int,
c2 char(10),
insert_ts timestamp not null with default current timestamp,
change_ts timestamp not null generated always for each row
on update as row change timestamp
);

Resources