Sql与oracle中null值(二)

2014-11-24 12:59:27 · 作者: · 浏览: 1
e table t(a number,b number);

Table created.

SQL> create unique index idx_t on t(a,b);

Index created.

SQL> insert into t values(null,null);

1 row created.

SQL> insert into t values(1,null);

1 row created.

SQL> insert into t values(null,1);

1 row created.

SQL> commit;
SQL> insert into t values(1,null);
insert into t values(1,null)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.IDX_T) violated

7 null的排序

order by默认升序(asc),这时候null是排在最后的,如果指定降序那么null是排在最前面的,认为null最大。
但是可以用nulls first和nulls last进行调整。
SQL> select * from emp
2 order by comm asc;
SQL> select * from emp
2 order by comm desc;
SQL> select *from emp
2 order by comm asc nulls first;
SQL> select *from emp
2 order by comm desc nulls last;

8 null与性能的关系

Not null约束,定义约束是要付出消耗性能的代价的,由下面的测试可以看出虽然约束检查的很快,但是有时候还是很消耗资源的,至少在这个例子上是这样的,不需要not null约束,除非必要,不要乱定义约束。
SQL> set serveroutput on
SQL> declare
2 v_value number not null :=0;
3 start_time number;
4 end_time number;
5 begin
6 start_time :=DBMS_UTILITY.GET_TIME;
7 FOR i in 0..100000000 LOOP
8 v_value :=i;
9 end LOOP;
10 end_time :=DBMS_UTILITY.GET_TIME;
11 DBMS_OUTPUT.PUT_LINE(end_time-start_time);
12 END;
13 /
1043

PL/SQL procedure successfully completed.

SQL> declare
2 v_value number;
3 start_time number;
4 end_time number;
5 begin
6 start_time :=DBMS_UTILITY.GET_TIME;
7 FOR i IN 0..100000000 LOOP
8 v_value :=i;
9 end LOOP;
10 end_time :=DBMS_UTILITY.GET_TIME;
11 DBMS_OUTPUT.PUT_LINE(end_time-start_time);
12 END;
13 /
767

9 动态语句中的绑定变量与null

在PL/SQL中动态SQL和动态PL/SQL经常使用绑定变量,这个绑定变量有个要求,就是不能直接传入字面量null值,因为PL/SQL中动态语句要求传入的绑定变量必须是SQL类型,而字面量null是无类型的,null字面量传入是不可以的。当然可以采用多种方法,如果一定要传入null,则可以将null改为空字符串、TO_NUMBER,TO_CHAR,TO_DATE等函数进行转换,或定义一个未初始化的变量、直接传入变量等。
SQL> create table test(id number,name varchar2(10),birth date);

Table created.

SQL> insert into test values(1,'aa',SYSDATE);

1 row created.

SQL> insert into test values(null,'aa',SYSDATE);

1 row created.

SQL> COMMIT;

Commit complete.

SQL> declare
2 v_sql varchar2(4000);
3 begin
4 v_sql :='update test set birth=:vbirth where id is null';
5 execute immediate v_sql using null;
6 commit;
7 end;
8 /
execute immediate v_sql using null;
*
ERROR at line 5:
ORA-06550: line 5, column 31:
PLS-00457: expressions have to be of SQL types
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
SQL> declare
2 v_sql varchar2(4000);
3 begin
4 v_sql :='update test set birth=:vbirth where id is null';
5 execute immediate v_sql using '';
6 commit;
7 end;
8 /

PL/SQL procedure successfully completed.

10 sqlserver 中null值介绍
创建示例表, Script 如下:
create table dbo. cassaba_null
(
column1 nvarchar ( 50) not null,
column2 nvarchar ( 50) null
)
go

insert into dbo. cassaba_null values ( '1' , null)
insert into dbo. cassaba_null values ( '2' , 'string' )
insert into dbo. cassaba_null values ( '3' , '' )
go

a. 使用 =null / <>null 默认情况下的确不能使用 =null / <> null 来判断 null 值如此。实际上 SQL Server 可以 使用 SET ANSI_NULLS { ON | OFF } 设定来控制 =null / <>null 的行为。
当 SET ANSI_NULLS 为 ON 时,即使 column_name 中包含空值,使用 WHERE column_name = NULL 的 SELECT 语句仍返回零行。
即使 column_name 中包含非空值,使用 WHERE column_name <> NULL 的 SELECT 语句仍会返回零行

但是当 SET ANSI_NULLS 为 OFF 时,等于 (=) 和不等于 (<>) 比较运算符不遵守 ISO 标准。
使用 WHERE column_name = NULL 的 SELECT 语句返回 column_name 中包含空值的行。
使用 WHERE column_name <> NULL 的 SELECT 语句返回列中包含非空值的行。
此外,使用 WHERE column_name <> XYZ_value 的 SELECT 语句返回所有不为 XYZ_value 也不为 NULL 的行。


b. 改变 null 值的连接行为
SQL Server 提供 SE