我是Oracle的新手,希望在以下方面提供帮助:
*注意:由一个序列生成的“租金”表中的Vehicle_Id
create table configuration
(config_id int not null,
SEAT NUMBER(2),
2 3 4 DOORS NUMBER(2),
5 LARGE_BAGS NUMBER(2),
SMALL_BAGS NUMBER(2),
Vehicle_Type Varchar2(25),
primary key(config_id));
6 7 8
Table created.
SQL> SQL>
create sequence config_seq start with 1 increment by 1 nocache;
SQL>
Sequence created.
SQL> SQL>
SQL> insert into configuration (seat,doors,large_bags,small_bags,vehicle_type,config_id)
select x.seat,x.doors,x.large_bags,x.small_bags,x.vehicle_type,config_seq.nextval from (select distinct seat,doors,large_bags,small_bags,vehicle_type from rentals) x;2 3
12 rows created.
SQL> SQL> SQL> create table Vehicle
(VEHICLE_ID int,
VEHICLE VARCHAR2(25) NOT NULL,
2 3 4 RENTAL_COMMENT CHAR(20),
5 COMPANY_ID int,
config_id int,
Primary key(VEHICLE_ID),
6 7 8 foreign key(company_id) references company(company_id),
9 foreign key(config_id) references configuration(config_id) on delete cascade);
Table created.
SQL>
SQL> insert into Vehicle (vehicle_id,vehicle,rental_comment,company_id)
select vehicle_id,vehicle,rental_comment,company_id from rentals; 2
20 rows created.
SQL> insert into vehicle(config_id) select config_id from configuration;
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("RGAKHAR"."VEHICLE"."VEHICLE_ID")发布于 2017-01-13 11:53:38
你对这个错误有什么不理解的?您已经将vehicle.vehicle定义为NOT NULL。您的insert看起来像:
insert into vehicle(config_id)
select config_id from configuration;列的默认值是NULL,这是不允许的。你有三个选择:
insert中指定一个值。NULL值。default值。有可能要更新现有行。如果是这样的话,您应该正确地插入数据。以下是你的意图的一种可能:
insert into Vehicle (vehicle_id, vehicle, rental_comment, company_id, config_id)
select r.vehicle_id, r.vehicle, r.rental_comment, r.company_id,
c.config_id
from rentals r join
configuration c
on r.vehicle_type = c.vehicle_type; -- just a guess发布于 2017-01-13 11:56:36
它没有从配置中获取config_id,请确认它,并且它也是vehicle表中的NOT NULL列:
因此,您可以在插入以下语句时使config_id为空或提供任何默认值:
insert into vehicle(config_id) select config_id from configuration;发布于 2017-01-13 11:58:06
您的表车辆没有空列车辆。在插入数据时使用
SQL>插入车辆( config_id )从配置中选择config_id;
您没有提供不为空的所有字段。
您需要只在第一次输入时插入config_id,或者使用更新查询来填充config_id。
https://stackoverflow.com/questions/41634013
复制相似问题