SQL> create table test (id number,zs number default 10);Table createdSQL> insert into test(id) values(1);1 row insertedSQL> select * from test; ID ZS ---------- ---------- 1 10SQL> 没问题啊
SQL> create table t(idx int,num int default(0) not null);Table createdSQL> insert into t(idx) values(10);1 row insertedSQL> insert into t(idx,num) values(10,null);insert into t(idx,num) values(10,null)ORA-01400: cannot insert NULL into ("SYS"."T"."NUM")SQL> insert into t(idx,num) values(10,nvl(null,0));1 row insertedSQL> select * from t; IDX NUM --------------------------------------- --------------------------------------- 10 0 10 0SQL>
默认值是在你不明确给出值的时候自动给的值。 需要这么用: CREATE TABLE t(n INT DEFAULT 1 NOT NULL ,m INT); INSERT INTO t (m)VALUES(3); SELECT * FROM t; 输出: N M 1 3
在不行,读取的时候,用nvl(字段,0)
你在命令行里输入desc tablename不就知道default是什么了吗。
insert into test(id) values(default);
引用 5 楼 oracledbalgtu 的回复: SQL code默认值是在你不明确给出值的时候自动给的值。 需要这么用: CREATE TABLE t(n INT DEFAULT 1 NOT NULL ,m INT); INSERT INTO t (m)VALUES(3); SELECT * FROM t; 输出: N M 1 3 引用 1 楼 oracledbalgtu 的回复: 如果设置了: not null属性并指定了默认值,你的这种情况在oracle是不存在的。 把你插入的语句和贴出来,帮你改改。 引用楼主 asdk3ksks 的帖子: 建表的时候其实设置了。。… up
SQL> set serveroutput on; SQL> SQL> declare 2 s nvarchar2(20); 3 sdefault nvarchar2(20); 4 sout nvarchar2(20); 5 begin 6 s := null; 7 sdefault := '0'; 8 select decode(trim(s),null,sdefault,'',sdefault) into sout from dual; 9 dbms_output.put_line('Input equal null :'||sout); 10 s := ' '; 11 select decode(trim(s),null,sdefault,'',sdefault) into sout from dual; 12 dbms_output.put_line('Input equal blank :'||sout); 13 end; 14 /Input equal null :0 Input equal blank :0PL/SQL procedure successfully completedSQL>
把你插入的语句和贴出来,帮你改改。
=====================================
报什么错误?
如果传过来的数据是null。那么让它插入默认值
==========================================
这个仅用默认值是做不到的
---------- ----------
1 10SQL>
没问题啊
SQL> create table t(idx int,num int default(0) not null);Table createdSQL> insert into t(idx) values(10);1 row insertedSQL> insert into t(idx,num) values(10,null);insert into t(idx,num) values(10,null)ORA-01400: cannot insert NULL into ("SYS"."T"."NUM")SQL> insert into t(idx,num) values(10,nvl(null,0));1 row insertedSQL> select * from t; IDX NUM
--------------------------------------- ---------------------------------------
10 0
10 0SQL>
需要这么用:
CREATE TABLE t(n INT DEFAULT 1 NOT NULL ,m INT);
INSERT INTO t (m)VALUES(3);
SELECT * FROM t;
输出:
N M
1 3
SQL code默认值是在你不明确给出值的时候自动给的值。
需要这么用:
CREATE TABLE t(n INT DEFAULT 1 NOT NULL ,m INT);
INSERT INTO t (m)VALUES(3);
SELECT * FROM t;
输出:
N M
1 3
引用 1 楼 oracledbalgtu 的回复:
如果设置了: not null属性并指定了默认值,你的这种情况在oracle是不存在的。
把你插入的语句和贴出来,帮你改改。
引用楼主 asdk3ksks 的帖子:
建表的时候其实设置了。。…
up
SQL>
SQL> declare
2 s nvarchar2(20);
3 sdefault nvarchar2(20);
4 sout nvarchar2(20);
5 begin
6 s := null;
7 sdefault := '0';
8 select decode(trim(s),null,sdefault,'',sdefault) into sout from dual;
9 dbms_output.put_line('Input equal null :'||sout);
10 s := ' ';
11 select decode(trim(s),null,sdefault,'',sdefault) into sout from dual;
12 dbms_output.put_line('Input equal blank :'||sout);
13 end;
14 /Input equal null :0
Input equal blank :0PL/SQL procedure successfully completedSQL>