【问题标题】:Oracle: object relational database, how to insert table values with foreign key?Oracle:对象关系数据库,如何用外键插入表值?
【发布时间】:2018-03-08 12:32:48
【问题描述】:

实际上,我有两个表 Branch 和 Account_table,我想插入带有外键 (bID) 值的 account_table 值,该外键值是分支表 (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');
/

谢谢。

【问题讨论】:

    标签: oracle11g


    【解决方案1】:
    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 行。

    我使用了与您相同的代码。只有插入语句不同。对我来说效果很好。

    【讨论】:

    • 根据您的代码执行后 SQL 错误:ORA-00913:太多值 00913. 00000 - “太多值” *原因:*操作:
    • 我只使用了你的代码。这对我来说可以。插入一行,没有任何错误。我已将整个解决方案粘贴在答案中。检查你在做什么与上面的代码不同。另外,如果它不起作用,您面临的错误是什么?
    • 我们开始吧。感谢分配。工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2018-06-10
    相关资源
    最近更新 更多