Download SGBD Project

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Bucharest Academy of Economic Studies
SGBD Project
Market Management
Guglea Octavian CSIE, Series G, group 1066
1. Description of the project
My database is consisting of 5 tables with relations between them. This database
helps
managing easily a market.
The 5 tables consist of :
EMPLOYEES2: EMPLOYEE_ID, EMPLOYEE_NAME, PHONE, EMAIL,
DATE_OF_BIRTH;
CLIENTS2: CLIENT_ID, CLIENT_NAME, CLIENT_EMAIL,
CLIENT_PHONE_NO,
CLIENT_DATE_OF_BIRTH;
ORDERS2:
ORDER_ID,
ORDER_DATE,
PRODUCT_NAME,
PRODUCT_PRICE, PRODUCT_QTY,
CLIENT_ID, EMPLOYEE_ID;
JOBS2: JOB_ID, JOB_NAME, JOB_SALARY, JOB_COMMISSIONS;
DEPARTMENTS2:
DEPARTMENT_ID,
DEPARTMENT_NAME,
D_EMPLOYEE_ID,
DEPARTMENT_FLOOR, JOB_ID;
One department can have many jobs, but one job has only one department.
One department can have many employees, but one employee has only one
department.
One employee can have many orders, but one order has only employee.
One client can have many orders, but one order has only client.
Database schema
DDL commands
A. Creating the tables and constraints
CREATE TABLE EMPLOYEES2 (
EMPLOYEE_ID VARCHAR(3) NOT NULL CONSTRAINT PK_EMPLOYEE_ID PRIMARY
KEY,
EMPLOYEE_NAME VARCHAR(20),
PHONE CHAR(10),
EMAIL VARCHAR(30),
DATE_OF_BIRTH VARCHAR(10)
);
CREATE TABLE CLIENTS2 (
CLIENT_ID VARCHAR(3) NOT NULL CONSTRAINT PK_CLIENTS_ID PRIMARY KEY,
CLIENT_NAME VARCHAR(20),
CLIENT_EMAIL VARCHAR(30),
CLIENT_PHONE_NO CHAR(10),
CLIENT_DATE_OF_BIRTH VARCHAR(10)
);
CREATE TABLE ORDERS2 (
ORDER_ID VARCHAR(3) NOT NULL CONSTRAINT PK_ORDERS_ID PRIMARY KEY,
ORDER_DATE VARCHAR(10),
PRODUCT_NAME VARCHAR(10),
PRODUCT_PRICE CHAR(10),
PRODUCT_QTY CHAR(10),
CLIENT_ID VARCHAR(3),
EMPLOYEE_ID VARCHAR(3)
);
CREATE TABLE JOBS2 (
JOB_ID VARCHAR(3) NOT NULL CONSTRAINT PK_JOBS_ID PRIMARY KEY,
JOB_NAME VARCHAR(10),
JOB_SALARY CHAR(10),
JOB_COMMISSIONS CHAR(10)
);
CREATE TABLE DEPARTMENTS2 (
DEPARTMENT_ID VARCHAR(3) NOT NULL CONSTRAINT PK_DEPARTMENT_ID
PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(10),
D_EMPLOYEE_ID VARCHAR(3) NOT NULL,
DEPARTMENT_FLOOR CHAR(1),
JOB_ID VARCHAR(3)
);
B. Altering the tables
1. Add a foreign key in table ODRERS2 to link it to EMPLOYEES2.
ALTER TABLE "ORDERS2" ADD CONSTRAINT "orders_employees_fk" FOREIGN KEY
("EMPLOYEE_ID") REFERENCES "EMPLOYEES2" ("EMPLOYEE_ID") ENABLE;
2. Add a foreign key in table DEPARTMENTS2 to link it to JOBS2.
ALTER TABLE "DEPARTMENTS2" ADD CONSTRAINT "job_department_fk" FOREIGN
KEY ("JOB_ID") REFERENCES "JOBS2" ("JOB_ID") ENABLE;
3.Add a foreign key in table DEPARTMENTS2 to link it to EMPLOYEES2.
ALTER TABLE "DEPARTMENTS2" ADD CONSTRAINT "employee_department_fk"
FOREIGN KEY ("D_EMPLOYEE_ID ") REFERENCES "EMPLOYEES2"
("EMPLOYEE_ID") ENABLE;
4.Add a foreign key in table ORDERS2 to link it to CLIENTS2.
ALTER TABLE "ORDERS2" ADD CONSTRAINT "orders_clients_fk" FOREIGN KEY
("CLIENT_ID") REFERENCES "CLIENTS2" ("CLIENT_ID") ENABLE;
DML commands
--Select the employee with the id=010
SET SERVEROUTPUT ON
DECLARE
v_employee_name EMPLOYEES2.EMPLOYEE_NAME%type;
BEGIN
select EMPLOYEE_NAME
into v_employee_name
from EMPLOYEES2
where EMPLOYEE_ID='010';
dbms_output.put_line('the employee with the id 010 is '||v_employee_name);
END;
--update the salaries for certain jobs
BEGIN
update JOBS2
set JOB_SALARY=JOB_SALARY+200
where JOB_NAME = 'Salesman';
update JOBS2
set JOB_SALARY=JOB_SALARY+100
where JOB_NAME = 'Porter';
update JOBS2
set JOB_SALARY=JOB_SALARY-250
where JOB_NAME = 'Courier';
update JOBS2
set JOB_SALARY=JOB_SALARY+500
where JOB_NAME = 'Cleaner';
END;
/
select JOB_NAME, JOB_SALARY from JOBS2;
Structures
--show the nr of orders of each client with the id 001…006, but it stops when the client have no
orders.
set serveroutput on
DECLARE
v_nr number;
v_nume CLIENTS2.CLIENT_NAME%type;
v_id CLIENTS2.CLIENT_ID%type;
BEGIN
for v_id in 001..006 loop
v_nr:=0;
SELECT count(c.ORDER_ID) into v_nr from ORDERS2 c, CLIENTS2 a
where c.CLIENT_ID=a.CLIENT_ID and a.CLIENT_ID=v_id;
dbms_output.put_line('Clientrul cu ID: '||v_id||' are: '||v_nr||' comenzi');
exit when v_nr=0;
end loop;
end;
/
Exceptions
Implicit
--show the id of the employees working in sales departament
set serveroutput on
DECLARE
v_id DEPARTMENTS2.D_EMPLOYEE_ID%type;
BEGIN
SELECT D_EMPLOYEE_ID into v_id from DEPARTMENTS2
where DEPARTMENT_NAME='SALES';
dbms_output.put_line(v_id);
exception
when no_data_found then
dbms_output.put_line('there are no employees working in sales department.');
end;
/
--show the job id of the jobs called Salesman
set serveroutput on
DECLARE
v_id JOBS2.JOB_ID%type;
BEGIN
SELECT JOB_ID into v_id from JOBS2
where JOB_NAME='Salesman';
dbms_output.put_line(v_id);
exception
when TOO_MANY_ROWS then
dbms_output.put_line('too many rows!');
end;
/
EXPLICIT
--Count the employees and show an error if the nr of employees its lower than 5.
set serveroutput on
DECLARE
nr number (2);
BEGIN
SELECT count (*) into nr from EMPLOYEES2;
if nr < 5 then
raise_application_error (-20011, 'nr of employees lower than 5');
else
dbms_output.put_line('there are '||nr||' employees');
end if;
end;
set serveroutput on
DECLARE
nr number (2);
data_not_found exception;
BEGIN
SELECT count (*) into nr from JOBS2
where JOB_NAME in ('Salesman','Porter');
if nr < 1 then
dbms_output.put_line('There are no salesman and porter jobs');
else
raise data_not_found;
end if;
exception
when data_not_found then
dbms_output.put_line ('There are salesman and porter jobs');
end;
/
Managing cursors
Implicit
-- show the number of employees born in 1999
set serveroutput on
DECLARE
v_nr EMPLOYEES2.EMPLOYEE_ID%type;
BEGIN
Select EMPLOYEE_ID into v_nr FROM EMPLOYEES2
WHERE DATE_OF_BIRTH like ('1999%');
v_nr:=SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE (v_nr || ' number of employees born in 1999');
END;
/
Explicit
set serveroutput on
declare
cursor c_department is select DEPARTMENT_ID, DEPARTMENT_NAME,
DEPARTMENT_FLOOR from DEPARTMENTS2
where DEPARTMENT_FLOOR='2'
BEGIN
dbms_output.put_line ('departments from the second flor');
for i in c_department loop
dbms_output.put_line ('Furnizorul '||i.DEPARTMENT_NAME||' este din: '||
i.DEPARTMENT_ID);
end loop;
end;
/
Functions, procedures, packages
Function
--Calculate the revenue from the orders
set serveroutput on
create or replace function revenue
s number (5.2);
return number;
is
begin
select * from ORDERS2;
while sql%found then
s=s+(PRODUCT_PRICE * PRODUCT_QTY);
exit when sql%notfound;
end loop;
dbms_output.put_line('the revenue is '||s);
exit;
/
PROCEDURE
--write a procedure that will raise the sallary for a job with a certain procent;
set serveroutput on
create or replace procedure raise_salary
(jid in JOBS2.JOB_ID&type, procent in number)
is
v_sallary JOBS2.JOB_SALARY%type;
inexistent_job exception;
BEGIN
update JOBS2
set JOB_SALARY = JOB_SALARY * (1+procent/100)
where JOB_ID=jid
if sql%notfound then
raise inexistent_job;
end if;
dbms_output.put_line('have been changed' ||sql%rowcount||' sallaries');
exception
when inexistent_job then
dbms_output.put_line('this job doesnt exist');
end;
/
PACKAGE
-- Create a package that will contain all the subprograms from the above
create or replace package subprograms
is
procedure raise_salary
(jid in JOBS2.JOB_ID&type, procent in number);
function revenue
s number (5.2);
return number;
end;
/
create or replace package body subprograms
is
procedure raise_salary
(jid in JOBS2.JOB_ID&type, procent in number)
is
v_sallary JOBS2.JOB_SALARY%type;
inexistent_job exception;
BEGIN
update JOBS2
set JOB_SALARY = JOB_SALARY * (1+procent/100)
where JOB_ID=jid
if sql%notfound then
raise inexistent_job;
end if;
dbms_output.put_line('have neem changed' ||sql%rowcount||' sallaries');
exception
when inexistent_job then
dbms_output.put_line('this job doesnt exist');
end;
/
function revenue
s number (5.2);
return number;
is
begin
select * from ORDERS2;
while sql%found then
s=s+(PRODUCT_PRICE * PRODUCT_QTY);
exit when sql%notfound;
end loop;
dbms_output.put_line('the revenue is '||s);
exit;
end;
/