My database is for a rental centre with music CDs and videotapes.
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
);
When a customer with id_cust='c1' borrows the cd with id_cd='cd1', then bor_cd=1 and when the customer 'c1' returns the 'cd1' I have to insert value 1 in ret_cd.
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))
);
I have to create a trigger (I don't know how to define it, but if someone knows, I will ask a new question to accept it's answer) that sets bor_cd=0 every time after I insert value 1 in ret_cd:
update bor_ret set bor_cd=0 where id_cust=(select id_cust from bor_ret where ret_cd=1);
Then I have to add a computed column to CDS that calculate the quantity of the last cd inserted: the quantity of 'cd1' from CDS which has the id_cd=cd1 is equal with the quantity supplied from BOR_RET where id_cd=cd1.
So, I want to run this command:
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));
and I get this error: " missing expression ". If I Run only the query: select quantity_cd... I get the error: " ERROR at line 2: ORA-00907: missing right paranthesis "
Or, better, how can I add this column as differences of:
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;
This thread is for solving the problem with computed column, but if someone can explain me how to create the trigger too I will open a new thread for it.
Thank you guys in advance.
Please note 2nd right parenthesis in the last but one line.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.