Project Documentation.doc

(48 KB) Pobierz
Project Documentation

Maciej Basista

S3524, gr.311

 

Project Documentation

                                                                      Car Insurance

 

 

Definition of TABLES

 

 

Table customer

create table customer(

cid number(4) primary key,

cname varchar(12) not null,

csurname varchar(12) not null,

cadress varchar(12) not null,

cphone number(8) not null)

;

 

 

Table staff

create table staff(

sid number(4) primary key,

sname varchar(12),

ssurname varchar(12),

sadress varchar(12),

sposition varchar(12),

sphone number(8),

salary number(8))

;

 

 

Table insurance

create table insurance(

insid number(4) primary key,

year_from number(4) not null,

year_to number(4) not null,

cid number(4) references customer,

sid number(4) references staff,

price number(12) not null)

;

 

 

Table car

create table car(

carid number(4) primary key,

car_brand varchar(12) not null,

car_colour varchar(12) not null,

cid number(4) references customer)

;

 

 

Table recent_customers (stores all deleted customers)

 

create table recent_customers(

cid number(4) not null,

cname varchar(12) not null,

csurname varchar(12) not null,

cadress varchar(12) not null,

cphone number(8) not null)

;

 

 

Table SAL

create table SAL(

ids NUMBER(4) PRIMARY KEY ,

losal NUMBER(7,2) not null,

hisal NUMBER(7,2) not null,

position varchar2(12) not null)

;

 

 

 

________________________________________________________

TRIGGERS

 

set serveroutput on

 

 

ChangeCust is an after delete trigger. Each time we delete any customer from table ‘customer’, this customer is added to table ‘recent_customers’. This is used to store all recent customers as a history.

 

create or replace trigger ChangeCust

after delete

on customer             

for each row

begin

insert into recent_customers values(      //into this table values are inserted

:old.cid,                         //variable from table customer

:old.cname,                               //variable from table customer

:old.csurname,                               //variable from table customer

:old.cadress, ,                  //variable from table customer

:old.cphone); ,                  //variable from table customer

end;

/

 

 

AddCustomer is after insert trigger. After inserting new values to table ‘customer’ then a text appear: 'A NEW CUSTOMER ADDED', it confirms us that we have added a new customer.

 

create or replace trigger AddCustomer  //creating trigger

after insert                      //must be after insert

on customer

begin

dbms_output.put_line('A NEW CUSTOMER ADDED');  //this text will appear after adding

end;

/

 

InsuranceIndex is before insert trigger. Before we create a new insurance, the insurance ‘insid’ index is automatically created. Each time it is increased by 1 for a new insurance.

 

create or replace trigger InsuranceIndex

before insert

on insurance     //on table insurance

for each row

begin

select nvl(max(i.insid)+1,1)  //maximum insid is taken, then its increased by 1.

into :new.insid    /// for new id

from insurance i;

end;

/

 

CheckPrice is before insert or update trigger. It’s main task is to not allow enter price of insurance lower than 1.

 

create or replace trigger CheckPrice

before insert or update

on insurance         //name of table we work on

for each row

begin

if (:new.price < 1)then                                                                                      //If we try to enter price

Raise_application_error(-20000,'WRONG PRICE ENTERED!'); //lower 1, error will start.

end if;                                                                                                                             

end;

/

 

DontDelete trigger doesn’t allow to delete any insurance from the table.

create or replace trigger DontDelete

before delete        //must be before delete

on insurance         //table insurance

for each row

begin

Raise_application_error(-20001,'YOU CANT DELETE AN INSURANCE!!!'); //error raise

end;

/

 

SalUpdate – after update trigger. This trigger will inform us, each time if we decrease employee salary.

 

 

create or replace trigger SalUpdate       //creating trigger

after update                                          //because after entering new price

on staff

for each row

begin

if(:new.salary < :old.salary) then       //this checks if new salary is bigger

dbms_output.put_line('WARNING!!! NEW SALARY IS LOWER THAN OLD SALARY!!!');                                          //if not then this text will appear

end if;

