Visualizza versione completa : [MYSQL] perkč questo codice non funziona?
Poseidon
20-10-2008, 10.32.23
Scritto in questo modo, il seguente trigger non funziona, mi da un generico errore di sintassi senza specificare cosa.
DELIMITER //
create trigger videoteca.aggiusta_quantita
after delete on noleggio
for each row
BEGIN
UPDATE dvd
IF quantita < 5 THEN SET quantita=quantita+1 where id_dvd = old.dvd_noleggiato
END IF;
END;//
DELIMITER;
Qualche idea?
L'errore che MySQL Query Browser mi restituisce č :
Nro Errore: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF quantita < 5 THEN SET quantita=quantita+1 where id_dvd = old.dvd_noleggiato
' at line 6
Alhazred
20-10-2008, 12.33.27
Manca il ; alla fine di questa riga
IF quantita < 5 THEN SET quantita=quantita+1 where id_dvd = old.dvd_noleggiato
Poseidon
20-10-2008, 16.19.55
umm corretto, ma MySQL Query Browser continua a darmi errore in linea 6.
DELIMITER //
create trigger videoteca.aggiusta_quantita
after delete on noleggio
for each row
BEGIN
UPDATE dvd
IF quantita < 5 THEN SET quantita=quantita+1 where id_dvd = old.dvd_noleggiato;
END IF;
END;//
DELIMITER;
Poseidon
23-10-2008, 11.44.11
Cambiando trigger, ma rimanendo in tema:
DELIMITER $$
create trigger consentiNoleggioSe
before insert on noleggio
for each row
BEGIN
IF dvd.quantita in (select quantita from dvd join noleggio
on dvd.id_dvd = new.dvd_noleggiato
where quantita between 1 and 5)
THEN insert into noleggio(id_nol, id_cliente, dvd_noleggiato, inizio, DataRestEff, prezzo)
values(new.id_nol, new.id_cliente, new.dvd_noleggiato, new.inizio, new.DataRestEff, new.prezzo);
END IF;
END; $$
DELIMITER;
L'idea č di consentire un noleggio solo se la quantitŕ copie di dvd disponibili relativi a 1 particolare dvd, caratterizzato da un suo id, č maggiore di 0 e minore di 5.
Il codice mi viene accettato da MySQL Query Browser, ma se tento poi di inserire un noleggio nella tabellanoleggi mi dice:
Unknown column 'quantita' in IN/ALL/ANY subquery ERRORE N° 1054
E non capisco perkč, dato che "quantita" č effettivamente un attributo della tabella DVD.
Poseidon
24-10-2008, 08.16.50
nessuno che mi sappia aiutare?
Poseidon
24-10-2008, 10.30.28
ho provato anche cosě, ma non funziona. Vi prego ne sto uscendo pazzo :( ...
DELIMITER //
create trigger consentiNol
before insert on noleggio
for each row
BEGIN
if dvd.quantita in (select distinct quantita
from dvd join noleggio
on dvd.id_dvd = dvd_noleggiato
where quantita between 1 and 5)
then insert into noleggio(id_nol, tipo, id_cliente, dvd_noleggiato, inizio, DataRestEff, prezzo)
values(new.id_nol, new.tipo, new.id_cliente, new.dvd_noleggiato, new.inizio, new.DataRestEff, new.prezzo);
end if;
END//
DELIMITER;
---> errore: Unknown table 'dvd' in IN/ALL/ANY subquery
>.< MySQL non riconosce le clausole check implementate nelle tabelle, ma nemmeno coi trigger si puň rendere tale vincolo? come diamine faccio? :( sn disperato: "un noleggio non puň essere inserito se la quantitŕ di copie dvd diponibili č = 0"
CREATE TABLE `videoteca`.`dvd` (
`id_dvd` int(10) NOT NULL auto_increment,
`data_fabb` date NOT NULL,
`id_film` int(10) NOT NULL,
`quantita` tinyint(1) default '5',
PRIMARY KEY (`id_dvd`),
UNIQUE KEY `FK_dvd_1` USING BTREE (`id_film`),
CONSTRAINT `FK_dvd_1` FOREIGN KEY (`id_film`) REFERENCES `film` (`id_film`)
CONSTRAINT `chk_quantita` CHECK (quantita between 0 and 5); **** viene ignorata da MySQL ****
);
CREATE TABLE `videoteca`.`film` (
`id_film` int(10) NOT NULL auto_increment,
`titolo` varchar(50) default NULL,
`anno` year(4) default NULL,
`regista` int(11) default NULL,
PRIMARY KEY (`id_film`),
KEY `FK_film_1` (`regista`),
CONSTRAINT `FK_film_1` FOREIGN KEY (`regista`) REFERENCES `attoreregista` (`id_AttReg`)
);
xaras2
03-11-2008, 02.44.12
Prova
IF quantita < '5' THEN SET quantita = quantita + 1 where id_dvd = old.dvd_noleggiato
Credi posso andare x cio che devi fare tu?
IF ((quantita < 5) and (id_dvd = old.dvd_noleggiato)) THEN SET quantita=quantita+1
Magari funzia
vBulletin® v3.8.6, Copyright ©2000-2025, Jelsoft Enterprises Ltd.