1 / 2

Zadatak 2.

Zadatak 2. a) Kreirajmo tabelu TRANSAKCIJE koja će pamtiti sve transakcije obavljene na računima korisnika CREATE TABLE ` test`.`transakcije ` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, ` ovome_oduzmi ` INTEGER UNSIGNED, ` ovome_dodaj ` INTEGER UNSIGNED,

thanh
Download Presentation

Zadatak 2.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Zadatak 2. a) Kreirajmo tabelu TRANSAKCIJE koja će pamtiti sve transakcije obavljene na računima korisnika CREATE TABLE `test`.`transakcije` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `ovome_oduzmi` INTEGER UNSIGNED, `ovome_dodaj` INTEGER UNSIGNED, `iznos` DOUBLE(10,2), `opis_transakcije` VARCHAR(255), PRIMARY KEY(`id`), CONSTRAINT `FK_transakcije_oduzmi` FOREIGN KEY `FK_transakcije_oduzmi` (`ovome_oduzmi`) REFERENCES `korisnici` (`id`) ON DELETE SET NULL, CONSTRAINT `FK_transakcije_dodaj` FOREIGN KEY `FK_transakcije_dodaj` (`ovome_dodaj`) REFERENCES `korisnici` (`id`) ON DELETE SET NULL ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT = 'Lookup tabelasatransakcijama'; b) Kreirati proceduru kojom se vrši prebacivanje novca sa računa jednog korisnika na račun drugog korisnika.

  2. DELIMITER $$ DROP PROCEDURE IF EXISTS `test`.`obavi_transakciju` $$ CREATE PROCEDURE `test`.`obavi_transakciju`( IN p_oduzmi_id INT, IN p_dodaj_id INT, IN p_iznos DOUBLE, IN p_opis VARCHAR(255), OUT p_poruka VARCHAR(255)) BEGIN DECLARE v_trenutno_stanje DOUBLE(10, 2) DEFAULT 0; BEGIN DECLARE exit HANDLER FOR NOT FOUND SET p_poruka = 'Korisniknijepronadjen!'; DECLARE exit HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; SET p_poruka = 'Greska'; END; -- oznacipocetaktransakcije START TRANSACTION; -- proveridalikorisnik, kom se skidasaracuna, postojiidali -- imadovoljnonovcada mu se zadaniiznosmozeskinuti: SELECT iznos INTO v_trenutno_stanje FROM korisnici WHERE id = p_oduzmi_id; -- akoima, ondaobavitransakciju: IF v_trenutno_stanje > p_iznos THEN UPDATE korisnici SET iznos = iznos - p_iznos WHERE id = p_oduzmi_id; UPDATE korisnici SET iznos = iznos + p_iznos WHERE id = p_dodaj_id; -- sacuvajinformacije o obavljenojtransakciji: INSERT INTO transakcije (id, ovome_oduzmi, ovome_dodaj, iznos, opis_transakcije) VALUES (NULL, p_oduzmi_id, p_dodaj_id, p_iznos, p_opis); ELSE SET p_poruka = 'Korisniknemadovoljnonovca'; END IF; END; -- potvrdiunosioznacikrajtransakcije -- iliponistiunos IF p_poruka != '' THEN ROLLBACK; ELSE COMMIT; SET p_poruka = 'SUCCESS! Transakcija je uspesnoobavljena'; END IF; SELECT p_poruka; END $$ DELIMITER ;

More Related