end;

/

 

___________________________________________________________________________

PROCEDURES

 

 

RaiseSalary procedure. This procedure is used to raise salary of given by the user employee. While executing procedure we give as id of employee and amount of money we want to increase salary of this person.

 

 

create or replace procedure RaiseSalary(idin number, adding number) //creating of procedure

 

AS

 

empty exception;             //exception , of empty employee

change_grade exception;  //exception when salary is not in allowed interval from table sal

ile integer;                         //declaration of variable ile

salary integer (7,2);                            //declaration of variable salary

my_pos varchar2(20);              //declaration of variable my_pos

my_losal integer;                             //declaration of variable my_losal

my_hisal integer;                            //declaration of variable  my_hisal

my_sal integer;                             //declaration of variable my_sal

 

 

 

BEGIN

 

select count(*) into ile from staff where staff.sid=idin;  - // ile must be 1

if ile != 1 then raise empty;  // if not 1 then exception raise, person not exists

else

select sposition,salary into my_pos,my_sal from staff where sid=idin;

select s.losal, s.hisal into my_losal, my_hisal from sal s where s.position=my_pos;

if my_sal+adding>my_hisal or my_sal+adding<my_losal then raise change_grade;

// new salary should be beetwen losal and hisal value if nto then exception chang_grade raises

 

else

 

update staff set salary=salary+adding;   //if it’s between losal and hisal , in table staff value is        

end if;                                                                                                                         modified

end if;

exception                                                  //definition of exceptions

when empty then     

dbms_output.put_line('Not exists');  if the  //displays message that person doesn’t exists

when change_grade then

dbms_output.put_line('Changing grade not allowed!');   //if not in losal and hisal

end;   //end of procedure

/

 

___________________________________________________________________________

 

Addcustom procedure is used to adding a new customers. Filling in table customer. Also exceptions are included.

 

create or replace procedure Addcustom(           //creating a procedure, with given parameters

Cname IN customer.cname%type,                              //%cname means, that it will be the sam type as

Csurname IN customer.csurname%type,                            in ‘parent’ table

Cadress IN customer.cadress%type,

Cphone IN customer.cphone%type)

 

AS

 

thesame exception;           //declaration of exceptions

counter integer;             

Cid customer.cid%type;   //cid – customer index , the same type as in customer table

 

BEGIN

 

select count(*) into counter from customer c where c.csurname=Csurname;

if counter != 0                 //if counter != it means that user with the same surname exists

then raise thesame;                            that’s not allowed so the error is raised

end if;

select NVL(max(customer.cid)+1,1) into Cid  //adds index, increasing by 1 for new customer

from customer;

 

insert into customer                                                             //new row is filled by given values

values(Cid,Cname,Csurname,Cadress,Cphone);

exception

when thesame then

dbms_output.put_line('Warning! The same surname exists!!');  //error raises when the same            

                                                                                                                                            surname

end;   //end of procedure

/

 

 

___________________________________________________________________________

Twolowest procedure displays 2 lowest salaries of all employees. As a parameter while executing we give an number.

 

create or replace procedure twolowest(param NUMBER)   //creating a procedure

AS

cursor salaries is            //declaring a cursor, which returns all rows from staff ordered by sal

select sname,ssurname,salary from staff order by salary ;  //cursor data

my_salaries salaries%rowtype;         

counter INTEGER;

no_workers exception;

BEGIN

select count(*) into counter from staff;

if counter=0 then raise no_workers;                //checks if there are any workers

else

DBMS_output.put_line('LOWEST EARNINGS:'); 

open salaries;

fetch salaries into my_salaries;                         //takes firs row from cursor

DBMS_output.put_line('1:'||my_salaries.sname||' '||my_salaries.ssurname||' '||my_salaries.salary||'.');                                      //displays first row

fetch salaries into my_salaries;

DBMS_output.put_line('2:'||my_salaries.sname||' '||my_salaries.ssurname||' '||my_salaries.salary||'.');                                   //takes second row from cursor

...

Zgłoś jeśli naruszono regulamin