Idoc.vn Bai Tap Oracle Co Loi Giai

Embed Size (px)

Citation preview

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    1/37

    V ng Giang 2012

    1 Trng cao ng cng nghthng tin TP.HCM

    Download bi ti:http://www.mediafire.com/?3hh975tcdvici7zDownload c sdliu ti:http://www.mediafire.com/?amchtkg9ilx1wk1 Sau import dliu vo oracle.

    TRUY VN ORACLE1. Lit k tn(last_name)v lng(salary)ca nhng nhn vin clng ln hn 12000$.SELECTLAST_NAME,SALARYFROMemployees;2. Lit k tn v lng ca nhng nhn vin c lng thp hn 5000$hoc ln hn 12000$.

    SELECTFIRST_NAME,LAST_NAME,SALARYFROMEMPLOYEESWHERESALARY12000;

    3. Cho bit thng tin tn nhn vin(last_name),m cng vic(job_id),ngy thu(hire_date)ca nhng nhn vin c thu tngy20/02/1998 n ngy 1/05/1998.Thng tin c hin thtng dn theo ngy thu.

    SELECTlast_name,job_id,hire_dateFROMemployees

    WHEREhire_date BETWEEN'20/FEB/1998'AND'1/MAY/1998'ORDERBYhire_date ASC;

    4. Lit k danh sch nhn vin lm vic cho phng 20 v 50. Thng tinhin thgm:last_name,department_id ,trong tn nhn vin c sp xp theothtalphabe.

    SELECTLAST_NAME,DEPARTMENT_IDFROMEMPLOYEESWHEREDEPARTMENT_ID IN(20,50);

    5. Lit k danh sch nhn vin c thu nm 1994.

    SELECT*FROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'YY')='05';

    http://www.mediafire.com/?3hh975tcdvici7zhttp://www.mediafire.com/?amchtkg9ilx1wk1http://www.mediafire.com/?amchtkg9ilx1wk1http://www.mediafire.com/?amchtkg9ilx1wk1http://www.mediafire.com/?amchtkg9ilx1wk1http://www.mediafire.com/?3hh975tcdvici7z
  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    2/37

    V ng Giang 2012

    2 Trng cao ng cng nghthng tin TP.HCM

    6. Lit k tn nhn vin(last_name),m cng vic(job_id)ca nhngnhn vinkhng c ngi qun l.SELECTLAST_NAME,FIRST_NAME,JOB_IDFROMEMPLOYEESWHEREMANAGER_ID ISNULL;7. Cho bit thng tin tt cnhn vin c hng hoa hng(commission_pct),kt quc sp xp gim dn theo lng v hoa hng.

    SELECTFIRST_NAME,LAST_NAME,COMMISSION_PCTFROMEMPLOYEESWHERECOMMISSION_PCT ISNOTNULL;8.Lit k danh sch nhn vin m c k tth3 trong tn l a.SELECT*FROMEMPLOYEESWHEREFIRST_NAME LIKE('__a%');

    9. Lit k danh sch nhn vin m trong tn c cha mt chav mtche.

    SELECTemployee_id,first_name,last_nameFROMemployeesWHEREfirst_name LIKE('%a%e%');

    10. Cho bit tn(last_name),m cng vic(job_id),lng(salary)

    ca nhngnhn vin lm Sales representative hoc Stock clertv c mclng khc2500$,3500$,7000$.

    --CACH 1SELECTA.last_name,A.job_id,A.salaryFROMEMPLOYEES A INNERJOINJOBS bONA.job_id =b.job_idWHERE job_title IN('Sales Representative','Stock Clerk')ANDsalary NOTIN(2500,3500,7000);

    --cach 2SELECTA.last_name,A.JOB_ID,A.SALARYFROMEMPLOYEES A,JOBS BWHEREB.JOB_TITLE IN('Sales Representative','Stick cleark')ANDA.SALARY NOTIN(2500,3500,7000);--CACH 3SELECTA.LAST_NAME,A.JOB_ID,A.SALARYFROMEMPLOYEES A

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    3/37

    V ng Giang 2012

    3 Trng cao ng cng nghthng tin TP.HCM

    WHEREA.JOB_ID IN(SELECTB.JOB_IDFROMJOBS BWHEREJOB_TITLE IN('Sales Representative','Stick

    cleark'))ANDA.SALARY NOTIN(2500,3500,7000);--CACH 3.1SELECTA.LAST_NAME,A.JOB_ID,A.SALARYFROMEMPLOYEES AWHEREA.SALARY NOTIN(2500,3500,7000)ANDA.JOB_ID IN

    (SELECTB.JOB_IDFROMJOBS BWHEREJOB_TITLE IN('Sales Representative','Stick

    cleark'));--CACH 3.2SELECTA.LAST_NAME,A.JOB_ID,A.SALARYFROMEMPLOYEES AWHEREA.JOB_ID IN

    (SELECTB.JOB_IDFROMJOBS BWHEREJOB_TITLE IN('Sales Representative','Stick

    cleark')ANDA.SALARY NOTIN(2500,3500,7000));

    11. Cho bit m nhn vin(employee_id),tn nhn vin(last_name),

    lng saukhi tng thm 15%so vi lng ban u,c lm trn nhng n vv t li tn ct l New Salary.SELECTEMPLOYEE_ID,LAST_NAME,ROUND(SALARY+((SALARY*15)/100),1)AS"NEW SALARY"FROMEMPLOYEES;12. Cho bit tn nhn vin,chiu di tng ng ca tn i vi nhngnhnvin c k tbt u trong tn l J, A,L,M.Kt quhin thtng dn theo tn,k tu ca tn vit hoa,cc k tcn li vitthng.(dng hm INITCAP,LENGTH,SUBSTR)

    SELECTINITCAP(FIRST_NAME),LENGTH(FIRST_NAME)FROMEMPLOYEESWHERESUBSTR(FIRST_NAME,1,1)IN('J','A','A','M')ORDERBYFIRST_NAME;

    13. Lit k danh sch nhn vin,khong thi gian(tnh theo thng)mnhn

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    4/37

    V ng Giang 2012

    4 Trng cao ng cng nghthng tin TP.HCM

    vin lm vic trong cng ty cho n nay.Kt qusp xp tng dntheo slng thng lm vic.(dng hm MONTHS_BETWEEN)

    SELECTEMPLOYEE_ID,FIRST_NAME,LAST_NAME,MONTHS_BETWEEN(SYSDATE,HIRE_DATE)ASKHOANG_TGFROMEMPLOYEES;

    13.1 TUONG TU CAU 13 NHUNG LAM TRON HANG CHUCSELECTEMPLOYEE_ID,FIRST_NAME,LAST_NAME,ROUND(MONTHS_BETWEEN(SYSDATE,HIRE_DATE),2)ASKHOANG_TGFROMEMPLOYEES;

    14. Thc hin cu truy vn cho kt qutheo nh dng sau :earns monthly but wants .Ct

    c hin th

    c tn

    Dream Salaries

    SELECTconcat(concat(concat(concat(last_name,' earns '),salary),'monthly but wants'),(salary*3)) asDream_SalariesFROMEMPLOYEES;

    15. Lit k tn nhn vin, mc hoa hng nhn vin nhn c.Trng hp nhn vin no khng c hng hoa hng th hin th"No commission. (dng hm NVL)

    SELECTFIRST_NAME,NVL(TO_CHAR(COMMISSION_PCT,'.9'),'NO COMMISSION')FROMEMPLOYEES;

    16. Thc hin cu truy vn cho kt qunh sau: (dng hm DECODE hocCASE)JOB_ID GRADEAD_PRES AST_MAN BIT_PROG CSA_REP DST_CLERK E

    Khng thuc 0

    selectdistinctjob_id,casejob_id when'AD_PRES'then'A'when'ST_MAN'then'B'when'IT_PROG'then'C'when'SA_REP'then'D'when'ST_CLERK'then'E'

    else'0'end"GRADE"

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    5/37

    V ng Giang 2012

    5 Trng cao ng cng nghthng tin TP.HCM

    FROMjobs ORDERBYGRADE ;

    17. Cho bit tn nhn vin, m phng, tn phng ca nhng nhn vin

    lm vic thnh phToronto.

    SELECTEMPLOYEE_ID,FIRST_NAME,LAST_NAME,CITYFROMEMPLOYEES A INNERJOIN

    (SELECTDEPARTMENT_ID,LOCATION_ID FROMDEPARTMENTS)BONA.DEPARTMENT_ID=B.DEPARTMENT_ID INNERJOIN

    (SELECTLOCATION_ID,CITYFROMLOCATIONS WHERECITY='Toronto')CONB.LOCATION_ID=C.LOCATION_ID;

    18. Lit k thng tin nhn vin cng vi ngi qun l ca nhn vin

    . Kt qu

    hi

    n th

    : m nhn vin, tn nhn vin, m ngi qu

    n l,

    tn ngi qun l.

    ----Cu 18 cch 1cch 1selecte1.employee_id,e1.last_name,e1.first_name,e1.manager_id,e2.name_managerfromemployees e1 join

    (selectdistinctemployee_id,last_name,first_name asname_manager

    fromemployees

    whereemployee_id in(selectmanager_id fromemployees))e2one1.manager_id=e2.employee_id;

    --Cu 18 cch 2selecte1.employee_id,e1.last_name,e1.first_name,e1.manager_id,e2.name_managerfromemployees e1 ,(selectdistinctemployee_id,last_name ,first_nameasname_manager

    fromemployeeswhereemployee_id in(selectmanager_id from

    employees))e2wheree1.manager_id=e2.employee_id;

    19. Lit k danh sch nhng nhn vin lm vic cng phng.

    select*fromemployeesorderbydepartment_id;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    6/37

    V ng Giang 2012

    6 Trng cao ng cng nghthng tin TP.HCM

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    7/37

    V ng Giang 2012

    7 Trng cao ng cng nghthng tin TP.HCM

    20. Lit k danh sch nhn vin c thu sau nhn vin Davies.

    select*fromEMPLOYEES wherehire_date >

    (selecthire_date fromEMPLOYEES wherelast_name='Davies');

    21. Lit k danh sch nhn vin c thu vo lm trc ngi qun lca h.

    selecta.last_name,a.hire_date,b.name_manager,b.hire_date_managerfrom (selectlast_name,hire_date,manager_id fromEMPLOYEES)a

    innerjoin(selectemployee_id,last_name asname_manager,hire_date ashire_date_managerfromEMPLOYEESwhereemployee_id in(selectdistinctmanager_id fromEMPLOYEES))bona.manager_id=b.employee_idwherea.hire_date

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    8/37

    V ng Giang 2012

    8 Trng cao ng cng nghthng tin TP.HCM

    24. Cho bit tng snhn vin, tng nhn vin c thu tng nm1995, 1996, 1997, 1998.

    SELECTto_char(hire_date,'YYYY')asNam,count(*)fromEMPLOYEESwhereto_char(hire_date,'YYYY')in('1996','1997','1998','1995')groupbyto_char(hire_date,'YYYY');

    25. Lit k tn, ngy thu ca nhng nhn vin lm vic cng phng vinhn vin Zlotkey.

    selectlast_name,hire_date fromEMPLOYEESwheredepartment_id =(selectdepartment_idfromEMPLOYEESwherelast_name=INITCAP('zlotkey'));

    26. Lit k tn nhn vin, m phng ban, m cng vic ca nhng nhnvin lm vic cho phng ban t ti vtr (location_id) 1700.

    selecta.last_name,a.department_id,a.job_id,c.location_idfromEMPLOYEES a innerjoin(selectdepartment_id,location_id fromDEPARTMENTS)b ona.department_id=b.department_idinnerjoin(selectlocation_id fromLOCATIONS wherelocation_id=1700)c

    onb.location_id=c.location_id;

    27. Lit k danh sch nhn vin c ngi qun l tn King.

    select*fromEMPLOYEESwheremanager_id in(selectemployee_id fromEMPLOYEES wherelast_name=INITCAP('king'));

    28. Lit k danh sch nhn vin c lng cao hn mc lng trung bnh

    v lm vic cng phng vi nhn vin c tn kt thc bi n.

    select*fromEMPLOYEESwheredepartment_id in(selectdepartment_id

    fromemployeeswherelast_name like('%n'))andsalary>(selectavg(salary)fromEMPLOYEES);

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    9/37

    V ng Giang 2012

    9 Trng cao ng cng nghthng tin TP.HCM

    29. Lit k danh sch m phng ban, tn phng ban c t hn 3 nhnvin.

    selectdepartment_id,department_namefromDEPARTMENTS

    wheredepartment_id in(selectdepartment_id fromemployees )groupbydepartment_idhavingcount(*)=all(selectcount(*)fromEMPLOYEESgroupbydepartment_id)orcount(*) =all

    (selectcount(to_char(hire_date,'day'))fromEMPLOYEESgroupbyto_char(hire_date,'day')))bona.Ngay=b.Ngay;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    10/37

    V ng Giang 2012

    10 Trng cao ng cng nghthng tin TP.HCM

    32. Lit k thng tin 3 nhn vin c lng cao nht.

    select*from (select*fromEMPLOYEES orderbysalary desc)

    whererownum

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    11/37

    V ng Giang 2012

    11 Trng cao ng cng nghthng tin TP.HCM

    TO VIEW

    39. To view cha thng tin ca nhng quc gia vng Asia.

    createorreplaceviewcau39asselectcountry_namefromcountrieswhereregion_id in

    (selectregion_idfromregions

    whereregion_name='Asia');

    40. To view cha danh sch nhn vin khng c ngi qun l.

    createorreplaceviewcau40as

    selectemployee_id,first_name,last_name,manager_idfromemployees

    wheremanager_id isnull;

    41. To view cha danh sch phng ban cha c nhn vin.

    createorreplaceviewcau41as

    selectdepartment_idfromDEPARTMENTSminusselectdepartment_idfromEMPLOYEES ;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    12/37

    V ng Giang 2012

    12 Trng cao ng cng nghthng tin TP.HCM

    42. To view cha m nhn vin, tn nhn vin, tn phng, m cngvic, snm lm vic, lng ca nhng nhn vin c mc lng ln hnmc lng trung bnh ca cng ty.

    createviewascau42asselecte1.employee_id,e1.first_name,e1.job_id,salary,a.department_namefromemployees e1,departments awheree1.department_id=a.department_id;

    43. Lit k cc m phng ban(department_id) khng tn ti trong bng nhnvin(employees).

    selectdepartment_id

    fromdepartments deptwherenotexists(selectnullfromemployees empwhereemp.department_id =dept.department_id );

    TO STORE PROCEDURE43. To thtc c tn l dept_info cho bit thng tin vphng banvi tham struyn vo l m phng ban.

    createorreplaceproceduredept_info(v_department_id number,ten outdepartments.department_name%type)asbegin

    select department_name intotenfromdepartmentswheredepartment_id=v_department_id;dbms_output.put_line('Ten phong ban: '||ten);exception whenno_data_found

    thendbms_output.put_line('Khong co phong ban');

    end;

    --Thuc thisetserveroutput ondeclareten departments.department_name%type;begin

    DEPT_INFO(&v_department_id,ten);end;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    13/37

    V ng Giang 2012

    13 Trng cao ng cng nghthng tin TP.HCM

    44. To thtc c tn l add_job thm mt cng vic mi vi tham struyn vo l m cng vic,tn cng vic.

    createorreplaceprocedureadd_job(v_macv JOBS.job_id%TYPE,v_tencvJOBS.job_title%TYPE)asv_macv_temp JOBS.job_id%TYPE;v_loi EXCEPTION;begin

    selectjob_id intov_macv_tempfromJOBSwherejob_id=v_macv;ifv_macv_temp isnotnullthen

    raise v_loi;endif;

    exception whenv_loi thendbms_output.put_line('Khong them duoc');whenno_data_found theninsertintoJOBS(job_id,job_title)values (v_macv,v_tencv);

    dbms_output.put_line('Cong viec '||v_tencv ||' da duocthem.');end;--thuc thisetserveroutput onexecuteadd_job('p_code','Lap Trinh Vien');45. To thtc c tn l update_comm cp nht hoa hng cho nhn vin

    tang thm 5%hoa hng ban du,tham struyn vo l m nhn vin.

    createorreplaceprocedureupdate_comm2(manhanvien number)asbegin

    updateemployeessetcommission_pct =commission_pct*1.05whereemployee_id=manhanvien;dbms_output.put_line('Cap nhat hoa hong cua '||manhanvien ||'

    thanh cong!');end;

    --thuc thisetserveroutput onexecuteupdate_comm2(3);46. To thtc c tn l add_emp thm mt nhn vin mi vi tt ccc gi trl tham struyn vo.

    createorreplaceprocedureadd_emp(v_employee_id number,first_name varchar2,last_name varchar2,email varchar2,phone_number

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    14/37

    V ng Giang 2012

    14 Trng cao ng cng nghthng tin TP.HCM

    varchar2,hire_date employees.hire_date%type,job_id varchar2,salarynumber,commission_pct number,manager_id number,department_id employees.department_id%type)asv_count number;v_loi exception;begin

    selectcount(*)intov_countfromemployeeswhereemployee_id=v_employee_id;ifv_count >0 thenraise v_loi;elseinsertintoemployees values(v_employee_id,first_name,last_name,

    email,phone_number,hire_date,job_id,salary,commission_pct,

    manager_id,department_id);dbms_output.put_line('Them thanh cong!');endif;exception whenv_loi thendbms_output.put_line('Nhan vien da ton tai');

    end;47. To thtc c tn l delete_emp xa mt nhn vin mi vi m nhnvin l tham struyn vo.

    createorreplaceproceduredelete_employee(manhanvien number)

    asbegindeletefromemployees whereemployee_id=manhanvien;dbms_output.put_line('Da xoa'||manhanvien||'thanh cong!');

    end;

    48. To thtc c tn find_emp tm kim nhn vin c lng ln hnmc lng thp nht

    createorreplaceprocedurefind_empas

    cursorc_nhanvien isselectemployee_id,first_name,last_namefromEMPLOYEES awheresalary>(selectmin_salary fromjobs b wherea.job_id=b.job_id)andsalary

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    15/37

    V ng Giang 2012

    15 Trng cao ng cng nghthng tin TP.HCM

    beginforr_nhanvien inc_nhanvienloop

    v_emp_id :=r_nhanvien.employee_id;v_first_name :=r_nhanvien.first_name;v_last_name:=r_nhanvien.last_name;dbms_output.put_line('Ma NV: '||v_emp_id ||' TenNV: '||

    v_first_name ||' '||v_last_name);endloop;

    end;--Thuc thisetserveroutput on

    executefind_emp;

    49. To th

    t

    c c tn update_comm c

    p nh

    t luong c

    a nhn vin v

    i

    diu kin nhn vin no lm vic trn 2 nam th tang luong thm 200$,nhn vin lm vic trn 1 nam v dui 2 nam th tang luong thm 100$,nhn vin no lm vic dng 1 nam th tang 50$,cn li khng tng.

    createorreplaceprocedureupdate_commasv_sonam_lamviec varchar2(2);cursorc_nhanvien isselect*fromEMPLOYEES;begin

    forr_nhanvien inc_nhanvienloopv_sonam_lamviec :=to_char(sysdate,'yyyy')-

    to_char(r_nhanvien.hire_date,'yyyy');if v_sonam_lamviec>=2 then

    updateEMPLOYEESsetsalary=salary+200whereemployee_id=r_nhanvien.employee_id;dbms_output.put_line('Nhan vien :'||

    r_nhanvien.last_name ||' da duoc tang 200$');else

    updateEMPLOYEESsetsalary=salary+100whereemployee_id=r_nhanvien.employee_id;dbms_output.put_line('Nhan vien :'||

    r_nhanvien.last_name ||' da duoc tang 100$');endif;

    endloop;end;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    16/37

    V ng Giang 2012

    16 Trng cao ng cng nghthng tin TP.HCM

    --thuc thisetserveroutput onexecuteupdate_comm;

    TO FUNCTION

    --51create or replace functionsum_salary(maphongban innumber)returnnumber

    asv_salary number;beginselectsum(salary)intov_salaryfromemployeeswheredepartment_id=maphongban;returnv_salary;exception

    whenno_data_foundthen

    return('Du lieu khong tim thay');whenothers thenreturn('loi ham');end;--Testsetserveroutput onshow error;

    selectcolumn_name,data_type,data_lengthfromuser_tab_columnswheretable_name='EMPLOYEES';--SELECTDEPARTMENT_ID FROMDEPARTMENTS;setserveroutput onsetverify offexecutedbms_output.put_line('Tong luong la: '||sum_salary(&maphong));

    52. Tao ham co ten name_con cho biet ten quoc gia voi ma quoc gia latham so--truyen vao

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    17/37

    V ng Giang 2012

    17 Trng cao ng cng nghthng tin TP.HCM

    --Xem kieu du lieu

    selectcolumn_name,data_type,data_lengthfromuser_tab_columnswheretable_name='COUNTRIES';--52createorreplacefunctionname_con(maqg incountries.country_id%type)returnvarchar2istenqg varchar2(50);begin

    selectcountry_nameintotenqgfromcountrieswherecountry_id=maqg;returntenqg;

    exceptionwhenno_data_foundthenreturn('Du lieu khong tim thay');whenothers thenreturn('loi ham');end;

    --Thuc thi

    SELECTCOUNTRY_ID FROMCOUNTRIES;--Goi thuc thi 1setserveroutput onsetverify offdeclarequocgia varchar2(35);begin

    quocgia:=name_con('AU');dbms_output.put_line(quocgia);

    end;--Goi thuc thi 2--53

    createorreplacefunctionannual_comp(luong employees.salary%type,hoahong employees.commission_pct%type)returnnumberasthunhap number;begin

    thunhap:=luong*12+(hoahong*luong*12);

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    18/37

    V ng Giang 2012

    18 Trng cao ng cng nghthng tin TP.HCM

    returnthunhap;exception

    whenno_data_foundthenreturn('Du lieu khong tim thay');whenothers thenreturn('loi ham');end;--Goi hamselectsalary,commission_pct fromEMPLOYEES;--setserveroutput onsetverify offdeclareThuNhap number;begin

    ThuNhap:=annual_comp(14200,0.4);dbms_output.put_line(ThuNhap);

    end;--54Create or replace functionavg_salary(mapb employees.department_id%type)returnnumberasluongtb number;begin

    selectavg(salary)

    intoluongtbfromemployeeswheredepartment_id=mapb;returnluongtb;

    exceptionwhenno_data_foundthenreturn('Du lieu khong tim thay');whenothers thenreturn('loi ham');end;

    --Goi thuc thisetserveroutput on;setverify offdeclareLuongTB number;begin

    LuongTB:=avg_salary(110);dbms_output.put_line(LuongTB);

    end;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    19/37

    V ng Giang 2012

    19 Trng cao ng cng nghthng tin TP.HCM

    --55

    Create or replace functionTime_work(MaNhanVienEMPLOYEES.EMPLOYEE_ID%TYPE)returnnumberastglamviec number;

    beginselect round(months_between(to_date(sysdate,'dd/mm/yyyy'),to_date(hire_date,'dd/mm/yyyy')),1)

    intotglamviecfromEMPLOYEESwhereemployee_id=MaNhanVien;returntglamviec;

    exception whenno_data_found thenreturn('Du lieu khong tim thay');end;--Goi thuc thisetserveroutput ondeclaretg number(22);begintg:=Time_work(&manv);dbms_output.put_line('So thang lam viec cua nhan vien la '||tg);

    end;

    show error

    --TESTselect*fromemployees;--xem kieu du lieu cua cotselectcolumn_name,data_type,data_lengthfromuser_tab_columnswheretable_name='EMPLOYEES'

    select round(months_between(to_date(sysdate,'dd/mm/yyyy'),to_date(hire_date,'dd/mm/yyyy')),2)fromemployees

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    20/37

    V ng Giang 2012

    20 Trng cao ng cng nghthng tin TP.HCM

    TO TRIGGER

    60. Ci t rng buc ton vn ngy thu nhn vin phi nhhn hoc bng ngyhin hnh khi thm mi hoc cp nht thng tin vnhn vin.

    createorreplacetriggertr_ngaythueafterinsertorupdateonEMPLOYEESforeach rowdeclarev_ngaythue EMPLOYEES.HIRE_DATE%TYPE;begin

    if(v_ngaythue>sysdate)thenraise_application_error(-20020,'Ngay thue khong hop le');

    endif;end;

    61. Ci t rng buc ton vn min_salary lun nhhn max_salary khi thm mihoc cp nht thng tin bng cng vic

    createorreplacetriggertr_luongbefore insertorupdateonjobsforeach row

    beginif(:new.min_salary>:new.max_salary)thenraise_application_error(-20022,'Luong nhap vao khong hop le');

    endif;end;

    62. Ci t rng buc ton vn ngy bt u lun nhhn hoc bng ngy kt thckhi thm mi hoc cp nht thng tin bng job_history.

    createorreplacetriggertr_Ngaybefore insertorupdateonjob_history

    foreach rowbegin

    if(:new.start_date>:new.end_date)thenraise_application_error(-20021,'Ngay bat dau phai nho hon ngay

    ket thuc');endif;

    end;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    21/37

    V ng Giang 2012

    21 Trng cao ng cng nghthng tin TP.HCM

    63. Ci t rng buc ton vn lng v hoa hng ca nhn vin phi tng chkhng gim khi cp nht nhn vin.

    createorreplacetriggertr_Luong_HoaHong

    before updateonemployeesforeach rowbegin

    if(:new.salary

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    22/37

    V ng Giang 2012

    22 Trng cao ng cng nghthng tin TP.HCM

    TO PACKAGEcreateorreplacepackage emp_infoasproceduresalary_table(manv employees.employee_id%TYPE);functionsum_salary(mapb departments.department_id%TYPE)returnnumber;end;

    createorreplacepackage body emp_infoasproceduresalary_table(manv employees.employee_id%TYPE)asluong employees.salary%TYPE;

    beginselectsalary intoluong fromemployees whereemployee_id=manv;dbms_output.put_line('luong cua nhan vien nay la:'||luong);exceptionwhenno_data_found thendbms_output.put_line('ko tim thay nhan vien nay');endsalary_table;

    functionsum_salary(mapb departments.department_id%TYPE)returnnumberas

    tongluong number;beginselectsum(salary)intotongluong fromemployees wheredepartment_id=mapb;returntongluong;exceptionwhenno_data_found thendbms_output.put_line('ko tim thay phong ban');endsum_salary;endemp_info;

    thuc thisetserveroutput on;declarev_luong number;v_tongluong number;beginemp_info.salary_table(198);v_tongluong:=emp_info.sum_salary(110);

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    23/37

    V ng Giang 2012

    23 Trng cao ng cng nghthng tin TP.HCM

    dbms_output.put_line(v_luong);dbms_output.put_line(v_tongluong);end;

    58. To package c tn l job_pack cha 3 thtc tn l add_job thm cngvic,update_job cp nht cng vic,del_job xa cng vic v 1 hm c tnq_job tm kim tn cng vic theo m.

    createorreplacepackage job_packasprocedureadd_job(

    macv jobs.job_id%type,

    tencv jobs.job_title%type,luongthapnhat jobs.min_salary%type,luongcaonhat jobs.max_salary%type

    );procedureupdate_job(

    macv jobs.job_id%type,tencv jobs.job_title%type,luongthapnhat jobs.min_salary%type,

    luongcaonhat jobs.max_salary%type);proceduredel_job(

    macv jobs.job_id%type);endjob_pack;

    --phan thancreateorreplacepackage body job_packas

    procedureadd_job(macv jobs.job_id%type,tencv jobs.job_title%type,luongthapnhat jobs.min_salary%type,luongcaonhat jobs.max_salary%type

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    24/37

    V ng Giang 2012

    24 Trng cao ng cng nghthng tin TP.HCM

    )is

    begininsertintoJOBS

    values (macv,tencv,luongthapnhat,luongcaonhat);dbms_output.put_line('Cong viec '||tencv||' da

    duoc them');exception whenno_data_found then

    dbms_output.put_line('Khong tim thay cong viec');endadd_job;

    procedureupdate_job(

    macv jobs.job_id%type,

    tencv jobs.job_title%type,luongthapnhat jobs.min_salary%type,luongcaonhat jobs.max_salary%type

    )isbeginupdatejobs setjob_title=tencv,min_salary=luongthapnhat,max_salary=luongcaonhat wherejob_id=macv;dbms_output.put_line('CAP NHAT THANH CONG');endupdate_job;proceduredel_job(macv jobs.job_id%type)is

    begindeletefromjobs wherejob_id=macv;

    dbms_output.put_line('XOA THANH CONG');enddel_job;

    endjob_pack;

    --Thuc thi

    --add_jobbeginjob_pack.add_job('ADMIN2','ADMINISTRATOR2',20000,60000);end;--update_jobbegin

    job_pack.update_job('ADMIN2','AA',21000,61000);

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    25/37

    V ng Giang 2012

    25 Trng cao ng cng nghthng tin TP.HCM

    end;--del_job cach goi 1EXECUTEjob_pack.del_job('ADMIN')--del_job cach goi 2

    beginjob_pack.del_job('ADMIN3');

    end;

    59. To package c tn emp_pack cha mt thtc tn new_emp thm mt nhnvin mi vi tt ccc tham struyn vo v mt hm tn valid_deptid kim tram phng ban hp l, khi m phng hp lmi c php thm nhn vin.

    createorreplacepackage emp_pack

    asprocedurenew_emp(

    MaNV employees.employee_id%type,TenNV employees.first_name%type,HoNV employees.last_name%type,Email employees.email%type,DienThoai employees.phone_number%type,NgayThue employees.hire_date%type,MaCV employees.job_id%type,Luong employees.salary%type,HoaHong employees.commission_pct%type,MaQuanLy employees.manager_id%type,MaPhong employees.department_id%type

    );functionvalid_deptid(i_department_id innumber)returnboolean;endemp_pack;

    --phan than

    createorreplacepackage body emp_packas

    procedurenew_emp(

    MaNV employees.employee_id%type,TenNV employees.first_name%type,

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    26/37

    V ng Giang 2012

    26 Trng cao ng cng nghthng tin TP.HCM

    HoNV employees.last_name%type,Email employees.email%type,DienThoai employees.phone_number%type,NgayThue employees.hire_date%type,

    MaCV employees.job_id%type,Luong employees.salary%type,HoaHong employees.commission_pct%type,MaQuanLy employees.manager_id%type,MaPhong employees.department_id%type

    )isbegin

    insertintoemployees values(MaNV,TenNV,HoNV,Email,

    DienThoai,NgayThue,MaCV,Luong,HoaHong,MaQuanLy,MaPhong);endnew_emp;--ket thuc proc new_empfunctionvalid_deptid(i_department_id innumber)returnbooleanis

    v_id_dept number;begin

    selectcount(*)intov_id_deptfromdepartmentswheredepartment_id=i_department_id;

    return1=v_id_dept;exception whenothers thenreturnfalse;

    endvalid_deptid;--ket thuc proc valid_deptidendemp_pack;

    --goi thuc thi

    setserveroutput onbegin

    if(emp_pack.valid_deptid(&i_department_id))thenemp_pack.new_emp(1,'First',

    'Last','[email protected]','(123)123-1234','18-JUN-02','IT_PROG',900,00,

    100,110);

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    27/37

    V ng Giang 2012

    27 Trng cao ng cng nghthng tin TP.HCM

    dbms_output.put_line('Them thanh cong');else

    dbms_output.put_line('Ma phong ban nay khong tontai!');

    endif;end;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    28/37

    V ng Giang 2012

    28 Trng cao ng cng nghthng tin TP.HCM

    QUN TRNGI DNG

    Qun trngi dng.

    64. To khng gian bng (tablespace) c kch thc 100M.

    65. To khng gian bng tm (temporary tablespace) c kch thc 50M.

    66. To rollback segment rolora truy xut ng thi cho table space va to.

    67. To user c tn l tn sinh vin, mt khu do sinh vin tt vi tablespace vtemporary tablespace va to.

    68. Cp quyn truy xut ti nguyn (resource) cho user va to.

    69. Cp quyn cho phin lm vic (session) cho user va to.

    70. Cp quyn to bng (table) cho user va to.

    71. Cp quyn to khung nhn (view) cho user va to.

    72. Cp quyn Select, Insert, Update, Delete trn bng Employees cho user va

    to.73. Cp quyn Select, Insert, Update, Delete trn bng Departments cho user vato.

    74. Cp quyn Select chvi cc thuc tnh job_id, job_title trn bng Jobs chouser va to.

    75. Login vo csdl HR vi user va to.

    76. Truy vn cc bng trong csdl HR v cho nhn xt.

    77. Cho bit cc user hin c tview dba_users.78. ng nhp vi quyn hthng v tao user c tn l m sinh vin, mt khu ltn sinh vina. Thay i mt khu ca user.

    b. Cp quyn ng nhp csdl

    c. Truy xut view v$session xem phin lm vic.

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    29/37

    V ng Giang 2012

    29 Trng cao ng cng nghthng tin TP.HCM

    d. Tao bng phng ban gm 2 thuc tnh: m phng ban, tn phng ban v chonhn xt cu c.

    e. Cp pht hn ngch (quota) 20M cho user va tao.

    f. Tao li bng Phng ban v cho nhn xt.g. Cp pht khng gian gii hn tablespace cho user va tao

    h. Cp pht ti nguyn cho user

    i. Cp pht phin lm vic cho user

    j. Cp quyn ng nhp OEM79. Truy xut vo view hthng dba_profiles.

    80. Tao profile gii hn vic truy xut ti nguyn v cp cho user va to:

    a. Thi gian kt ni 120s

    b. Sln tht bi khi cho php kt ni l 3.

    c. Thi gian chkt ni l 60s

    d. Phin lm vic cho mi user l 2

    e. Gn profile cho user va tao

    f. Thay i thng sca profile vi phin lm vic cho mi user l 4.

    g. Sln tht bi khi cho php kt ni l 3

    h. Thit lp thng sprofile c hiu lc.

    i. Xa profile va to.81. Oracle Database Resource Manager(ODRM) to vng treo,tao nhm ngidng, tao khoch ti nguyn v nh hng khoch, kim tra tnh hp lcavng treo v gi vng treo:a. To vng treo.

    b. Xa vng treo.

    c. Kim tra nhm ngi dng no tn ti v cho nhn xt lin quan cc nhmngi dng.

    d. Kim tra khoch ca mi CSDL tdba_rsrc_plans

    e. Tao 4 nhm ngi dng: cp nht, tm kim, thng k, bo co

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    30/37

    V ng Giang 2012

    30 Trng cao ng cng nghthng tin TP.HCM

    f. Kim tra tnh hp lca vng treo.

    g. Truy xut view dba_users v cho nhn xt.

    h. Gn ngi dng va to ti nhm ngi dng.

    i. Truy xut view dba_users v cho nhn xt.

    j. Khi to user cho nhm ngi dng

    k. Truy xut view dba_users v cho nhn xt.

    l. To khoch ti nguyn

    m. To nh hng khoch v gii hn ti nguyn cho tng ngi dng (CPU_1nhm cp nht l 60, CPU_1 nhm thng k 40, CPU_2 nhm bo co l 30, nhmkhc l 100).

    n. Gi vng treo.

    o. Truy xut view dba_rsrc_plan_directives v cho nhn xt.

    p. Kch hot khoch sdng ti nguyn

    q. Truy xut view v$rsrc_consumer_group xem vic sdng ti nguyn giacc nhm

    r. Truy xut view v$rsrc_plan v cho nhn xt.

    --64

    create tablespace oracle

    datafile'oracle.dbf' size 100m;

    --65

    create temporary tablespace oracle_2

    tempfile'oracle2.dbf' size 50m;

    --66

    create rollback segment seg

    tablespace oracle;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    31/37

    V ng Giang 2012

    31 Trng cao ng cng nghthng tin TP.HCM

    --67

    create user sinhvien identified by sinhvien

    default tablespace oracletemporary tablespace oracle_2

    password expire;

    --68

    grant resource to sinhvien;

    --69

    grant create session to sinhvien;

    --70

    grant create table to sinhvien;

    --71

    grant create view to sinhvien;

    --72

    grant insert, update, select, delete on hr.employees to

    sinhvien;

    --73

    grant insert, select, update, delete on hr.departments to

    sinhvien;

    --74

    grant update(job_id, job_title) on hr.jobs to sinhvien;

    --75

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    32/37

    V ng Giang 2012

    32 Trng cao ng cng nghthng tin TP.HCM

    conn hr/hr;

    --76

    Khong truy van duoc vi chua cap quyen truy van--77

    select username from dba_users;

    --78

    conn system/system;

    grant create user to hr;

    conn hr/hr;

    create user masinhvien identified by masinhvien

    password exprire;

    --78bis xoa user masinhvien

    conn system/system;

    drop user masinhvien;

    --78b

    create create connect to masinhvien;

    --78c

    select username, status from v$session;

    --78 d Khong tao duoc vi chua cap quyen

    --78e

    create user sinhvien identified by sinhvien

    default tablespace oracle

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    33/37

    V ng Giang 2012

    33 Trng cao ng cng nghthng tin TP.HCM

    temporary tablespace oracle_2

    quota 100m on oracle;

    --80 a, b, c, dcreate profile giang limit

    connect_time 120

    failed_login_attempts 3

    idle_time 60

    sessions_per_user 2;

    --80e

    alter user sinhvien identified by sinhvien

    profile giang;

    --80h

    thiet lap thong so de profile co hieu luc

    grant create profile to sinhvien;

    --80f,g

    alter profile giang limit

    sessions_per_user 4

    failed_login_attempts 3

    --80i

    drop profile giang cascade;

    --81 a.Tao vung treo

    exec dbms_resource_manager.create_pending_area;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    34/37

    V ng Giang 2012

    34 Trng cao ng cng nghthng tin TP.HCM

    --81 b.xoa vung treo

    exec dbms_resource_manager.clear_pending_area;

    --81 eexec dbms_resource_manager.create_pending_area;

    exec

    dbms_resource_manager.create_consumer_group('capnhat','nhom

    cap nhat');

    exec

    dbms_resource_manager.create_consumer_group('timkiem','nhom

    tim kiem');

    exec

    dbms_resource_manager.create_consumer_group('thongke','nhom

    thong ke');

    exec

    dbms_resource_manager.create_consumer_group('baocao','nhom

    bao cao');

    --81 f

    --kiem tra tinh hop le cua vung treo

    exec dbms_resource_manager.validate_pending_area

    --81 h

    grant capnhat to sinhvien;

    grant timkiem to sinhvien;

    grant thongke to sinhvien;

    grant baocao to sinhvien;

    --81 i

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    35/37

    V ng Giang 2012

    35 Trng cao ng cng nghthng tin TP.HCM

    select username, account_status from dba_users

    --81 h. Gn ngi dng va to ti nhm ngi dng

    conn system/system

    exec

    dbms_resource_manager_privs.grant_system_privilege('SYSTEM',

    'ADMINISTER_RESOURCE_MANAGER',TRUE);

    exec dbms_resource_manager.create_pending_area

    exec dbms_resource_manager.create_consumer_group('sv','nhomsv','ROUND-ROBIN'

    --khong chay dc

    exec

    dbms_resource_manager_privs.grant_switch_consumer_group('sin

    hvien','sv',true)

    --81 l

    exec dbms_resource_manager.create_pending_area

    exec

    dbms_resource_manager.create_plan('kehoachtainguyen','ke

    hoach tai nguyen cho nguoi dung')

    --81 m

    exec

    dbms_resource_manager.create_plan_directive('kehoachtainguye

    n','capnhat',60,100,100,100,100,100,100,100)

    exec

    dbms_resource_manager.create_plan_directive('kehoachtainguye

    n','thongke',40,100,100,100,100,100,100,100)

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    36/37

    V ng Giang 2012

    36 Trng cao ng cng nghthng tin TP.HCM

    exec

    dbms_resource_manager.create_plan_directive('kehoachtainguye

    n','baocao',30,100,100,100,100,100,100,100)

    --81 n gui ving treo

    exec dbms_resource_manager.create_pending_area

    exec dbms_resource_manager.submit_pending_area

    --81 o

    select plan, cpu_p1, cpu_p2, cpu_p3 from

    dba_rsrc_plan_directives

    --Tao role

    create role capnhat;

    create role timkiem;

    create role thongke;

    create role baocao;

    --cap quyen

    grant capnhat to sinhvien;

    --Xoa role

    revoke capnhat from sinhvien;

  • 5/28/2018 Idoc.vn Bai Tap Oracle Co Loi Giai

    37/37

    V ng Giang 2012

    37 Trng cao ng cng nghthng tin TP.HCM

    THE END