Script Tema 2

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

DROP DROP DROP DROP DROP DROP DROP DROP DROP DROP

TABLE TABLE TABLE TABLE TABLE TABLE TABLE TABLE TABLE TABLE

incasfact ; incasari ; liniifact ; facturi ; produse ; persclienti ; persoane ; clienti ; coduri_postale ; judete ;

CREATE TABLE judete ( jud CHAR(2) CONSTRAINT pk_judete PRIMARY KEY CONSTRAINT ck_jud CHECK (jud=LTRIM(UPPER(jud))), judet VARCHAR2(25) CONSTRAINT un_judet UNIQUE CONSTRAINT nn_judet NOT NULL CONSTRAINT ck_judet CHECK (judet=LTRIM(INITCAP(judet))), regiune VARCHAR2(15) DEFAULT 'Moldova' CONSTRAINT nn_regiune NOT NULL CONSTRAINT ck_regiune CHECK (regiune IN ('Banat', 'Transilvania', 'Dobro gea', 'Oltenia', 'Muntenia', 'Moldova')) ) ; CREATE TABLE coduri_postale ( codpost CHAR(6) CONSTRAINT pk_coduri_post PRIMARY KEY CONSTRAINT ck_codpost CHECK (codpost=LTRIM(codpost)), loc VARCHAR2(25) CONSTRAINT nn_loc NOT NULL CONSTRAINT ck_loc CHECK (loc=LTRIM(INITCAP(loc))), jud CHAR(2) DEFAULT 'IS' CONSTRAINT fk_coduri_post_jud REFERENCES judete(jud) ) ; CREATE TABLE clienti ( codcl NUMBER(6) CONSTRAINT pk_clienti PRIMARY KEY CONSTRAINT ck_codcl CHECK (codcl > 1000), dencl VARCHAR2(30) CONSTRAINT ck_dencl CHECK (SUBSTR(dencl,1,1) = UPPER(SUBSTR(dencl,1,1))) , codfiscal CHAR(9) CONSTRAINT ck_codfiscal CHECK (SUBSTR(codfiscal,1,1) = UPPER(SUBSTR(codf iscal,1,1))), adresa VARCHAR2(40) CONSTRAINT ck_adresa_clienti CHECK (SUBSTR(adresa,1,1) = UPPER(SUBSTR(ad resa,1,1))), codpost CHAR(6) CONSTRAINT fk_clienti_coduri_postale REFERENCES coduri_postale(codpost), telefon VARCHAR2(10) )

; CREATE TABLE persoane ( cnp CHAR(14) CONSTRAINT pk_persoane PRIMARY KEY CONSTRAINT ck_cnp CHECK (cnp=LTRIM(UPPER(cnp))), nume VARCHAR2(20) CONSTRAINT ck_nume CHECK (nume=LTRIM(INITCAP(nume))), prenume VARCHAR2(20) CONSTRAINT ck_prenume CHECK (prenume=LTRIM(INITCAP(prenume))), adresa VARCHAR2(40) CONSTRAINT ck_adresa_persoane CHECK (SUBSTR(adresa,1,1) = UPPER(SUBSTR(adresa,1,1))), sex CHAR(1) DEFAULT 'B' CONSTRAINT ck_sex CHECK (sex IN ('F','B')), codpost CHAR(6) CONSTRAINT fk_persoane_coduri_postale REFERENCES coduri_postale(codpost) , telacasa VARCHAR2(10), telbirou VARCHAR2(10), telmobil VARCHAR2(10), email VARCHAR2(50) ) ; CREATE TABLE persclienti ( cnp CHAR(14) CONSTRAINT fk_persclienti_persoane REFERENCES persoane(cnp), codcl NUMBER(6) CONSTRAINT fk_persclienti_clienti REFERENCES clienti(codcl), functie VARCHAR2(25) CONSTRAINT ck_functie CHECK (SUBSTR(functie,1,1) = UPPER(SUBSTR(functie,1,1))), CONSTRAINT pk_persclienti PRIMARY KEY (cnp, codcl, functie) ) ; CREATE TABLE produse ( codpr NUMBER(6) CONSTRAINT pk_produse PRIMARY KEY CONSTRAINT ck_codpr CHECK (codpr > 0), denpr VARCHAR2(30) CONSTRAINT ck_denpr CHECK (SUBSTR(denpr,1,1) = UPPER(SUBSTR(denpr,1,1))), um VARCHAR2(10), grupa VARCHAR2(15) CONSTRAINT ck_produse_grupa CHECK (SUBSTR(grupa,1,1) = UPPER(SUBSTR(grupa,1,1))), procTVA NUMBER(2,2) DEFAULT .19 ) ; CREATE TABLE facturi ( nrfact NUMBER(8) CONSTRAINT pk_facturi PRIMARY KEY, datafact DATE DEFAULT CURRENT_DATE, CONSTRAINT ck_datafact CHECK (datafact >= TO_DATE('01/08/2010','DD/MM/YYYY') AND datafact <= TO_DATE('31/12/2015','DD/MM/YYYY')), codcl NUMBER(6)

CONSTRAINT fk_facturi_clienti REFERENCES clienti(codcl) , Obs VARCHAR2(50) ) ; ----tvafact NUMBER(12,2), valfact NUMBER(12,2), valincas NUMBER(12,2), )

CREATE TABLE liniifact ( nrfact NUMBER(8) CONSTRAINT fk_liniifact_facturi REFERENCES facturi(nrfact), linie NUMBER(2) CONSTRAINT ck_linie CHECK (linie > 0), codpr NUMBER(6) CONSTRAINT fk_liniifact_produse REFERENCES produse(codpr), cantitate NUMBER(8), pretunit NUMBER (9,2), -tvalinie NUMBER(10,2), CONSTRAINT pk_liniifact PRIMARY KEY (nrfact,linie) ) ; CREATE TABLE incasari ( codinc NUMBER(8) CONSTRAINT pk_incasari PRIMARY KEY, datainc DATE DEFAULT SYSDATE CONSTRAINT ck_datainc CHECK (datainc >= TO_DATE('01/08/2008','DD/MM/YYYY ') AND datainc <= TO_DATE('31/12/2015','DD/MM/YYYY')), -tipinc CHAR(1) CONSTRAINT ck_tipinc CHECK (tipinc IN ('P', 'C', 'A')), coddoc CHAR(4) CONSTRAINT ck_coddoc CHECK(coddoc=UPPER(LTRIM(coddoc))), nrdoc VARCHAR2(16), datadoc DATE DEFAULT SYSDATE - 7 CONSTRAINT ck_datadoc CHECK (datadoc >= TO_DATE('01/01/2010','DD/MM/YYYY ') AND datadoc <= TO_DATE('31/12/2015','DD/MM/YYYY')) ) ; CREATE TABLE incasfact ( codinc NUMBER(8) CONSTRAINT fk_incasfact_incasari REFERENCES incasari(codinc ) , nrfact NUMBER(8) CONSTRAINT fk_incasfact_facturi REFERENCES facturi(nrfact), transa NUMBER(16,2) NOT NULL, CONSTRAINT pk_incasfact PRIMARY KEY (codinc, nrfact) ) ; ALTER TABLE produse ADD PretOrientativ NUMERIC(9,2); ALTER TABLE liniifact ADD tvalinie NUMERIC(12,2);

DELETE FROM incasfact ; DELETE FROM incasari ; DELETE FROM liniifact ;

DELETE DELETE DELETE DELETE DELETE DELETE DELETE INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT

FROM FROM FROM FROM FROM FROM FROM INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO

facturi ; produse ; persclienti ; persoane ; clienti ; coduri_postale ; judete ; judete judete judete judete judete judete judete VALUES VALUES VALUES VALUES VALUES VALUES VALUES ('IS', ('VN', ('NT', ('SV', ('VS', ('TM', ('BT', VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES 'Iasi', 'Moldova') ; 'Vrancea', 'Moldova') ; 'Neamt', 'Moldova') ; 'Suceava', 'Moldova') ; 'Vaslui', 'Moldova') ; 'Timis', 'Banat') ; 'Botosani', 'Moldova'); ('700505', ('701150', ('706500', ('705300', ('706400', ('705800', ('705550', ('701900', ('705551', ('701800', 'Iasi', 'IS') ; 'Pascani', 'IS') ; 'Vaslui', 'VS') ; 'Focsani', 'VN') ; 'Birlad', 'VS') ; 'Suceava', 'SV') ; 'Roman', 'NT') ; 'Timisoara', 'TM') ; 'Tg Neamt', 'NT') ; 'Botosani', 'BT') ;

coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale coduri_postale

INSERT INTO clienti VALUES (1001, '700505', NULL) ; INSERT INTO clienti VALUES (1002, '706500','035-222222'); INSERT INTO clienti VALUES (1003, 111111'); INSERT INTO clienti VALUES (1004, 551', NULL) ; INSERT INTO clienti VALUES (1005, lui, 2', '701150', '056-121212'); INSERT INTO clienti VALUES (1006, 111111'); INSERT INTO clienti VALUES (1007, 400', NULL) ; INSERT INTO clienti VALUES (1008, 00', '056-121212'); INSERT INTO clienti VALUES (1009, 111111'); INSERT INTO clienti VALUES (1010, 5551', NULL) ; INSERT INTO clienti VALUES (1011, '701900', '056-121212'); INSERT INTO clienti VALUES (1012, 50', NULL) ; INSERT INTO clienti VALUES (1013, 01150', '056-121212'); INSERT INTO clienti VALUES (1014, 05300', NULL) ; INSERT INTO clienti VALUES (1015, 00', '056-121212'); INSERT INTO clienti VALUES (1016, ', '701800', NULL) ; INSERT INTO clienti VALUES (1017,

'Client 1 SRL', 'R1001', 'Tranzitiei, 13 bis', 'Client 2 SRL', 'R1002', 'Prosperitatii, 22', 'Client 3 SRL', 'R1003', NULL, '701900', '056'Client 4 SRL', 'R1004', 'Pacientei, 33', '705 'Client 5 SRL', 'R1005', 'Victoria Capitalismu 'Client 6 SRL', 'R1006', NULL, '701900', '056'Client 7 SRL', 'R1007', 'Pacientei, 35', '706 'Client 8 SRL', 'R1008', 'Sperantei, 2', '7019 'Client 9 SRL', 'R1009', NULL, '701900', '056'Client 10 SRL', 'R1010', 'Liliacului, 4', '70 'Client 11 SRL', 'R1011', 'Universitatii, 22', 'Client 12 SRL', 'R1012', 'Lacului, 39', '7055 'Client 13 SRL', 'R1013', 'Revolutiei, 12', '7 'Client 14 SRL', 'R1014', 'Suferintei, 19', '7 'Client 15 SRL', 'R1015', 'Florilor, 2', '7019 'Client 16 SRL', 'R1016', 'Stefan cel Mare, 21 'Client 17 SRL', 'R1017', 'Trandafirilor, 2',

'701900', '056-121212'); INSERT INTO clienti VALUES 13', '700505', NULL) ; INSERT INTO clienti VALUES '701800','035-222222'); INSERT INTO clienti VALUES -111111'); INSERT INTO clienti VALUES 705550', NULL) ; INSERT INTO clienti VALUES 300', '056-121212'); INSERT INTO clienti VALUES -111111'); INSERT INTO clienti VALUES ', NULL) ; INSERT INTO clienti VALUES , NULL) ; INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse produse VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES

(1018, 'Client 18 SRL', 'R1018', 'Tudor Vladimirescu, (1019, 'Client 19 SRL', 'R1019', 'Prosperitatii, 2', (1020, 'Client 20 SRL', 'R1020', NULL, '701900', '056 (1021, 'Client 21 SRL', 'R1021', 'Sarbatorilor, 8', ' (1022, 'Client 22 SRL', 'R1022', 'Victoriei, 5', '705 (1023, 'Client 23 SRL', 'R1023', NULL, '701800', '056 (1024, 'Client 24 SRL', 'R1024', 'Postei, 4', '705550 (1025, 'Client 25 SRL', 'R1025', 'Pacii, 6', '705551'

(1, 'Produs 1','buc', 'Tigari', .19, 10); (2, 'Produs 2','kg', 'Bere', 0.09, 9.2) ; (3, 'Produs 3','kg', 'Bere', 0.19, 8) ; (4, 'Produs 4','l', 'Dulciuri', .09, 12); (5, 'Produs 5','buc', 'Tigari', .19, 11) ; (6, 'Produs 6','buc', 'Cafea', 0.19, 5.5) ; (7, 'Produs 7','buc', 'Lactate', .19, 10); (8, 'Produs 8','kg', 'Lactate', 0.09, 3.2) ; (9, 'Produs 9','kg', 'Tigari', 0.19, 8.5) ; (10, 'Produs 10','l', 'Dulciuri', .09, 1); (11, 'Produs 11','buc', 'Tigari', .19, 13) ; (12, 'Produs 12','buc', 'Cafea', 0.19, 3) ; (13, 'Produs 13','buc', 'Dulciuri', .19, 3.4); (14, 'Produs 14','kg', 'Bere', 0.09, 9.2) ; (15, 'Produs 15','kg', 'Lactate', 0.19, 8) ; (16, 'Produs 16','l', 'Lactate', .09, 12); (17, 'Produs 17','buc', 'Tigari', .19, 0.8) ; (18, 'Produs 18','buc', 'Lactate', 0.19, 5.5) ; (19, 'Produs 19','buc', 'Tigari', .19, 10); (20, 'Produs 20','kg', 'Bere', 0.09, 9.2) ; (21, 'Produs 21','kg', 'Bere', 0.19, 8) ; (22, 'Produs 22','l', 'Dulciuri', .09, 12); (23, 'Produs 23','buc', 'Tigari', .19, 11) ; (24, 'Produs 24','buc', 'Cafea', 0.19, 5.5) ; (25, 'Produs 25','buc', 'Lactate', 0.19, 5) ; (26, 'Produs 26','buc', 'Tigari', .19, 10); (27, 'Produs 27','kg', 'Lactate', 0.09, 24) ; (28, 'Produs 28','kg', 'Lactate', 0.19, 8) ; (29, 'Produs 29','l', 'Dulciuri', .09, 12); (30, 'Produs 30','buc', 'Tigari', .19, 11) ;

CREATE OR REPLACE FUNCTION f_lucr (zicrt_ date) RETURN BOOLEAN AS v_zilucr BOOLEAN ; BEGIN if TO_char(zicrt_, 'D') in (6, 7) then

v_zilucr := false; else v_zilucr := true; end if; return v_zilucr; END ; create or replace procedure p_PopulareFacturi AS v_zicrt DATE ; v_nrfact NUMBER(8,0) ; v_codcl NUMBER(6,0) ; i NUMBER(2,0) ; v_factpezi NUMBER(2,0) ; BEGIN DELETE FROM liniifact; delete from incasfact; DELETE FROM facturi; v_zicrt := TO_DATE('01/08/2010','DD/MM/YYYY'); v_nrfact := 100001 ; WHILE v_zicrt <= TO_DATE('31/12/2015','DD/MM/YYYY') and v_nrfact <= 150001 LOOP if f_lucr(v_zicrt) THEN v_factpezi := dbms_random.value(1,80) ; for i in 1..v_factpezi loop v_codcl := dbms_random.value(1001,1025) ; INSERT INTO facturi (nrfact, datafact, codcl) VALUES (v_nrfact, v_zicrt, v_codcl ) ; v_nrfact := v_nrfact + 1 ; END LOOP; end if; v_zicrt := v_zicrt + 1 ; END LOOP ; END ;

CREATE OR REPLACE FUNCTION pret (codpr_ produse.codpr%TYPE) RETURN NUMERIC AS v_valpret NUMERIC(6,2) ; BEGIN SELECT PretOrientativ INTO v_valpret FROM produse WHERE codpr = codpr_ ; RETURN v_valpret ; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 0; WHEN OTHERS THEN RETURN NULL ; END ;

create or replace procedure p_PopulareLiniiFacturi AS r_nrfact NUMBER(8) ; r_linie NUMBER(2) ; r_codpr NUMBER(6) ; r_cantitate NUMBER(8); r_pretunit NUMBER (9,2); i NUMBER(2,0) ; j NUMBER(2,0) ; BEGIN DELETE FROM liniifact; DELETE from incasfact; r_nrfact:=100001; WHILE r_nrfact <= 150001 LOOP j:=dbms_random.value(1, 20); FOR i IN 1..j LOOP r_linie:= i; r_codpr:= dbms_random.value(1, 30) ; r_cantitate:= dbms_random.value(1, 200) ; r_pretunit:=dbms_random.VALUE(0, 0.75) * pret(r_codpr); INSERT INTO liniifact (nrfact, linie, codpr, cantitate, pretunit) VALUES (r_ nrfact, r_linie, r_codpr, r_cantitate, r_pretunit) ; END LOOP; r_nrfact:=r_nrfact+1; END LOOP ; END ;

You might also like