缺少正确的parantjesis,缺少表达式,子查询错误

时间:2016-01-10 01:22:01

标签: sql oracle oracle11g triggers subquery

我的数据库适用于带有音乐CD和录像带的租赁中心。

    create table cds (
    id_cd varchar2(5) pk_cduri primary key,
    artists varchar2(100) not null,
    genre varchar2(20) not null,
    release date not null,
    constraint pk_cds primary key(id_cd)
    );


    create table replenishment(
    id_ap varchar2(5) constraint pk_aprovizionari primary key,
    id_cd varchar2(5),
    quantity_cd number(5,2) default 0,
    price_cd number(5,2) default 0,
    id_cv varchar2(5), 
    cquantity_cv number(5,2) default 0, 
    price_cv number(5,2) default 0,
    data_rep date default sysdate, 
    total_cost as (quantity_cd* price_cd+ quantity_cv* price_cv),
    constraint fk_ap_cd foreign key (id_cd) references  cds,
    constraint fk_ap_cv foreign key (id_cv) references  videotapes
    );

当客户的id_cust =' c1'借用id_cd =' cd1'借用cd,然后bor_cd = 1,当客户' c1'返回' cd1'我必须在ret_cd中插入值1。

    create table bor_ret (
    id int constraint pk_bor_ret primary key,
    id_cust varchar2(5) not null, 
    id_cd varchar2(5),
    bor_cd int default 0,
    ret_cd int default 0,
    id_cv varchar2(5),
    bor_cv int default 0,
    ret_cv int default 0,
    data date default sysdate,
    constraint fk_cd_imp foreign key (id_cd) references cds(id_cd),
    constraint fk_cv_imp foreign key (id_cv) references videotapes(id_cv),
    constraint fk_ab_imp  foreign key (id_cust) references customers,
    constraint ck_imp_cd_value_flag check (bor_cd in (1,0)), 
    constraint ck_imp_cv_value_flag check (bor_cv in (1,0)),
    constraint ck_ret_cd_value_flag check (ret_cd in (1,0)),
    constraint ck_ret_cv_value_flag check (ret_cv in (1,0))
    );

我必须创建一个触发器(我不知道如何定义它,但如果有人知道,我会问一个新问题接受它的回答)那个每次在ret_cd中插入值1后设置bor_cd = 0:

update bor_ret set bor_cd=0 where id_cust=(select id_cust from bor_ret where ret_cd=1);

然后我必须向CDS添加一个计算列,用于计算插入的最后一张cd的数量: 数量为' cd1'来自CDS的id_cd = cd1等于BOR_RET提供的数量,其中id_cd = cd1。

所以,我想运行这个命令:

alter table cds add quantity as(
 select quantity_cd from replenishment b inner join cds a on a.id_cd=b.id_cd where b.id_cd=
 (select id_cd from cds where rownum=1 order by id_cd desc) -
 (select count(id_cd) from bor_ret d inner join cds c on c.id_cd=d.id_cd where d.id_cd=
 (select id_cd from cds where rownum=1 order by id_cd desc) and d.bor_cd=1));

我收到此错误:" 缺少表达"。 如果我只运行查询:选择quantity_cd ...我收到错误: " 第2行的错误:ORA-00907:错过正确的副作用"

或者,更好的是,我如何将此列添加为:

的差异
    select quantity_cd from replenishment where id_cd=(select max(id_cd) from cds) as A

    select count(id_cd) from bor_ret where id_cd=(select max(id_cd) from cds) and bor_cd=1 as B

quantity = A-B;

此线程用于解决计算列的问题,但如果有人可以解释我如何创建触发器,我将为它打开一个新线程。

提前谢谢你们。

1 个答案:

答案 0 :(得分:0)

请注意最后一行的第二个右括号。

select quantity_cd 
from replenishment b 
inner join cds a on a.id_cd=b.id_cd 
where b.id_cd=
           (select id_cd from cds where rownum=1 order by id_cd desc) - 
           (select count(id_cd) from bor_ret d inner join cds c on c.id_cd=d.id_cd where d.id_cd= (select id_cd from cds where rownum=1 order by id_cd desc)) /* <------ ===*/
    and d.bor_cd=1)