📄 nulls.out
字号:
ij> ---- this test shows the current supported null value functionality--autocommit off;ij> -- trying to define null and not null for a columncreate table a(a1 int null not null);ERROR 42X01: Syntax error: Encountered "null" at line 2, column 23.ij> -- same as above, except that it's in reverse ordercreate table a(a1 int not null null);ERROR 42X01: Syntax error: Encountered "null" at line 2, column 32.ij> -- defining null constraint for a column now does not workcreate table a(a1 int not null , a2 int not null);0 rows inserted/updated/deletedij> -- alter table adding explicitly nullable column and primary key column-- constraint on it failsalter table a add column a3 int null constraint ap1 primary key;ERROR 42X01: Syntax error: Encountered "null" at line 3, column 33.ij> -- alter table table level primary key constraint on nullable column-- doesn't give an erroralter table a add constraint ap1 primary key(a1,a2);0 rows inserted/updated/deletedij> drop table a;0 rows inserted/updated/deletedij> -- create table with not null column and unique key should workcreate table a (a int not null unique );0 rows inserted/updated/deletedij> insert into a values (1);1 row inserted/updated/deletedij> -- second insert should failinsert into a values (1);ERROR 23505: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'xxxxGENERATED-IDxxxx' defined on 'A'.ij> drop table a;0 rows inserted/updated/deletedij> -- alter nullability on a unique column should failcreate table a ( a int not null unique);0 rows inserted/updated/deletedij> alter table a modify a null;ERROR 42X01: Syntax error: Encountered "modify" at line 1, column 15.ij> drop table a;0 rows inserted/updated/deletedij> -- try adding a primary key where there is null data-- this should errorcreate table a (a1 int not null, a2 int);0 rows inserted/updated/deletedij> insert into a values(1, NULL);1 row inserted/updated/deletedij> alter table a add constraint ap1 primary key(a1, a2);ERROR 42831: 'A2' cannot be a column of a primary key or unique key because it can contain null values.ij> drop table a;0 rows inserted/updated/deletedij> -- try with multiple columnscreate table a (a1 int, a2 int, a3 int);0 rows inserted/updated/deletedij> -- This is an error in DB2 compatibility modealter table a add constraint ap1 primary key(a1, a2, a3);ERROR 42831: 'A1' cannot be a column of a primary key or unique key because it can contain null values.ij> drop table a;0 rows inserted/updated/deletedij> -- try with multiple null columnscreate table a (a1 int not null, a2 int, a3 int);0 rows inserted/updated/deletedij> insert into a values(1,1,1);1 row inserted/updated/deletedij> -- table with no null data should workalter table a add constraint ap1 primary key(a1, a2, a3);ERROR 42831: 'A2' cannot be a column of a primary key or unique key because it can contain null values.ij> -- insert a null into one of the primary key columns should failinsert into a values(1, NULL, 1);1 row inserted/updated/deletedij> drop table a;0 rows inserted/updated/deletedij> -- try with multiple null columnscreate table a (a1 int not null, a2 int default null, a3 int default null);0 rows inserted/updated/deletedij> insert into a values(1,NULL,1);1 row inserted/updated/deletedij> -- table with some null data should failalter table a add constraint ap1 primary key(a1, a2, a3);ERROR 42831: 'A2' cannot be a column of a primary key or unique key because it can contain null values.ij> -- defining primarykey column constraint for explicitly nullable column-- gives errorcreate table a1(ac1 int null primary key);ERROR 42X01: Syntax error: Encountered "null" at line 3, column 25.ij> -- defining primarykey table constraint on explicitly nullable columns-- give errorcreate table a1(ac1 int null, ac2 int not null, primary key(ac1,ac2));ERROR 42X01: Syntax error: Encountered "null" at line 3, column 25.ij> -- say null twice should failcreate table a2(ac1 int null null);ERROR 42X01: Syntax error: Encountered "null" at line 2, column 25.ij> -- say not null, null and no null for a column. This is to make sure the flags-- stay proper for a columncreate table a3(ac1 int not null null not null);ERROR 42X01: Syntax error: Encountered "null" at line 3, column 34.ij> -- first statement says null and second one says not null. This is to make sure-- the flag for the first one doesn't affect the second onecreate table a3(ac1 int default null);0 rows inserted/updated/deletedij> create table a4(ac1 int not null);0 rows inserted/updated/deletedij> -- one column says null and second one says not nullcreate table a5(ac1 int default null, ac2 int not null);0 rows inserted/updated/deletedij> -- statement1 says null, 2nd says nothing but says primary keycreate table a6(ac1 int default null);0 rows inserted/updated/deletedij> create table a7(ac1 int not null primary key);0 rows inserted/updated/deletedij> -- create a table with null and non-null columnscreate table t (i int, i_d int default null, i_n int not null, s smallint, s_d smallint default null, s_n smallint not null);0 rows inserted/updated/deletedij> -- insert non-nulls into null and non-null columnsinsert into t (i, i_d, i_n, s, s_d, s_n) values (1, 1, 1, 1, 1, 1);1 row inserted/updated/deletedij> -- insert nulls into those columns that take nullsinsert into t values (null, null, 2, null, null, 2);1 row inserted/updated/deletedij> -- insert a null as a default value into the first default null columninsert into t (i, i_n, s, s_d, s_n) values (3, 3, 3, 3, 3);1 row inserted/updated/deletedij> -- insert a null as a default value into the other default null columnsinsert into t (i, i_d, i_n, s, s_n) values (4, 4, 4, 4, 4);1 row inserted/updated/deletedij> -- insert nulls as default values into all default null columnsinsert into t (i, i_n, s, s_n) values (5, 5, 5, 5);1 row inserted/updated/deletedij> -- attempt to insert default values into the columns that don't accept nullsinsert into t (i, i_d, s, s_d) values (6, 6, 6, 6);ERROR 23502: Column 'I_N' cannot accept a NULL value.ij> -- insert default nulls into nullable columns that have no explicit defaultsinsert into t (i_d, i_n, s_d, s_n) values (7, 7, 7, 7);1 row inserted/updated/deletedij> -- attempt to insert an explicit null into a column that doesn't accept nullsinsert into t values (8, 8, null, 8, 8, 8);ERROR 23502: Column 'I_N' cannot accept a NULL value.ij> -- attempt to insert an explicit null into the other columns-- that doesn't accept nullsinsert into t values (9, 9, 9, 9, 9, null);ERROR 23502: Column 'S_N' cannot accept a NULL value.ij> -- select all the successfully inserted rowsselect * from t;I |I_D |I_N |S |S_D |S_N --------------------------------------------------------1 |1 |1 |1 |1 |1 NULL |NULL |2 |NULL |NULL |2 3 |NULL |3 |3 |3 |3 4 |4 |4 |4 |NULL |4 5 |NULL |5 |5 |NULL |5 NULL |7 |7 |NULL |7 |7 ij> -- create a table with a non-null column with a default value of null-- and verify that nulls are not allowedcreate table s (x int default null not null, y int);0 rows inserted/updated/deletedij> insert into s (y) values(1);ERROR 23502: Column 'X' cannot accept a NULL value.ij> select * from s;X |Y -----------------------ij> -- is null/is not null on an integer typecreate table u (c1 integer);0 rows inserted/updated/deletedij> insert into u values null;1 row inserted/updated/deletedij> insert into u values 1;1 row inserted/updated/deletedij> insert into u values null;1 row inserted/updated/deletedij> insert into u values 2;1 row inserted/updated/deletedij> select * from u where c1 is null;C1 -----------NULL NULL ij> select * from u where c1 is not null;C1 -----------1 2 ij> -- is [not] null and parametersprepare p1 as 'select * from u where cast (? as varchar(1)) is null';ij> execute p1 using 'values (''a'')';C1 -----------ij> prepare p2 as 'select * from u where cast (? as varchar(1)) is not null';ij> execute p2 using 'values (''a'')';C1 -----------NULL 1 NULL 2 ij> select count(*) from u where c1 is null;1 -----------2 ij> insert into u select * from (values null) as X;1 row inserted/updated/deletedij> select count(*) from u where c1 is null;1 -----------3 ij> -- cleanupdrop table t;0 rows inserted/updated/deletedij> drop table s;0 rows inserted/updated/deletedij> drop table u;0 rows inserted/updated/deletedij> drop table a;0 rows inserted/updated/deletedij> drop table a3;0 rows inserted/updated/deletedij> drop table a4;0 rows inserted/updated/deletedij> drop table a5;0 rows inserted/updated/deletedij> drop table a6;0 rows inserted/updated/deletedij> drop table a7;0 rows inserted/updated/deletedij>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -