Who can help me with a trigger?

Hi, I have a little problem with my database that can be resolved, or so I think that, with a trigger. First of all, that is.


create table person
(id_utente number (5) Primary Key,)
name varchar2 (40),
sexo varchar2 (1) check (sex = ' or sex = 'F').
Morada varchar2 (60),
date of data_nascimento,
contacto number (10),
numero_BI number (9)
);

create table Serviço
(id_servico number (5) Primary Key,)
Description varchar2 (40),
Valor number (5),
date of data_inicio,
date of data_fim,
check (data_fim > = data_inicio)
);

create the table modalidade
(id_modalidade number (5) Primary Key,)
Description varchar2 (40),
Valor number (5)
);

create table quarto
(id_quarto number (5) Primary Key,)
Description varchar2 (40)
);

create the table inscricao_servico
(id_inscricao_servico number (5) Primary Key,)
id_servico number (5) references Serviço (id_servico).
id_utente number (5) references user (id_utente),.
date of data_pagamento
);

create the table inscricao_mod
(id_inscricao_mod number (5) Primary Key,)
id_modalidade number (5) references modalidade (id_modalidade).
id_utente number (5) references user (id_utente),.
date of data_inscricao,
date of data_cessacao,
check (data_cessacao > data_inscricao)
);

create table voy
(id_estadia number (5) Primary Key,)
id_utente number (5) references user (id_utente),.
id_quarto number (5) quarto references (id_quarto).
date of data_entrada,
date of data_saida,
check (data_saida > data_entrada)
);


create the table pag_mensalidade
(id_pag_mensalidade number (5) Primary Key,)
id_inscricao_mod number (5) references inscricao_mod (id_inscricao_mod).
mes_mensalidade number (6).
date of data_pagamento,
unique (id_inscricao_mod, mes_mensalidade)
);

insert into person values (1, 'Manel', am', 'Rua large', to_date('80.02.02','yy.mm.dd'), 123456789, 687654321);
insert into person values (2, "Artur", am', 'Rua pequeña', to_date ('81.03.04', 'yy.mm.dd'), 223456789, 587654321);
insert into person values (3, 'Cardoso', ', 'Rua estreita', to_date ('82.04.05', 'yy.mm.dd'), 323456789, 487654321);
insert into person values (4, 'Abílio', ', 'Rua larga', to_date ('79.07.06', 'yy.mm.dd'), 423456789, 787654321);
insert into person values (5, 'Antunes', ', 'Rua nova', to_date ('79.06.01', 'yy.mm.dd'), 423557982, 387654822);

insert into modalidade values (1, 'Total', 800);
insert into modalidade values (2, ' Diurno ate 18' 600 ');
insert into modalidade values (3, ' Diurno ate 22', 700 ');

Insert in quarto values (1, '412');
Insert in quarto values (2, '413');
Insert in quarto values (3, '414');
insert into values of quarto (4, '415');
Insert in quarto values (5, '416');

insert into Serviço values (1, ' Excursao a Fatima ", 150, to_date ('11.05.13', 'yy.mm.dd'), to_date ('11.05.13', 'yy.mm.dd'));
insert into Serviço values (2, 'Ida ao cinéma', 10, to_date ('11.08.22', 'yy.mm.dd'), to_date ('11.08.22', 'yy.mm.dd'));
insert into Serviço values (3, 'Apoio domiciliario', 100, to_date ('11.07.01', 'yy.mm.dd'), to_date ('11.07.31', 'yy.mm.dd'));

insert into voy values (1, 1, 1, to_date ('11.05.05', 'yy.mm.dd'), null);
insert into voy values (2, 2, 2, to_date ('10.01.02', 'yy.mm.dd'), null);
insert into voy values (3, 3, 3, to_date ('09.12.15', 'yy.mm.dd'), to_date ('10.02.25', 'yy.mm.dd'));

insert into inscricao_mod values (1, 1, 1, to_date ('09.12.15', 'yy.mm.dd'), null);
insert into inscricao_mod values (2, 2, 2, to_date ('11.11.11', 'yy.mm.dd'), null);
insert into inscricao_mod values (3, 1, 3, to_date ('10.04.10', 'yy.mm.dd'), to_date ('11.09.21', 'yy.mm.dd'));

insert into inscricao_servico values (1, 1, 1, null);
insert into inscricao_servico values (2, 1, 2, to_date ('11.10.01', 'yy.mm.dd'));
insert into inscricao_servico values (3, 2, 4, null);
insert into inscricao_servico values (3, 4, 5, to_date ('10.12.12', 'yy.mm.dd'));

insert into pag_mensalidade values (1, 1, 201110, to_date ('11.10.23', 'yy.mm.dd'));
insert into pag_mensalidade values (2, 2, 201111, to_date ('11.11.27', 'yy.mm.dd'));
insert into pag_mensalidade values (3, 3, 201112, NULL);
insert into pag_mensalidade values (1, 4, 201111, NULL);


drop table voy;
drop table inscricao_servico;
drop table quarto;
drop table pag_mensalidade;
drop table inscricao_mod;
drop table modalidade;
drop table Serviço;
drop table person;


OK, the problem is: the table "voy" means "stay" and "quarto" means "room". I must, before inserting it on 'Hotel', to check whether or not this room is available.

I can do this:

insert into voy values (4, 3, * 3 *, to_date ('09.12.15', 'yy.mm.dd'), to_date ('10.02.25', 'yy.mm.dd'));

But if, after this command, I do this:

insert into voy values (5, 4, * 3 *, to_date ('09.12.15', 'yy.mm.dd'), to_date ('10.02.25', 'yy.mm.dd'));

He accepts and he shouldn't, because I put 3 and 4 users in the same room at the same time.

the 3rd field is the id of the room, and the two fields are the date of entry (data_entrada) and (data_saida) release date. If a piece is linked to a specific residence in which the release date has not disappeared by, this room should not be available. In addition, the release date can be null, which means that the release date is unknown.

I hope I was clear on my question.

Thanks, Chiapa

I have not yet tested, but this should do:

CREATE OR REPLACE TRIGGER valida_estadia
BEFORE INSERT ON estadia
FOR EACH ROW
DECLARE
quarto NUMBER;

BEGIN
 SELECT COUNT(1)
 INTO quarto
 FROM estadia
 WHERE id_quarto = :new.id_quarto and
 (data_saida > :new.data_entrada or
 data_saida IS NULL);

 IF quarto > 0 THEN
   RAISE_APPLICATION_ERROR(-20001,'There is already a user in the room.');
 END IF;
END valida_estadia;
/ 

Published by: René Argento on 02/10/2012 11:43

Tags: Database

Similar Questions

Maybe you are looking for