Компьютерный практикум по дисциплине «Базы данных» КоП1 SET SERVEROUTPUT ON --Лабораторная работа №1 №1 select MIN(salary) from employees; №2 DECLARE emp_lname employees.last_name%TYPE; emp_sal employees.salary%TYPE; BEGIN select last_name, salary into emp_lname, emp_sal from employees, departments where departments.department_name = 'IT' and departments.manager_id = employees.employee_id; DBMS_OUTPUT.PUT_LINE(‘Начальник отдела IT'); DBMS_OUTPUT.PUT_LINE('Фамилия ' || emp_lname); DBMS_OUTPUT.PUT_LINE('Зарплата: ' || emp_sal); END; №3 variable emp_lname VARCHAR2(20) variable emp_sal NUMBER BEGIN select last_name, salary into :emp_lname, :emp_sal from employees, departments where departments.department_name = 'IT' and departments.manager_id = employees.employee_id; DBMS_OUTPUT.PUT_LINE('Начальник отдела IT'); DBMS_OUTPUT.PUT_LINE('Фамилия: ' || :emp_lname); DBMS_OUTPUT.PUT_LINE('Зарплата: ' || :emp_sal); END; print emp_lname print emp_sal №4 DECLARE sal_emp employees.salary%type; begin select MIN(salary) into sal_emp from employees where job_id = 'FI_ACCOUNT'; DBMS_OUTPUT.PUT_LINE('Найден сотрудник отдела FI_ACCOUNT'); DBMS_OUTPUT.PUT_LINE('его зарплата: ' || sal_emp); sal_emp := sal_emp + (sal_emp*0.1); DBMS_OUTPUT.PUT_LINE('его новая зарплата: ' || sal_emp); end; №5 declare v_job employees.job_id%type := 'IT_PROG'; v_start_salary employees.salary%type := 4400; v_dept employees.department_id%type; begin select distinct department_id into v_dept from employees where job_id = v_job; INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, MANAGER_ID, DEPARTMENT_ID) VALUES (1000, 'Daniil', 'Shvagirev', SomeEmail@gmail.com', '71234567890', '11.03.20', v_job, v_start_salary+(v_start_salary*0.5), 103, v_dept); INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, MANAGER_ID, DEPARTMENT_ID) VALUES (993, 'SomeName', 'Surname', 'SomeEmail1@main.com', '71231231212', '12.05.20', v_job, v_start_salary + (v_start_salary*0.25), 103, v_dept); INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, MANAGER_ID, DEPARTMENT_ID) VALUES (986, 'Tester', 'Testerov', 'tstmail@mail.com', '79085436767', '12.03.20', v_job, v_start_salary + (v_start_salary*0.15), 103, v_dept); end; select * from employees; КоП2 SET SERVEROUTPUT ON --Лабораторная работа 2 №1 CREATE table Emp_Rate ( emp_id number, salary number, rate VARCHAR(50) ); INSERT into emp_rate (emp_id, salary) select employee_id, salary FROM employees emp, departments dep where emp.department_id = dep.department_id and dep.department_name = 'IT'; №2 DECLARE TYPE list_of_salaries IS TABLE OF Emp_Rate.salary%type; emp_salary list_of_salaries; TYPE list_of_id IS TABLE OF Emp_Rate.emp_id%type; e_id list_of_id; i number := 1; BEGIN select salary, emp_id bulk collect into emp_salary, e_id from emp_rate; loop update emp_rate set rate = rpad('*', floor(emp_salary(i)/1000), '*') where TO_NUMBER(emp_rate.emp_id) = e_id(i); DBMS_OUTPUT.PUT_LINE (rpad('*', floor(emp_salary(i)/1000), '*')); i:=i+1; j:=j+1; exit when i > emp_salary.count; end loop; END; select * from emp_rate; №3.1 DECLARE TYPE list_of_salaries IS TABLE OF Emp_Rate.salary%type; emp_salary list_of_salaries; TYPE list_of_id IS TABLE OF Emp_Rate.emp_id%type; e_id list_of_id; i number := 1; BEGIN select salary, emp_id bulk collect into emp_salary, e_id from emp_rate; for i in 1..emp_salary.count loop update emp_rate set rate = rpad('*', floor(emp_salary(i)/1000), '*') where TO_NUMBER(emp_rate.emp_id) = e_id(i); DBMS_OUTPUT.PUT_LINE (rpad('*', floor(emp_salary(i)/1000), '*')); j:=j+1; end loop; END; №3.2 DECLARE TYPE list_of_salaries IS TABLE OF Emp_Rate.salary%type; emp_salary list_of_salaries; TYPE list_of_id IS TABLE OF Emp_Rate.emp_id%type; e_id list_of_id; i number := 1; BEGIN select salary, emp_id bulk collect into emp_salary, e_id from emp_rate; while i < emp_salary.count loop update emp_rate set rate = rpad('*', floor(emp_salary(i)/1000), '*') where TO_NUMBER(emp_rate.emp_id) = e_id(i); DBMS_OUTPUT.PUT_LINE (rpad('*', floor(emp_salary(i)/1000), '*')); i:=i+1; end loop; END; select * from emp_rate; №4 DROP TABLE Emp_Rate; №5 select d.department_name, l.country_id, l.city,l.postal_code, l.street_address from departments d, locations l where d.location_id = l.location_id; DECLARE TYPE list_of_dep IS TABLE OF departments.department_name%type; type list_of_country IS TABLE OF locations.country_id%type; type list_of_city IS TABLE OF locations.city%type; type list_of_state IS TABLE OF locations.STATE_PROVINCE%type; type list_of_post IS TABLE OF locations.postal_code%type; type list_of_addr IS TABLE OF locations.street_address%type; dep list_of_dep; country list_of_country; city list_of_city; state_pr list_of_state; post list_of_post; addr list_of_addr; begin select d.department_name, l.country_id, l.city, l.STATE_PROVINCE, l.postal_code, l.street_address bulk collect into dep, country, city, state_pr, post, addr from departments d, locations l where d.location_id = l.location_id; for i in 1..dep.count loop IF country(i) = 'US' THEN DBMS_OUTPUT.PUT_LINE (i || ' ' || dep(i) || ', ' || country(i) || ', ' || city(i) || ', ' || state_pr(i)); ELSIF country(i) = 'DE' or country(i) = 'UK' or country(i) = 'NL' THEN DBMS_OUTPUT.PUT_LINE (i || ' ' || dep(i) || ', ' || post(i) || ', ' || city(i) || ', ' || addr(i)); ELSE DBMS_OUTPUT.PUT_LINE (i || ' ' || dep(i) || ', ' || post(i) || ', ' || city(i) || ' далекодалеко'); END IF; end loop; end; №6 DECLARE TYPE list_of_surname IS TABLE OF employees.last_name%type; emp_surname list_of_surname; i number := 1; begin select last_name bulk collect into emp_surname from employees where last_name like '%n%' and commission_pct > 0.2; dbms_output.put_line('Cписок работников, у которых фамилия содержит букву ‘n’ и комиссионные больше 20%:'); for i in 1..emp_surname.count loop dbms_output.put_line(emp_surname(i)); end loop; end; КоП3 set serveroutput ON --Лабораторная работа #3 SELECT * FROM jobs №1 create or replace procedure ADD_JOB(new_jobid jobs.job_id%type, new_jobtitle jobs.job_title%type) as begin insert into jobs(job_id, job_title) values (new_jobid, new_jobtitle); end ADD_JOB; №2 EXECUTE ADD_JOB('IT_WEB', 'Web Master') №3 EXECUTE ADD_JOB('ST_MAN', 'Stock Manager') №4 CREATE table EMP ( EMPNO number, ENAME VARCHAR(20), JOB_TITLE VARCHAR(20), MGR NUMBER, HIREDATE DATE, SAL number, COMM NUMBER, DEPTNO NUMBER ); №5 create or replace procedure ADD_EMP(p_empno emp.empno%type default null, p_ename emp.ename%TYPE default null, p_job_title emp.job_title%TYPE := 'SALESMAN', p_mgr emp.mgr%type default null, p_hiredate emp.hiredate%TYPE default sysdate, p_sal emp.sal%TYPE DEFAULT 1500, p_comm emp.comm%TYPE default null, p_deptno emp.deptno%type DEFAULT 30) as BEGIN INSERT INTO EMP VALUES (p_empno, p_ename, p_job_title, p_mgr, p_hiredate, p_sal, p_comm, p_deptno); END ADD_EMP; №6 execute ADD_EMP('1000', 'Daria'); №7 select * from emp КоП4 SET SERVEROUTPUT ON --Лабораторная работа #4 №1 create or replace function F_TAX(salary employees.salary%type) return employees.salary%type is sal employees.salary%type; begin sal := salary*0.13; return sal; end; select last_name, salary from employees select last_name ‘фамилия’, F_TAX(salary) налог from employees №2 create or replace function F_COMMISION(emp_id employees.employee_id%TYPE) RETURN employees.commission_pct%TYPE IS commission employees.commission_pct%TYPE; BEGIN SELECT commission_pct INTO commission FROM employees WHERE employee_id = emp_id; if commission is null then return 0; end if; RETURN commission; END F_COMMISION; select last_name, CONCAT(F_COMMISION(employee_id)*100, '%') comm from employees №3 create or replace function F_TAKE_COM(emp_id employees.employee_id%TYPE) return boolean is begin if F_COMMISION(emp_id) = 0 then return false; else return true; end if; end F_TAKE_COM; №4 declare TYPE list_of_id IS TABLE OF employees.employee_id%type; emp_id list_of_id; nalog number:=0; begin select employee_id bulk collect into emp_id from employees; for i in 1..emp_id.count loop if F_TAKE_COM(emp_id(i)) = true then nalog := nalog + F_TAX(emp_id(i)); end if; end loop; DBMS_OUTPUT.PUT_LINE('общая сумма налога равна ' || nalog); end; КоП5 SET SERVEROUTPUT ON --Лабораторная работа №5 №1 CREATE OR REPLACE TRIGGER trg_check_sal AFTER INSERT OR UPDATE OF salary ON employees FOR EACH ROW DECLARE TYPE lst_job_id IS TABLE OF jobs.job_id%TYPE; TYPE lst_job_sal_min IS TABLE OF jobs.min_salary%TYPE; TYPE lst_job_sal_max IS TABLE OF jobs.max_salary%TYPE; job_id lst_job_id; max_salary lst_job_sal_max; min_salary lst_job_sal_min; BEGIN SELECT job_id, max_salary, min_salary BULK COLLECT INTO job_id, max_salary, min_salary FROM jobs; FOR i IN 1..job_id.count LOOP IF ( :new.job_id = job_id(i) ) THEN IF ( ( :new.salary > max_salary(i) ) OR ( :new.salary < min_salary(i) ) ) THEN raise_application_error(-20100, 'Зарплата выходит за пределы допустимого диапазона'); ELSE dbms_output.put_line('Новая зп - ' || :new.salary); END IF; END IF; END LOOP; END; №2 SAVEPOINT old_database; №3 ALTER TRIGGER trg_check_sal DISABLE; ALTER TRIGGER trg_check_sal ENABLE; DECLARE TYPE lst_names IS TABLE OF employees.last_name%TYPE; TYPE lst_salaries IS TABLE OF employees.salary%TYPE; TYPE lst_jobs IS TABLE OF employees.job_id%TYPE; last_names lst_names; salaries lst_salaries; max_salary lst_salaries; jobs_id lst_jobs; mx_jobs_id lst_jobs; BEGIN SELECT last_name, job_id, salary BULK COLLECT INTO last_names, jobs_id, salaries FROM employees WHERE last_name LIKE 'L%'; SELECT job_id, MAX(salary) BULK COLLECT INTO mx_jobs_id, max_salary FROM employees WHERE last_name LIKE 'L%' GROUP BY job_id; dbms_output.put_line('Список людей с максимальной зарплатой по должностям : '); FOR i IN 1..last_names.count LOOP FOR j IN 1..mx_jobs_id.count LOOP IF ( jobs_id(i) = mx_jobs_id(j) AND max_salary(j) = salaries(i) ) THEN dbms_output.put_line(last_names(i) || ' ' || jobs_id(i) || ' ' || salaries(i)); END IF; END LOOP; END LOOP; END; №4 DECLARE TYPE lst_names IS TABLE OF employees.last_name%TYPE; TYPE lst_salaries IS TABLE OF employees.salary%TYPE; TYPE lst_jobs IS TABLE OF employees.job_id%TYPE; last_names lst_names; salaries lst_salaries; max_salary lst_salaries; jobs_id lst_jobs; mx_jobs_id lst_jobs; BEGIN SELECT last_name, job_id, salary BULK COLLECT INTO last_names, jobs_id, salaries FROM employees WHERE last_name LIKE 'L%'; SELECT job_id, MAX(salary) BULK COLLECT INTO mx_jobs_id, max_salary FROM employees WHERE last_name LIKE 'L%' GROUP BY job_id; dbms_output.put_line('Список людей с максимальной зарплатой по должностям:’ ); FOR i IN 1..last_names.count LOOP FOR j IN 1..mx_jobs_id.count LOOP IF ( jobs_id(i) = mx_jobs_id(j) AND max_salary(j) = salaries(i) ) THEN UPDATE employees SET salary = salaries(i) + salaries(i) * 0.5 WHERE last_name = last_names(i); dbms_output.put_line(last_names(i) || ' ' || jobs_id(i) || ' ' || salaries(i)); END IF; END LOOP; END LOOP; END; №5 ROLLBACK TO old_database; №6 CREATE VIEW dept_loc_view AS SELECT d.department_name, c.country_name, l.city, l.street_address FROM departments d, countries c, locations l WHERE d.location_id = l.location_id AND c.country_id = l.country_id; SELECT * FROM dept_loc_view №7 CREATE OR REPLACE TRIGGER DEPT_LOC_VIEW_TRIGGER for insert or update ON DEPT_LOC_VIEW compound trigger dep_id number; local_id number; country_id varchar(40) := upper(substr(:new.country_name, 1, 2)); instead of each row is BEGIN INSERT INTO countries ( country_id, country_name ) VALUES ( country_id, :new.country_name ); local_id := dbms_random.value(100, 1000); INSERT INTO locations ( location_id, city, street_address ) VALUES ( local_id, :new.city, :new.street_address ); dep_id := dbms_random.value(100, 1000); INSERT INTO departments ( department_id, department_name, location_id ) VALUES ( dep_id, :new.department_name, local_id ); END instead of each row; end; №8 INSERT INTO dept_loc_view VALUES ( 'Support Service', 'Norway', 'Oslo', 'Oslo street, 14' ); №9 SELECT * FROM dept_loc_view; №10 SELECT * FROM departments d WHERE d.department_name = 'Support Service'; SELECT * FROM countries c WHERE c.country_name = 'Norway'; SELECT * FROM locations l WHERE l.city = 'Oslo';