简体   繁体   English

ORACLE - 无法将NULL值插入NON-Primary Key

[英]ORACLE - Cannot insert a NULL value to a NON-Primary Key

I Have searched the web and various forums but I cannot figure out why this won't work. 我搜索过网络和各种论坛,但我无法弄清楚为什么这不起作用。 My Database is made up from the following Tables: 我的数据库由以下表格组成:

CREATE TABLE CUSTOMER(
custid Number(4),
cfirstname varchar2(30),
csurname varchar2(20) NOT NULL, 
billingaddr varchar2(30), 
cgender varchar2(1),
CONSTRAINT custpk PRIMARY KEY (custid),
CONSTRAINT genderconst CHECK(cgender in ('M','F','m','f'))
);

CREATE TABLE PRODUCT(
prodid Number(4),
prodname varchar2(30),
currentprice Number(6,2),
CONSTRAINT cprice_chk CHECK(currentprice >= 0 AND currentprice <=5000 ),
CONSTRAINT prodpk PRIMARY KEY (prodid),
CONSTRAINT pricepos CHECK((currentprice >= 0))
);

CREATE TABLE SALESPERSON(
spid Number(4),
spfirstname varchar2(30),
spsurname varchar2(30),
spgender varchar2(1),
CONSTRAINT salespk PRIMARY KEY (spid)
);

CREATE TABLE SHOPORDER(
ordid Number(4),
deliveryaddress varchar2(30),
custid Number(4) NOT NULL,
spid Number(4) NOT NULL,
CONSTRAINT orderpk PRIMARY KEY (ordid),
CONSTRAINT orderfk1 FOREIGN KEY (custid) REFERENCES CUSTOMER(custid),
CONSTRAINT orderfk2 FOREIGN KEY (spid) REFERENCES SALESPERSON(spid)
);

CREATE TABLE ORDERLINE(
qtysold Number(4),
qtydelivered Number(4),
saleprice Number (6,2),
ordid Number(4) NOT NULL,
prodid Number(4) NOT NULL,
CONSTRAINT qty_chk CHECK (qtydelivered >= 0 AND qtydelivered <=99),
CONSTRAINT price_chk CHECK(saleprice >= 0 AND saleprice <=5000 ),
CONSTRAINT linefk1 FOREIGN KEY (ordid) REFERENCES SHOPORDER(ordid),
CONSTRAINT linefk2 FOREIGN KEY (prodid) REFERENCES PRODUCT(prodid)
);

And I am using an insert statement to insert the following: 我使用insert语句插入以下内容:

INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);

Whether I use '' or NULL it gives me the error: 无论我使用''还是NULL,它都会给我错误:

ORA-01400: cannot insert NULL into ("S9710647"."SHOPORDER"."CUSTID"); ORA-01400:无法插入NULL(“S9710647”。“SHOPORDER”。“CUSTID”);

My issue that I have not set deliveryaddress as a Primary key nor is it a Foreign key or contain any NOT NULL CoNSTRAINTS. 我的问题是我没有将deliveryaddress设置为主键,也不是外键或包含任何NOT NULL CoNSTRAINTS。

Is there a factor that I am missing here? 我在这里缺少一个因素吗? The majority of forums have had people with problems relating to constraints. 大多数论坛都有人遇到与限制有关的问题。 I cannot see any conflicting constraints. 我看不出任何冲突的约束。

Cheers 干杯

You're only inserting the columns ordid , deliveryaddress and spid into SHOPORDER which means the others will probably default to NULL . 你只是将列ordiddeliveryaddressspid插入SHOPORDER ,这意味着其他人可能会默认为NULL

However, you've declared custId as NOT NULL so that's not allowed. 但是,您已将custId声明为NOT NULL因此不允许这样做。 You can actually tell what the complaint is by looking at the error message: 您可以通过查看错误消息实际告诉投诉是什么:

ORA-01400: cannot insert NULL into ("S9710647"."SHOPORDER"."CUSTID");
                                                            ^^^^^^

It's clearly having troubles with the CUSTID column there and you know you haven't explicitly set that, so it must be the default value causing you grief. 它显然在那里有CUSTID列的麻烦,你知道你没有明确设置它,所以它必须是导致你悲伤的默认值。

You can fix it by either inserting a specific value in to that column as well, or by giving a non-NULL default value to it, though you'll have to ensure the default exists in the CUSTOMER table lest the orderfk1 foreign key constraint will fail. 你也可以通过在该列中插入一个特定的值来修复它,或者通过给它一个非NULL的默认值来修复它,尽管你必须确保CUSTOMER表中存在默认值,以免orderfk1外键约束失败。

The problem is that this: 问题是这个:

INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);

uses the default values for all columns that you don't specify an explicit value for, so it's equivalent to this: 使用未指定显式值的所有列的默认值,因此它等效于:

INSERT INTO SHOPORDER(ordid, deliveryaddress, custid, spid)
VALUES (41, NULL, NULL, 23);

which violates the NOT NULL constraint on custid . 这违反了custid上的NOT NULL约束。

CREATE TABLE SHOPORDER(
ordid Number(4),
deliveryaddress varchar2(30),
custid Number(4) NOT NULL,
spid Number(4) NOT NULL,
CONSTRAINT orderpk PRIMARY KEY (ordid),
CONSTRAINT orderfk1 FOREIGN KEY (custid) REFERENCES CUSTOMER(custid),
CONSTRAINT orderfk2 FOREIGN KEY (spid) REFERENCES SALESPERSON(spid)
);

INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);

Your problem is with custid which is defines as NOT NULL . 你的问题是用custid定义为NOT NULL You do not specify a value for it, hence your attempt to set it to NULL . 没有为它指定值 ,因此您尝试将其设置为NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM