根据informix中复杂select语句的输出删除外键约束?

时间:2017-09-28 17:19:36

标签: sql foreign-keys informix

我正在使用INFORMIX数据库。

我已经在表中添加了forign键而没有给它起一个名字,例如。

ALTER TABLE myreport ADD CONSTRAINT FOREIGN KEY (rid) REFERENCES report(id), ADD CONSTRAINT FOREIGN KEY (uid) REFERENCES user(id);

我想根据select语句的输出删除其forign键约束,如下所示:

ALTER TABLE myreport DROP CONSTRAINT (
   SELECT b.constrname
       FROM
           systables a, sysconstraints b, sysindexes c, syscolumns d
       WHERE
           a.tabname = 'myreport' AND (d.colname='rid' or d.colname='uid')
       AND
           b.tabid = a.tabid
       AND
           c.idxname = b.idxname
       AND
           d.tabid = a.tabid
       AND
       (
           d.colno = c.part1 or d.colno = c.part2 or d.colno = c.part3 or d.colno = c.part4 or
           d.colno = c.part5 or d.colno = c.part6 or d.colno = c.part7 or d.colno = c.part8 or
           d.colno = c.part9 or d.colno = c.part10 or d.colno = c.part11 or d.colno = c.part12 or
           d.colno = c.part13 or d.colno = c.part14 or d.colno = c.part15 or d.colno = c.part16
       )
);
  

会抛出错误201: A syntax error has occurred.

但是当我单独运行它时它工作正常并提供如下输出:

:选择:

SELECT b.constrname
       FROM
           systables a, sysconstraints b, sysindexes c, syscolumns d
       WHERE
           a.tabname = 'myreport' AND (d.colname='rid' or d.colname='uid')
       AND
           b.tabid = a.tabid
       AND
           c.idxname = b.idxname
       AND
           d.tabid = a.tabid
       AND
       (
           d.colno = c.part1 or d.colno = c.part2 or d.colno = c.part3 or d.colno = c.part4 or
           d.colno = c.part5 or d.colno = c.part6 or d.colno = c.part7 or d.colno = c.part8 or
           d.colno = c.part9 or d.colno = c.part10 or d.colno = c.part11 or d.colno = c.part12 or
           d.colno = c.part13 or d.colno = c.part14 or d.colno = c.part15 or d.colno = c.part16
       )

给予:

constrname  r169_278
constrname  r169_279
constrname  r169_280

ALTER TABLE:

ALTER TABLE cuicscheduledreports DROP CONSTRAINT (r169_278, r169_279)

显示成功日志

  
    

所以我需要一组能够以动态方式实现我的需求的查询,因为它将用作不同系统上db的迁移

  

1 个答案:

答案 0 :(得分:1)

DROP CONSTRAINT不允许使用约束名称的子查询。必须明确命名约束名称。

此处记录了DROP CONSTRAINT的语法:https://www.ibm.com/support/knowledgecenter/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_0069.htm

<table >
          <thead>
          <tr>
              <th> Market</th>
              <th> </th>
              <th>Average</th>
          </tr>
          <tr>
              <th ><form >
                  <input type="text" class="form-control"  placeholder="Filter market">
              </form></th>
              <th> </th>
              <th></th>
          </tr>
          </thead>
          <tbody>

          <tr ng-repeat-start="(key, value) in rc.runRate">
              <td>{{key}}</td>
              <td> </td>
              <td> </td>
          </tr>
          <tr ng-repeat-end class="slide-toggle-js">
              <td colspan="3" >
                  <div  class="toggle-me" >

                  <table>
                      <thead>
                      <tr >
                          <th>Syscode</th>
                          <th>Network</th>
                          <th>Date</th>
                          <th>Average</th>

                      </tr>
                      <tr>
                          <th>
                              <form>
                                  <input class="form-control"  placeholder="Filter Syscode">
                              </form>
                          </th>
                          <th>
                              <form>
                                  <input class="form-control"  placeholder="Filter Network">
                              </form>
                          </th>
                          <th></th>
                          <th></th>


                      </tr>
                      </thead>
                      <tbody ng-cloak>
                      <tr  ng-class-odd="'stripe-row-odd'" ng-class-even="'stripe-row-even'" ng-repeat="(key, value) in key">
                          <td>{{key}}</td>
                          <td></td>
                          <td></td>
                          <td> </td>

                      </tr>
                      </tbody>
                  </table>
                  </div>

只允许以逗号分隔的名称列表,而不是子查询。

您必须分两步完成此任务,如您所发现的那样。

将来,您应该避免编写虚构的语法并期望它能够正常工作。查看手册以了解语法选项。