如何在我不知道名称

时间:2016-03-10 15:46:49

标签: sql db2

我有以下专栏:

CREATE TABLE MyTable
(
  ...
  PAGE_SETUP CHAR(1)    DEFAULT 0  check (PAGE_SETUP in (0, 1))  
);

我想创建更改检查约束的脚本。我希望得到以下结果:

CREATE TABLE MyTable
(
  ...
  PAGE_SETUP CHAR(1)    DEFAULT '0'  check (PAGE_SETUP in ('0', '1'))  
);

我不知道如何使用sql查询更改检查约束。我认为我可以删除并创建检查约束但我不知道它的名字。

1 个答案:

答案 0 :(得分:1)

如果命名检查约束,则可以更轻松地运行alter table语句。否则,您必须查看 sysibm.check_constraints 以查找约束的名称。这是一个示例脚本,可以帮助您入门。

connect to pocdb;
drop table stack.check_test;

create table stack.check_test (
    f1 integer not null default 0,
    constraint f1_check check ( f1 in (0,1) )
);

insert into stack.check_test (f1) values (0);
insert into stack.check_test (f1) values (1);
insert into stack.check_test (f1) values (2);

alter table stack.check_test
    drop check f1_check;

alter table stack.check_test
    add constraint f1_check check ( f1 in (0,1,2) );

insert into stack.check_test (f1) values (0);
insert into stack.check_test (f1) values (1);
insert into stack.check_test (f1) values (2);
insert into stack.check_test (f1) values (3);

connect reset;
terminate;

最终执行的脚本是:

connect to pocdb

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = POCDB


drop table stack.check_test
DB20000I  The SQL command completed successfully.

create table stack.check_test ( f1 integer not null default 0, constraint f1_check check ( f1 in (0,1) ) )
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (0)
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (1)
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (2)
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0545N  The requested operation is not allowed because a row does not
satisfy the check constraint "STACK.CHECK_TEST.F1_CHECK".  SQLSTATE=23513

alter table stack.check_test drop check f1_check
DB20000I  The SQL command completed successfully.

alter table stack.check_test add constraint f1_check check ( f1 in (0,1,2) )
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (0)
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (1)
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (2)
DB20000I  The SQL command completed successfully.

insert into stack.check_test (f1) values (3)
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0545N  The requested operation is not allowed because a row does not
satisfy the check constraint "STACK.CHECK_TEST.F1_CHECK".  SQLSTATE=23513

connect reset
DB20000I  The SQL command completed successfully.

terminate
DB20000I  The TERMINATE command completed successfully.