简体   繁体   English

DB2 通过 SQL 魔术“PERSIST”将主键添加到从 Jupyter 实验室创建的现有表中

[英]DB2 add Primary Key to existing table created from Jupyter Lab via SQL magic 'PERSIST'

I created sample table from Jupyter Lab notebook via:我通过以下方式从 Jupyter Lab 笔记本创建了示例表:

file_csv = 'data.csv'
df = pd.read_csv(file_csv)
df.insert(0, 'KEY_ID', df.index)

Then added table to DB2 on IBM cloud via:然后通过以下方式将表添加到 IBM 云上的 DB2:

%sql PERSIST df

Looking at that table in DB2 console it is created w/o primary key - so I tried to remedy it:查看 DB2 控制台中的那个表,它是在没有主键的情况下创建的 - 所以我试图纠正它:

ALTER TABLE DF
    ALTER COLUMN KEY_ID
    SET NOT NULL; 

And set KEY_ID as primary key:并将KEY_ID设置为主键:

ALTER TABLE DF
    ADD PRIMARY KEY(KEY_ID);

But DB2 stubbornly refuses with following error:但是 DB2 顽固地拒绝并出现以下错误:

InternalError: (ibm_db_dbi.InternalError) ibm_db_dbi::InternalError: Exception('Statement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0668N Operation not allowed for reason code "7" on table "MYID9999.DF". SQLSTATE=57016\r SQLCODE=-668') InternalError: (ibm_db_dbi.InternalError) ibm_db_dbi::InternalError: Exception('Statement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0668N 由于表“MYID9999.DF”上的原因代码“7”,不允许操作。SQLSTATE =57016\r SQLCODE=-668')
[SQL: ALTER TABLE DF [SQL:更改表 DF
ADD PRIMARY KEY(KEY_ID);]添加主键(KEY_ID);]
(Background on this error at: http://sqlalche.me/e/2j85 ) (此错误的背景: http://sqlalche.me/e/2j85

OK - I actually solved it while typing the question - but I will post it anyway.好的 - 我实际上在输入问题时解决了它 - 但无论如何我都会发布它。 You need to REORG the table (I guess after resetting 'KEY_ID' column props to NOT NULL) and it only worked from when run like this:您需要重组表(我猜是在将 'KEY_ID' 列属性重置为 NOT NULL 之后)并且它仅在像这样运行时才起作用:

CALL SYSPROC.ADMIN_CMD ('REORG TABLE DF')

But after that ALTER command works like a charm.但在那之后 ALTER 命令就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM