Sunteți pe pagina 1din 21

UNIVERSITATEA BUCURESTI

FACULTATEA DE MATEMATICA SI INFORMATICA


TEHNOLOGIA INFORMATIEI

MAGAZIN INGHETATA

Stan Antonio Dragos


Grupa 353, An III

Bucuresti, 2014

-1-

Cuprins

1. Descrierea modelului ........................................................................ 3


2. Modelul relational ............................................................................ 4
3. Schemele relationale......................................................................... 6
4. Implementarea in SQL ..................................................................... 7
5. Algebra relationala ......................................................................... 20

-2-

1. Descrierea modelui
Modelul este dezvoltat pe baza produsului principal: inghetata.
Pentru a avea o inghetata trebuie sa definim mai intai proprietatile acesteia: nume, tip,
descriere, popularitate, pret.
Fiecare inghetata este facuta din 1 sau mai multe ingrediente, are 1 sau mai multe
arome si la fabricarea ei este folosit un aparat special de facut inghetata.
Fiecare ingredient are urmatoarele proprietati: nume, unitate de masura, pret/unitate,
stoc.
Fiecare aroma are urmatoarele proprietati: nume, popularitate, descirere.
Fiecare aparat are urmatoarele proprietati: nume, data achizitiei, pret, cantitate.
Ingredientele, aromele si aparatele de facut inghetata sunt aduse de catre furnizori, din
diverse locatii, cu un anumit grad de incredere la o anumita data.
Dupa ce am definit procesul de fabricatie, urmeaza procesul de distributie, pentru care
avem nevoie de angajati.
Fiecare angajat are urmatoarele proprietati: manager(daca este cazut), nume, prenume,
numar de telefon, data angajarii, salariu, functie, locatie.
Fiecare manager este la randul sau un angajat, deci are aceleasi proprietati.
Fiecare functie are urmatoarele proprietati: nume, descriere, si salariul obisnuit.
Fiecare locatie are urmatoarele proprietati: adresa, descriere.
Pentru a putea vinde clientilor inghetatele, trebuie sa le duca in divere locuri cu
ajutorul masinilor conduse de soferi, la randul lor angajati.
Fiecare masina are urmatoarele proprietati: numar de inmatriculare, marca, data
achizitiei.
O masina poate sa mearga pe mai multe rute spre diverse locatii.
Fiecare ruta are urmatoarele proprietati: nume, descrierea traseului.
Dupa ce inghetata ajunge in aceste locatii exista alti angajati care preiau comenzi de la
niste clienti.
Fiecare client are urmatoarele proprietati: nume, locatie, aroma favorita, data nasterii.
Fiecare comanda are urmatoarele proprietati: angajatul care a preluat comanda,
clientul, locatia si data comenzii, si desigur produsele comandate, in cazul de fata
inghetata in cantitati si cu arome diferite.
Nu in ultimul rand mai sunt definiti si furnizorii acestei afaceri.
Fiecare furnizor are urmatoarele proprietati: nume, descriere.

-3-

2. Modelul relational

-4-

-5-

3. Schemele relationale
FURNIZOR(cod_furnizor#, nume, cod_locatie#, nume, grad_incredere);
LOCATIE(cod_locatie#, adresa, descriere);
APARAT(cod_aparat#, nume, data_achizitie, pret, cantitate);
FURNIZEAZA_APARAT(cod_furnizeaza_aparat#, cod_furnizor#, cod_aparat#, data,
descriere);
INGREDIENT(cod_ingredient#, nume, unitate_masura, pret_unitate, stoc);
FURNIZEAZA_INGREDIENT(cod_furnieaza_ingredient#, cod_furnizor#,
cod_ingredient#, data, descriere);
TIP_INGHETATA(cod_tip_inghetata#, nume, descriere);
INGHETATA(cod_inghetata#, nume, cod_tip_inghetata#, descriere, popularitate, pret);
INGREDIENT_INGHETATA(cod_ingredient_inghetata#, cod_inghetata#,
cod_ingredient#);
APARAT_INGHETATA(cod_aparat_inghetata#, cod_inghetata#, cod_aparat#);
AROMA(cod_aroma#, nume, popularitate, descriere);
AROMA_INGHETATA(cod_aroma_inghetata#, cod_inghetata#, cod_aroma#);
CLIENT(cod_client#, cod_aroma_favorita#, cod_locatie#, nume, data_nastere);
FUNCTIE(cod_functie#, nume, descriere, salariu_obisnuit);
ANGAJAT(cod_angajat#, cod_manager#, nume, prenume, telefon, data_angajarii,
salariu, cod_functie#, cod_locatie#);
MASINA(cod_masina#, numar_inmatriculare, marca, data_achizitie);
CONDUCE_MASINA(cod_conduce_masina#, cod_angajat#, cod_masina#);
RUTA(cod_ruta#, nume, descirere);
MASINA_RUTA(cod_masina_ruta#, cod_masina#, cod_ruta#);
COMANDA(cod_comanda#, cod_angajat#, cod_locatie#, data, cod_client#);
COMANDA_INGHETATA(cod_comanda_inghetata#, cod_inghetata#, cod_comanda# ,
cantitate);

-6-

4. Implementarea in SQL
-- Clienti
CREATE TABLE clients
(
id INTEGER NOT NULL ,
preferred_flavour_id INTEGER NOT NULL ,
location_id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) ,
birth_date DATE
)
;
ALTER TABLE clients
ADD CONSTRAINT clients_PK PRIMARY KEY ( id ) ;
ALTER TABLE clients
ADD CONSTRAINT clients__UN UNIQUE ( name , location_id ) ;

-- Comenzi
CREATE TABLE commands
(
id INTEGER NOT NULL ,
employee_id INTEGER NOT NULL ,
location_id INTEGER NOT NULL ,
"date" DATE NOT NULL ,
client_id INTEGER NOT NULL
)
;
ALTER TABLE commands
ADD CONSTRAINT commands_PK PRIMARY KEY ( id ) ;

-- Angajati
CREATE TABLE employees
(
id INTEGER NOT NULL ,
manager_id INTEGER NULL ,
first_name VARCHAR2 (64 CHAR) NOT NULL ,
last_name VARCHAR2 (64 CHAR) NOT NULL ,
phone VARCHAR2 (14 CHAR) ,
hire_date DATE NOT NULL ,
salary NUMBER (7,2) NOT NULL ,
function_id INTEGER NOT NULL ,
location_id INTEGER NOT NULL
)
;
ALTER TABLE employees
ADD CONSTRAINT employees_PK PRIMARY KEY ( id ) ;
ALTER TABLE employees
-7-

ADD CONSTRAINT employees__UN UNIQUE ( first_name , last_name ) ;

-- Angajati - masini
CREATE TABLE employees_machines
(
id INTEGER NOT NULL ,
employee_Id INTEGER NOT NULL ,
machine_id INTEGER NOT NULL
)
;
ALTER TABLE employees_machines
ADD CONSTRAINT employees_machines_PK PRIMARY KEY ( id ) ;
ALTER TABLE employees_machines
ADD CONSTRAINT employees_machines__UN UNIQUE ( employee_Id ,
machine_id ) ;

-- Arome
CREATE TABLE flavours
(
id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) NOT NULL ,
popularity NUMBER (4,2) ,
description VARCHAR2 (64 CHAR)
)
;
ALTER TABLE flavours
ADD CONSTRAINT flavours_PK PRIMARY KEY ( id ) ;
ALTER TABLE flavours
ADD CONSTRAINT flavours__UN UNIQUE ( name ) ;

-- Functii
CREATE TABLE functions
(
id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) ,
description VARCHAR2 (255 CHAR) ,
dafault_salary NUMBER (7,2)
)
;
ALTER TABLE functions
ADD CONSTRAINT functions_PK PRIMARY KEY ( id ) ;
ALTER TABLE functions
ADD CONSTRAINT functions__UN UNIQUE ( name ) ;

-- Tipuri inghetata
CREATE TABLE icecream_types

-8-

(
id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) ,
description VARCHAR2 (64 CHAR)
)
;
ALTER TABLE icecream_types
ADD CONSTRAINT icrecream_types_PK PRIMARY KEY ( id ) ;
ALTER TABLE icecream_types
ADD CONSTRAINT icrecream_types__UN UNIQUE ( name , description ) ;

-- Inghetata
CREATE TABLE icecreams
(
id INTEGER NOT NULL ,
name VARCHAR2 (32 CHAR) NOT NULL ,
icecream_type_id INTEGER NOT NULL ,
description VARCHAR2 (64 CHAR) ,
popularity NUMBER (4,2) ,
price NUMBER (5,2)
)
;
ALTER TABLE icecreams
ADD CONSTRAINT icecream_PK PRIMARY KEY ( id ) ;
ALTER TABLE icecreams
ADD CONSTRAINT icecream__UN UNIQUE ( name , icecream_type_id ,
description ) ;

-- Comenzi - inghetate
CREATE TABLE icecreams_commands
(
id INTEGER NOT NULL ,
icecream_id INTEGER NOT NULL ,
cantity INTEGER NOT NULL ,
command_id INTEGER NOT NULL
)
;
ALTER TABLE icecreams_commands
ADD CONSTRAINT icecreams_commands__UN UNIQUE ( icecream_id , cantity ,
command_id ) ;

-- Arome - inghetate
CREATE TABLE icecreams_flavours
(
id INTEGER NOT NULL ,
icecream_id INTEGER NOT NULL ,
flavour_id INTEGER NOT NULL

-9-

)
;
ALTER TABLE icecreams_flavours
ADD CONSTRAINT icecreams_flavours_PK PRIMARY KEY ( id ) ;
ALTER TABLE icecreams_flavours
ADD CONSTRAINT icecreams_flavours__UN UNIQUE ( icecream_id , flavour_id ) ;

-- Ingrediente - inghetate
CREATE TABLE icecreams_ingredients
(
id INTEGER NOT NULL ,
icecream_id INTEGER NOT NULL ,
ingredient_id INTEGER NOT NULL
)
;
ALTER TABLE icecreams_ingredients
ADD CONSTRAINT icecream_ingredients_PK PRIMARY KEY ( id ) ;

-- Aparete - inghetate
CREATE TABLE icecreams_tools
(
id INTEGER NOT NULL ,
icecream_id INTEGER NOT NULL ,
tool_id INTEGER NOT NULL
)
;
ALTER TABLE icecreams_tools
ADD CONSTRAINT icecreams_tools_PK PRIMARY KEY ( id ) ;
ALTER TABLE icecreams_tools
ADD CONSTRAINT icecreams_tools__UN UNIQUE ( icecream_id , tool_id ) ;

-- Ingrediente
CREATE TABLE ingredients
(
id INTEGER NOT NULL ,
name VARCHAR2 (32 CHAR) NOT NULL ,
unit_of_measurement VARCHAR2 (32 CHAR) NOT NULL ,
price_unit NUMBER (5,2) NOT NULL ,
stock NUMBER (5,2) NOT NULL
)
;
ALTER TABLE ingredients
ADD CONSTRAINT ingredients_PK PRIMARY KEY ( id ) ;
ALTER TABLE ingredients
ADD CONSTRAINT ingredients__UN UNIQUE ( name , unit_of_measurement ) ;

- 10 -

-- Locatii
CREATE TABLE locations
(
id INTEGER NOT NULL ,
address VARCHAR2 (64 CHAR) NOT NULL ,
description VARCHAR2 (64 CHAR)
)
;
ALTER TABLE locations
ADD CONSTRAINT locations_PK PRIMARY KEY ( id ) ;
ALTER TABLE locations
ADD CONSTRAINT locations__UN UNIQUE ( address , description ) ;

-- Masini
CREATE TABLE machines
(
id INTEGER NOT NULL ,
plate_number VARCHAR2 (10 CHAR) NOT NULL ,
name VARCHAR2 (32 CHAR) NOT NULL ,
purchase_date DATE
)
;
ALTER TABLE machines
ADD CONSTRAINT machines_PK PRIMARY KEY ( id ) ;
ALTER TABLE machines
ADD CONSTRAINT machines__UN UNIQUE ( plate_number ) ;

-- Rute - masini
CREATE TABLE machines_routes
(
id INTEGER NOT NULL ,
machine_id INTEGER NOT NULL ,
route_id INTEGER NOT NULL
)
;
ALTER TABLE machines_routes
ADD CONSTRAINT machines_routes_PK PRIMARY KEY ( id ) ;
ALTER TABLE machines_routes
ADD CONSTRAINT machines_routes__UN UNIQUE ( machine_id , route_id ) ;

-- Rute
CREATE TABLE routes
(
id INTEGER NOT NULL ,
name VARCHAR2 (128 CHAR) NOT NULL ,
description VARCHAR2 (255 CHAR)
)

- 11 -

;
ALTER TABLE routes
ADD CONSTRAINT routes_PK PRIMARY KEY ( id ) ;
ALTER TABLE routes
ADD CONSTRAINT routes__UN UNIQUE ( name ) ;

-- Sponsori
CREATE TABLE sponsors
(
id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) ,
description VARCHAR2 (255 CHAR)
)
;
ALTER TABLE sponsors
ADD CONSTRAINT sponsors_PK PRIMARY KEY ( id ) ;
ALTER TABLE sponsors
ADD CONSTRAINT sponsors__UN UNIQUE ( name ) ;

-- Furnizori
CREATE TABLE suppliers
(
id INTEGER NOT NULL ,
location_id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) NOT NULL ,
reliability INTEGER
)
;
ALTER TABLE suppliers
ADD CONSTRAINT suppliers_PK PRIMARY KEY ( id ) ;
ALTER TABLE suppliers
ADD CONSTRAINT suppliers__UN UNIQUE ( location_id , name ) ;

-- Furnizori - ingrediente
CREATE TABLE suppliers_ingredients
(
id INTEGER NOT NULL ,
supplier_id INTEGER NOT NULL ,
ingredient_id INTEGER NOT NULL ,
"date" DATE NOT NULL ,
description VARCHAR2 (64 CHAR)
)
;
ALTER TABLE suppliers_ingredients
ADD CONSTRAINT suppliers_ingredients_PK PRIMARY KEY ( id ) ;
ALTER TABLE suppliers_ingredients

- 12 -

ADD CONSTRAINT suppliers_ingredients__UN UNIQUE ( supplier_id ,


ingredient_id , "date" , description ) ;

-- Furnizori - aparate
CREATE TABLE suppliers_tools
(
id INTEGER NOT NULL ,
supplier_id INTEGER NOT NULL ,
tool_id INTEGER NOT NULL ,
"date" DATE NOT NULL ,
description VARCHAR2 (64 CHAR)
)
;
ALTER TABLE suppliers_tools
ADD CONSTRAINT suppliers_tools_PK PRIMARY KEY ( id ) ;
ALTER TABLE suppliers_tools
ADD CONSTRAINT suppliers_tools__UN UNIQUE ( supplier_id , tool_id , "date" ,
description ) ;

-- Aparate
CREATE TABLE tools
(
id INTEGER NOT NULL ,
name VARCHAR2 (64 CHAR) NOT NULL ,
purchase_date DATE ,
price NUMBER (7,2) ,
num INTEGER NOT NULL
)
;
ALTER TABLE tools
ADD CONSTRAINT tools_PK PRIMARY KEY ( id ) ;
ALTER TABLE tools
ADD CONSTRAINT tools__UN UNIQUE ( name ) ;

-- Aparate - inghetate
ALTER TABLE icecreams_tools
ADD CONSTRAINT TABLE_18_icecreams_FK FOREIGN KEY
(
icecream_id
)
REFERENCES icecreams
(
id
)
;
ALTER TABLE icecreams_tools
ADD CONSTRAINT TABLE_18_tools_FK FOREIGN KEY

- 13 -

(
tool_id
)
REFERENCES tools
(
id
)
;

-- Comenzi - inghetate
ALTER TABLE icecreams_commands
ADD CONSTRAINT TABLE_23_commands_FK FOREIGN KEY
(
command_id
)
REFERENCES commands
(
id
)
;
ALTER TABLE icecreams_commands
ADD CONSTRAINT TABLE_23_icecreams_FK FOREIGN KEY
(
icecream_id
)
REFERENCES icecreams
(
id
)
;

-- Furnizori
ALTER TABLE suppliers
ADD CONSTRAINT TABLE_2_locations_FK FOREIGN KEY
(
location_id
)
REFERENCES locations
(
id
)
;

-- Clienti
ALTER TABLE clients
ADD CONSTRAINT clients_flavours_FK FOREIGN KEY
(

- 14 -

preferred_flavour_id
)
REFERENCES flavours
(
id
)
;
ALTER TABLE clients
ADD CONSTRAINT clients_flavours_FKv1 FOREIGN KEY
(
location_id
)
REFERENCES locations
(
id
)
;

-- Comenzi
ALTER TABLE commands
ADD CONSTRAINT commands_clients_FK FOREIGN KEY
(
client_id
)
REFERENCES clients
(
id
)
;
ALTER TABLE commands
ADD CONSTRAINT commands_employees_FK FOREIGN KEY
(
employee_id
)
REFERENCES employees
(
id
)
;
ALTER TABLE commands
ADD CONSTRAINT commands_locations_FK FOREIGN KEY
(
location_id
)
REFERENCES locations
(
id
)
;

- 15 -

-- Angajati
ALTER TABLE employees
ADD CONSTRAINT employees_employees_FK FOREIGN KEY
(
manager_id
)
REFERENCES employees
(
id
)
;
ALTER TABLE employees
ADD CONSTRAINT employees_functions_FK FOREIGN KEY
(
function_id
)
REFERENCES functions
(
id
)
;
ALTER TABLE employees
ADD CONSTRAINT employees_locations_FK FOREIGN KEY
(
location_id
)
REFERENCES locations
(
id
)
;

-- Anagajati - masini
ALTER TABLE employees_machines
ADD CONSTRAINT employees_machines_FK FOREIGN KEY
(
employee_Id
)
REFERENCES employees
(
id
)
;
ALTER TABLE employees_machines
ADD CONSTRAINT employees_machines_machines_FK FOREIGN KEY
(
machine_id

- 16 -

)
REFERENCES machines
(
id
)
;

-- Inghetate
ALTER TABLE icecreams
ADD CONSTRAINT icecream_icecream_types_FK FOREIGN KEY
(
icecream_type_id
)
REFERENCES icecream_types
(
id
)
;

-- Inghetate - ingrediente
ALTER TABLE icecreams_ingredients
ADD CONSTRAINT icecream_ingredients_FK FOREIGN KEY
(
icecream_id
)
REFERENCES icecreams
(
id
)
;
ALTER TABLE icecreams_ingredients
ADD CONSTRAINT icecream_ingredients_FK2 FOREIGN KEY
(
ingredient_id
)
REFERENCES ingredients
(
id
)
;

-- Inghetate - arome
ALTER TABLE icecreams_flavours
ADD CONSTRAINT icecreams_flavours_flavours_FK FOREIGN KEY
(
flavour_id
)

- 17 -

REFERENCES flavours
(
id
)
;
ALTER TABLE icecreams_flavours
ADD CONSTRAINT icecreams_flavours_FK FOREIGN KEY
(
icecream_id
)
REFERENCES icecreams
(
id
)
;

-- Masini - rute
ALTER TABLE machines_routes
ADD CONSTRAINT machines_routes_machines_FK FOREIGN KEY
(
machine_id
)
REFERENCES machines
(
id
)
;
ALTER TABLE machines_routes
ADD CONSTRAINT machines_routes_routes_FK FOREIGN KEY
(
route_id
)
REFERENCES routes
(
id
)
;

-- Ingrediente - furnizori
ALTER TABLE suppliers_ingredients
ADD CONSTRAINT suppliers_ingredients_FK FOREIGN KEY
(
ingredient_id
)
REFERENCES ingredients
(
id
)

- 18 -

;
ALTER TABLE suppliers_ingredients
ADD CONSTRAINT suppliers_ingredients_FK2 FOREIGN KEY
(
supplier_id
)
REFERENCES suppliers
(
id
)
;

-- Aparate - furnizori
ALTER TABLE suppliers_tools
ADD CONSTRAINT suppliers_tools_suppliers_FK FOREIGN KEY
(
supplier_id
)
REFERENCES suppliers
(
id
)
;
ALTER TABLE suppliers_tools
ADD CONSTRAINT suppliers_tools_tools_FK FOREIGN KEY
(
tool_id
)
REFERENCES tools
(
id
)
;

- 19 -

5. Algebra relationala
A. Introducerea datelor (limbaj SQL):

insert into icecream_types (id, name) values (1, 'cornet');


insert into icecreams(id, name, icecream_type_id, description, popularity, price)
values(1, 'vanilla', 1, 'the best', 75, 50);
insert into locations(id, address) values(1, 'Vatra Luminoasa');
insert into functions(id, name) values(1, 'vanzator');
insert into employees(id, first_name, last_name, hire_date, salary, function_id,
location_id) values(1, 'Ovidiu', 'Rosu', current_date, 2500, 1, 1);
insert into employees(id, manager_id, first_name, last_name, hire_date, salary,
function_id, location_id)
values(2, 1, 'Vasile', 'Vasilescu', current_date+1, 1000, 1, 1);
insert into flavours(id, name, popularity) values(1, 'vanilie', 20);
insert into flavours(id, name, popularity) values(2, 'lamaie', 10);
insert into icecreams_flavours(id, icecream_id, flavour_id)
values(1, 1, 1);
insert into icecreams_flavours(id, icecream_id, flavour_id)
values(2, 1, 2);
B. Selectarea datelor(limbaj SQL):

select i.name as nume_inghetata, it.name as nume_tip_inghetata


, wm_concat(f.name) as arome
from icecreams i
left outer join icecream_types it on i.icecream_type_id = it.id
left outer join icecreams_flavours if on i.id = if.icecream_id
left outer join flavours f on f.id = if.flavour_id
where lower(i.name) like '%vanilla%'
group by i.name, it.name;
(result: vanilla cornet vanilie,lamaie)

select e.first_name || ' ' || e.last_name as nume_complet, e.salary as salariu,


f.name as functie, l.address as locatie
from employees e
left outer join functions f on e.function_id = f.id
left outer join locations l on e.location_id = l.id
where salary >= 1000;
(result: Ovidiu Rosu 2500

vanzator

Vatra Luminoasa)
- 20 -

select e1.first_name as manager, e.first_name || ' ' || e.last_name as nume_complet, f.name


as functie, l.address as locatie
from employees e
left join employees e1 on e.manager_id = e1.id
left join functions f on e.function_id = f.id
left join locations l on e.location_id = l.id
where lower(l.address) like lower('%Vatra%') and f.name like '%vanzator%' and
e.manager_id is not null;
(result: Ovidiu Vasile Vasilescu

vanzator

C. Modificare datelor(limbaj SQL):

update employees e
set e.manager_id = null
where e.manager_id = (
select e2.manager_id
from employees e2
where lower(e2.last_name) like ('%vasilescu%')
);
(result: 1 rows updated.)
update flavours f
set f.name = initcap(f.name)
where f.id in (
select if.flavour_id
from icecreams_flavours if
left outer join icecreams i on if.icecream_id = i.id
where i.name like '%vanilla%'
);
(result: 2 rows updated.)
D. Eliminarea datelor(limbaj SQL):

delete
from employees e
where e.function_id in (
select f.id
from functions f
where lower(f.name) like '%vanzato%'
);
(result: 2 rows deleted.)
delete from flavours
where not exists(
select * from icecreams_flavours
);

- 21 -

Vatra Luminoasa)

S-ar putea să vă placă și