插入null

时间:2018-11-26 17:17:27

标签: sql database oracle

我正在尝试为Oracle中的客户表设置默认值。

这是我的编码

CREATE TABLE CUSTOMER
(
CUST_ID     VARCHAR(10),
CUST_NAME   VARCHAR(20)   NOT NULL UNIQUE,
CUST_DOB    DATE          NULL,               
CUST_STATE  VARCHAR(20)   DEFAULT 'NOT STATED',
PRIMARY KEY(CUST_ID)
);

这是我的插入内容...

INSERT INTO CUSTOMER VALUES ('C001','Murphy','1/30/1989','Melaka');
INSERT INTO CUSTOMER VALUES ('C002','Cooper','4/20/1993','Selangor');
INSERT INTO CUSTOMER VALUES ('C003','Richard','','Perak');
INSERT INTO CUSTOMER VALUES ('C004','Howard','6/24/1997','Johor');
INSERT INTO CUSTOMER VALUES ('C005','Torres','8/3/1983','Negeri Sembilan');
INSERT INTO CUSTOMER VALUES ('C006','Peterson','12/31/1990','Kedah');
INSERT INTO CUSTOMER VALUES ('C007','Gray','5/20/1999','');
INSERT INTO CUSTOMER VALUES ('C008','James','','');
INSERT INTO CUSTOMER VALUES ('C009','Watson','10/9/1993','Sabah');
INSERT INTO CUSTOMER VALUES ('C010','Brooks','9/17/1989','Terengganu');
INSERT INTO CUSTOMER VALUES ('C011','Kelly','8/23/1997','Perlis');
INSERT INTO CUSTOMER VALUES ('C012','Wendy','','');
INSERT INTO CUSTOMER VALUES ('C013','Perry','7/18/1993','Selangor');
INSERT INTO CUSTOMER VALUES ('C014','Alexander','2/13/1980','Kelantan');
INSERT INTO CUSTOMER VALUES ('C015','Gladys','','Sarawak');

当我运行程序并使用

select * from CUSTOMER

表中所有没有插入值的CUST_STATE都出现一个“-”,而不是默认值“ NOT STATED”,但是运行程序时未检测到错误。

2 个答案:

答案 0 :(得分:1)

如果您确实希望该列默认为非空值,即使INSERT语句为其设置为NULL,也可以使用DEFAULT ON NULL语法,例如:

ALTER TABLE CUSTOMER MODIFY CUST_STATE DEFAULT ON NULL 'NOT STATED';

或者,如果要从头开始创建表:

CREATE TABLE CUSTOMER
(
CUST_ID     VARCHAR(10),
CUST_NAME   VARCHAR(20)   NOT NULL UNIQUE,
CUST_DOB    DATE          NULL,               
CUST_STATE  VARCHAR(20)   DEFAULT ON NULL 'NOT STATED',
PRIMARY KEY(CUST_ID)
);

现在,当您为该列插入带有NULL的行时:

INSERT INTO CUSTOMER VALUES ('C007','Gray','5/20/1999','');

该行的'NOT STATED'中将有CUST_STATE

注意:此“未声明”被称为“魔术值”,通常被认为是不良做法。如果希望在未输入任何值的情况下在屏幕上显示“ NOT STATED”,则最好在查询时使用NVL(CUST_STATE,'NOT STATED')之类的SQL表达式。

答案 1 :(得分:0)

这是您当前正在执行的操作(C007没有CUST_STATE):

SQL> insert into customer (cust_id, cust_name, cust_dob, cust_state)
  2    values ('C007','Gray','5/20/1999', '');

1 row created.

SQL> select * from customer where cust_id = 'C007';

CUST_ID    CUST_NAME            CUST_DOB   CUST_STATE
---------- -------------------- ---------- --------------------
C007       Gray                 05/20/1999

SQL>

如您所见,CUST_STATE,而您期望为“未声明”。

INSERT有什么问题?几乎没有;这是预期的行为。仅当您未指定要插入该列的值时,Oracle才会使用该列的默认值。另一方面,您说您想在该列中放入一个空字符串(''),以便从不使用默认值。

但是,如果您完全忽略CUST_STATE列的值,它将按您希望的那样工作:

SQL> delete from customer where cust_id = 'C007';

1 row deleted.

SQL> insert into customer (cust_id, cust_name, cust_dob)
  2    values ('C007','Gray','5/20/1999');

1 row created.

SQL> select * from customer where cust_id = 'C007';

CUST_ID    CUST_NAME            CUST_DOB   CUST_STATE
---------- -------------------- ---------- --------------------
C007       Gray                 05/20/1999 NOT STATED

SQL>

因此,重写那些INSERT INTO语句。一般来说,您应该总是命名您正在使用的所有列。没错,它需要更多的输入,但是-这样-您可以控制该过程。