最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can a temporary table in Db2 have a primary key? - Stack Overflow

programmeradmin3浏览0评论

I have created temporary tables in other DBMSs (Postgresql, Oracle, MariaDB, SQLite, MSSQL with modification) like this:

CREATE TEMPORARY TABLE test(
    id INT NOT NULL PRIMARY KEY,
    data VARCHAR(255)
);

When I try this in Db2, I get a most helpful error message:

SQL Error [42995]: The statement was not processed because the statement refers to a created temporary table or a declared temporary table and includes functionality that cannot be used with temporary tables.. SQLCODE=-526, SQLSTATE=42995, DRIVER=4.33.31

… which, I must confess, doesn’t enlighten me very much.

On experimenting, I’ve narrowed the fault down to the PRIMARY KEY.

Is there a way of including a primary key in a DB2 temporary table?

I created a table space using:

CREATE USER TEMPORARY TABLESPACE temp_tablespace
MANAGED BY AUTOMATIC STORAGE;

which was basically copied from somewhere.

I am using Db2 12 in a Docker container (LUW).

I have created temporary tables in other DBMSs (Postgresql, Oracle, MariaDB, SQLite, MSSQL with modification) like this:

CREATE TEMPORARY TABLE test(
    id INT NOT NULL PRIMARY KEY,
    data VARCHAR(255)
);

When I try this in Db2, I get a most helpful error message:

SQL Error [42995]: The statement was not processed because the statement refers to a created temporary table or a declared temporary table and includes functionality that cannot be used with temporary tables.. SQLCODE=-526, SQLSTATE=42995, DRIVER=4.33.31

… which, I must confess, doesn’t enlighten me very much.

On experimenting, I’ve narrowed the fault down to the PRIMARY KEY.

Is there a way of including a primary key in a DB2 temporary table?

I created a table space using:

CREATE USER TEMPORARY TABLESPACE temp_tablespace
MANAGED BY AUTOMATIC STORAGE;

which was basically copied from somewhere.

I am using Db2 12 in a Docker container (LUW).

Share Improve this question asked Mar 18 at 5:53 ManngoManngo 16.6k13 gold badges104 silver badges148 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

While primary keys cannot be created on temporary tables in DB2, regular indices can, according to the documentation:

You can also create indexes on the declared temporary table.

So what you could to partially simulate a primary key would be to add a unique index which is not null:

CREATE TEMPORARY TABLE test(
    id INT NOT NULL,
    data VARCHAR(255)
);

CREATE UNIQUE INDEX idx ON test (id);
发布评论

评论列表(0)

  1. 暂无评论