Oracle:对象关系数据库,如何用外键插入表值?

时间:2018-03-08 12:32:48

标签: oracle11g

实际上,我有两个表Branch和Account_table,我想插入account_table值和外键(bID)值,这是分支表(bID)中的主键,但插入值时出错。以下是我的代码。

create type bank_branch as object(
bID varchar2(10),
street varchar2(20),
city varchar2(20),
zipcode varchar2(10),
bPhone varchar2(20))
not final
/
create table branch of bank_branch(
primary key (bID),
/
insert into branch values(
'601','XYZ Street','Orlando','OR112AB','024771169');
/
insert into branch values(
'620','Terrace Road','California','CL229JH','548711131');
/
insert into branch values(
'630','Miami Street','Miami','M21334A','9665411211');
/

create type account_type as object(
accNum int,
accType varchar2(15),
balance number,
bID varchar2(10),
interest number,
overdraftLimit number,
openDate DATE)
/
create table account_table of account_type(
primary key (accNum),
FOREIGN key (bID) REFERENCES branch(bID));
/
insert into account_table 
select 'bID', b.BID
from branch b
where b.BID = 601,
'1001','current','630.87','0.009','400','10-Sep-14');
/

感谢。

1 个答案:

答案 0 :(得分:0)

create type bank_branch as object(
bID varchar2(10),
street varchar2(20),
city varchar2(20),
zipcode varchar2(10),
bPhone varchar2(20))
not final
/
create table branch of bank_branch(
primary key (bID));
/
insert into branch values(
'601','XYZ Street','Orlando','OR112AB','024771169');
/
insert into branch values(
'620','Terrace Road','California','CL229JH','548711131');
/
insert into branch values(
'630','Miami Street','Miami','M21334A','9665411211');
/

create type account_type as object(
accNum int,
accType varchar2(15),
balance number,
bID varchar2(10),
interest number,
overdraftLimit number,
openDate DATE);
/
create table account_table of account_type(
primary key (accNum),
FOREIGN key (bID) REFERENCES branch(bID));
/
insert into account_table 
select '1001','current','630.87',b.BID,'0.009','400','10-Sep-14'
from branch b
where b.BID = 601;
/

插入了1行。

我使用了与您相同的代码。只有insert语句不同。它对我来说很好。