【问题标题】:drop foreign key constraint on DB2 table through alter table通过 alter table 删除 DB2 表的外键约束
【发布时间】:2021-11-23 03:30:36
【问题描述】:

我有一个如下创建的 DB2(用于 IBMi)表。我想在 SQLRPGLE 程序中运行时删除外键约束。这可能吗?

 create table grid_action_details(id integer not null                   
   generated always as identity                                         
 (start with 1 increment by 1)                                          
  PRIMARY KEY,grid_details_id integer,foreign key(grid_details_id)      
 references grid_details(id),action_code_details_id integer,            
 foreign key(action_code_details_id)                                    
 references action_code_details(id),action_code_status varchar(2),      
 created_date date default                                              
 current_date,created_by varchar(30),                                   
 last_updated_date date default current_date,updated_by                 
 varchar(30),required_parameter clob);                   

我尝试了以下语法,但它似乎对我不起作用:

ALTER TABLE table-name
      DROP FOREIGN KEY foreign_key_name 


alter table iesqafile.grid_action_details                 
            drop foreign key action_code_details_id       
ACTION_CODE_DETAILS_ID in IESQAFILE type *N not found.    
           

【问题讨论】:

    标签: db2-400


    【解决方案1】:

    您通过名称而不是列删除 FK 约束。

    由于您在创建过程中没有指定名称,因此您需要查看系统生成的名称。

    最好的做法是自己命名事物。 CONSTAINT <name> FOREIGN KEY (<columns>)

    create table grid_action_details (
          id integer not null generated always as identity (
          start with 1 increment by 1
        )
          ,constraint grid_action_details_pk 
              primary key
          ,grid_details_id integer
          ,constraint grid_details_fk 
              foreign key (grid_details_id) 
              references grid_details (id)
          ,action_code_details_id integer
          ,constraint action_code_details_fk 
              foreign key (action_code_details_id) 
              references action_code_details (id)
          ,action_code_status varchar(2)
          ,created_date date default current_date
          ,created_by varchar(30)
          ,last_updated_date date default current_date
          ,updated_by varchar(30)
          ,required_parameter clob
        );
    

    现在您可以按已知名称删除

    alter table iesqafile.grid_action_details                 
                drop foreign key action_code_details_fk
    

    编辑
    要查找生成的名称,请使用:

    • ACS 架构组件
    • DSPFD
    • 针对 catalog views 之一的 SQL(QSYS2.SYSCST、SYSIBM.SQLFOREIGNKEYS、SYSIBM.REFERENTIAL_CONSTRAINTS)

    【讨论】:

    • 如何找到已知名称?我试图在 DBU 中打开表格并使用显示的字段名称,但这也不起作用?有没有办法找到这些信息?
    • 更改表 GRID_ACTION_details DROP FOREIGN KEY GRID_00001 ||未找到 IESQAFILE 类型 *N 中的 GRID_00001。这就是我找到系统名称的方式: SELECT SYSTEM_COLUMN_NAME FROM QSYS2.SYSCOLUMNS WHERE TABLE_NAME = 'GRID_ACTION_DETAILS' AND TABLE_SCHEMA = 'IESQAFILE' AND COLUMN_NAME = 'ACTION_CODE_DETAILS_ID'
    • @theju112t 查看编辑
    • 您找到了系统为列生成的名称,而不是约束。
    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 2022-01-25
    • 2018-12-16
    • 2012-04-28
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多