akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the...

30
Homework Week #1 1. 3GL PL/SQL YES SQL NO 4GL PL/SQL NO SQL YES Is proprietary to Oracle Corporation PL/SQL YES SQL NO Nonprocedural PL/SQL NO SQL YES Procedural PL/SQL YES SQL NO Is ANSI-compliant PL/SQL NO SQL YES 2. SQl is limited 4GL language. PL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. OPTIONAL OR MANDATORY ? WHAT IS INCLUDED IN THIS SECTION ? DECLARE Optional Contains declarations of all variables, constants, cursors, and user-defined exceptions that are referenced in the executable and exception section BEGIN Mandatory Contains PL/SQL and SQL statements. Must contain at least one statement EXCEPTION Optional Specifies the actions to perform when errors and abnormal conditions arise in the executable section. END; Mandatory Contains PL/SQL and SQL statements. Must contain at least one statement. Executable sections ending with 4.

Transcript of akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the...

Page 1: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #11.

3GL PL/SQL YES SQL NO4GL PL/SQL NO SQL YES Is proprietary to Oracle Corporation PL/SQL YES SQL NONonprocedural PL/SQL NO SQL YES Procedural PL/SQL YES SQL NOIs ANSI-compliant PL/SQL NO SQL YES

2. SQl is limited 4GL language. PL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements.

3.OPTIONAL OR MANDATORY ? WHAT IS INCLUDED IN THIS

SECTION ?DECLARE Optional Contains declarations of all

variables, constants, cursors, and user-defined exceptions that are referenced in the executable and exception section

BEGIN Mandatory Contains PL/SQL and SQL statements. Must contain at least one statement

EXCEPTION Optional Specifies the actions to perform when errors and abnormal conditions arise in the executable section.

END; Mandatory Contains PL/SQL and SQL statements. Must contain at least one statement. Executable sections ending with

4.

A. Unsuccessful. At least one statement is needed.B. Unsuccessful. The key word BEGIN is missingC. Unsuccessful. At least one statement is neededD. Successful.

Page 2: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

5.A.BEGINDBMS_OUTPUT.PUT_LINE('Simon Kacmar');END;B.DECLAREv_date DATE := SYSDATE;BEGINDBMS_OUTPUT.PUT_LINE('In six months, the date will be:' || ADD_MONTHS(v_date,6));END;

6.SELECT country_name, CONCAT(CONCAT(national_holiday_name, ' is '),national_holiday_date)NATIONAL_HOLIDAY FROM wf_countries;7.SELECT country_name, life_expect_at_birth, (life_expect_at_birth+7) "Improved Expectancy." FROM wf_countriesWHERE median_age < 20;8.SELECT UPPER(country_name)"NAME" FROM wf_countriesORDER BY country_name ASC;9.SELECT LOWER(language_name) FROM wf_languagesWHERE language_name LIKE 'F%'10.SELECT region_id id, region_name name, SUBSTR(region_name,0,3) abbreviation FROM wf_world_regions;11.SELECT region_id id, region_name name, SUBSTR(region_name,0,3) || (LENGTH(region_name)) abbreviation FROM wf_world_regions;12.SELECT country_name, ROUND(life_expect_at_birth)"life expentancy" FROM wf_countries;13.SELECT country_name, NVL(capitol,'none listed') FROM wf_countries

Page 3: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #21.A. An _____identifier______ is the name given to a PL/SQL object.

B. A __Reserved word_____ is a word that has special meaning to the Oracle database.

C. A ___Delimeter______ is a symbol that has special meaning to the Oracle database.

D. A ___Literal_______is an explicit numeric, character string, date, or Boolean value that is not represented by an identifier.

E. A_Comment___ explains what a piece of code is trying to achieve.

2.

Identifier Valid (X) Invalid (X) Why Invalid?Today XLast name X Empty spacetoday’s_date X apostrophenumber_of_days_in_february_this_ year

X More than 30 characters

Isleap$year X#number X First character mus

be letterNUMBER# XNumber1to7 X

3.Word Reserved? Y/N

Create YMake NTable YSeat NAlter YRename YRow NNumber YWeb N

Page 4: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

4.Value Lexical Unit

SELECT Reserved word:= Delimeter‘TEST’ Character literal FALSE Boolean literal-- new process CommentFROM Reserved word/*select the country with the highest elevation */

Multiline comment

V_test identifier4.09 Numeric literal

5.Value Data Type Data Type Category Data Type

Switzerland Scalar Character100.20 Scalar Number1053 Scalar Number12-DEC-2005 Scalar DateFalse Scalar Boolean

Index Last_name1 'Newman'2 'Raman'3 'Han'

Composite Table

A movie Lob BFileA soundbyte Lob BFileA picture Lob Blob

6.Avoid using column name as identifierv_country_name VARCHAR2 (50); v_median_age NUMBER(6,2);line 7 missing parenthesis

7.v_country_name wf_countries.country_name%TYPE; v_median_age wf_countries.median_age%TYPE; 8.DECLAREv_today DATE := SYSDATE;v_tomorow v_today%TYPE;BEGINv_tomorow := (v_today +1); DBMS_OUTPUT.PUT_LINE('Hello World ' || v_today || ' ' || v_tomorow);END;9.SELECT country_name, currency_code, currency_name

Page 5: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

FROM wf_countries INNER JOIN wf_currencies USING(currency_code)ORDER BY country_name;

10.SELECT country_name, official, language_name, language_id, country_idFROM wf_countries JOIN wf_spoken_languages USING(country_id)JOIN wf_languages USING(language_id )ORDER BY language_name,country_name

11.C – 100

12.B.

13.SELECT first_name, last_name FROM employeesWHERE hire_date = (SELECT MIN(hire_date) FROM employees)

14.The Holy See (State of the Vatican City)SELECT country_name FROM wf_countriesWHERE area= (SELECT MIN(area) FROM wf_countries)

15.

SELECT country_name, COUNT(language_id) FROM wf_countriesJOIN wf_spoken_languages USING(country_id)WHERE language_id > 0GROUP BY country_nameORDER BY COUNT(language_id) DESC

16.SELECT currency_name,COUNT(country_name) FROM wf_currenciesJOIN wf_countries USING(currency_code)GROUP BY currency_name HAVING COUNT(country_name) > 1ORDER BY currency_name

17.A: ‘123456’B: 579C: Operator + convert values to numbers and sum them.

Page 6: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

18.BEGIN DBMS_OUTPUT.PUT_LINE('Dlzka mojho mena je: ' || LENGTH('Simon Kacmar'));END;

19.DECLAREv_my_date DATE := SYSDATE;v_last_day DATE;BEGINv_last_day:=LAST_DAY(v_my_date);DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_my_date,'Month DD, YYYY') );DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_last_day,'Month DD, YYYY') );END;

20.DECLAREv_my_date DATE := SYSDATE + 45;v_last_day DATE;BEGINv_last_day:=LAST_DAY(v_my_date);DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_my_date,'Month DD, YYYY') );DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_last_day,'Month DD, YYYY') );DBMS_OUTPUT.PUT_LINE(MONTHS_BETWEEN(v_my_date,v_last_day ) );END

21.A: 11B: 11C: * has higher priory than +

22. A: 2B: Wester EuropeC: 601 D: Product 10012 is in stock

23.DECLARE v_country_name wf_countries.country_name%TYPE; v_myResult number(4);BEGINv_myResult := '1234';v_myResult := v_myResult * 2;SELECT country_name INTO v_country_nameFROM wf_countries WHERE country_id = 1246;DBMS_OUTPUT.PUT_LINE(v_country_name);End;

Page 7: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #31.

C. Delete all rows from STUDENTS

2.Statement Valid in PL/SQL Not Valid in PL/SQL

ALTER USER SET password='oracle';

X

CREATE TABLE test (a NUMBER);

X

DROP TABLE test; XSELECT emp_id INTO v_id FROM employees;

X

GRANT SELECT ON employees TO PUBLIC;

X

INSERT INTO grocery_items (product_id, brand, description) VALUES (199,'Coke','Soda');

X

REVOKE UPDATE ON employees FROM PUBLIC;

X

ALTER TABLE employees RENAME COLUMN employee_id TO emp_id;

X

DELETE FROM grocery_items WHERE description='Soap');

X

3.False, uses implicit cursors

4.Explicit _ cursors are created by the programmer

5.Implicit__ cursors are created by the Oracle server.

Page 8: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

6.DECLARE v_country_name wf_countries.country_name%TYPE := 'United States of America'; v_lowest_elevation wf_countries.lowest_elevation%TYPE; v_highest_elevation wf_countries.highest_elevation%TYPE;BEGIN SELECT lowest_elevation, highest_elevation INTO v_lowest_elevation, v_highest_elevation FROM wf_countries WHERE country_name = v_country_name;

DBMS_OUTPUT.PUT_LINE('The lowest elevation in ' || v_country_name||' is '||v_lowest_elevation || ' and the highest elevation is '|| v_highest_elevation ||'.');END;

7.One, this block is whole one transaction

8.Done.

9.100 ‘Polar Bear‘ ‘Ursus maritimus‘

10.Conditional Control: IF and CASE StatementsIterative Control: LOOP and EXIT StatementsSequential Control: GOTO and NULL Statements

11.ELSE, ELSE IF

12.IF, THEN, END IF;

13.DECLARE v_sum_salary employees.salary%TYPE;BEGIN SELECT SUM(salary) INTO v_sum_salary FROM employees WHERE department_id = 60;DBMS_OUTPUT.PUT_LINE(v_sum_salary );IF v_sum_salary > 19000 THENDBMS_OUTPUT.PUT_LINE('salary is greater than 19000' );ELSE DBMS_OUTPUT.PUT_LINE('salary is less than 19000' );END IF;END;

Page 9: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

14.Output will be 'salary is less than 19000', becouse in the if statement is not equality

15.DECLARE v_sum_salary employees.salary%TYPE;BEGIN SELECT SUM(salary) INTO v_sum_salary FROM employees WHERE department_id = 20;DBMS_OUTPUT.PUT_LINE(v_sum_salary );IF v_sum_salary > 19000 THENDBMS_OUTPUT.PUT_LINE('salary is greater than 19000' );ELSIF v_sum_salary < 19000 THENDBMS_OUTPUT.PUT_LINE('salary is less than 19000' );ELSEDBMS_OUTPUT.PUT_LINE('salary is equal to 19000' );END IF;END;

16.DECLAREv_number_of_countries NUMBER;BEGINSELECT COUNT(currency_code) INTO v_number_of_countries FROM wf_countries WHERE currency_code = 'EUR'; CASE WHEN v_number_of_countries > 20 THEN DBMS_OUTPUT.PUT_LINE('More than 20 countries'); WHEN (v_number_of_countries >= 10 AND v_number_of_countries <= 20) THENDBMS_OUTPUT.PUT_LINE('between 10 and 20 countries'); WHEN v_number_of_countries < 10 THEN DBMS_OUTPUT.PUT_LINE ('Fewer than 10 countries'); END CASE;END;

17.to execute statement repeatly

18.BASIC , FOR, WHILE

19.EXIT statement

20.

DECLARE v_country_name wf_countries.country_name%TYPE; v_country_id wf_countries.country_id%TYPE; v_pom INTEGER := 1;BEGIN

Page 10: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

LOOPv_country_id := v_pom;SELECT country_id, country_name INTO v_country_id, v_country_nameFROM wf_countriesWHERE country_id = v_country_id;v_pom := v_pom +1;DBMS_OUTPUT.PUT_LINE('number is: ' || v_pom );DBMS_OUTPUT.PUT_LINE(v_country_id || ' ' || v_country_name);IF v_pom > 3 THEN EXIT;END IF;END LOOP;END;

21.DECLARE v_country_name wf_countries.country_name%TYPE; v_country_id wf_countries.country_id%TYPE; v_pom INTEGER := 1;BEGINLOOPv_country_id := v_pom;SELECT country_id, country_name INTO v_country_id, v_country_nameFROM wf_countriesWHERE country_id = v_country_id;v_pom := v_pom +1;DBMS_OUTPUT.PUT_LINE('number is: ' || v_pom );DBMS_OUTPUT.PUT_LINE(v_country_id || ' ' || v_country_name);EXIT WHEN v_pom > 3;END LOOP;END;

22.BEGINFOR i IN 60..65 LOOPFOR j IN 100..110 LOOPDBMS_OUTPUT.PUT_LINE(i || ' ' || j);END LOOP;END LOOP;END;

Page 11: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #41.DECLAREv_country_name wf_countries.country_name%TYPE;v_national_holiday_date wf_countries.national_holiday_date%TYPE; v_national_holiday_name wf_countries.national_holiday_name%TYPE;CURSOR wf_countries_cursor IS SELECT country_name, national_holiday_date, national_holiday_name FROM wf_countries WHERE region_id = 5 AND national_holiday_date IS NOT NULL;BEGIN OPEN wf_countries_cursor;LOOPFETCH wf_countries_cursor INTO v_country_name, v_national_holiday_date, v_national_holiday_name;exit WHEN wf_countries_cursor%NOTFOUND;DBMS_OUTPUT.PUT_LINE(v_country_name || ' - ' || v_national_holiday_date || ' - ' || v_national_holiday_name);END LOOP;CLOSE wf_countries_cursor;END;

2.DECLARECURSOR employee_count_cursor IS SELECT department_name, COUNT(*) AS how_many FROM departments d, employees eWHERE d.department_id = e.department_idGROUP BY d.department_nameORDER BY how_many DESC;v_record employee_count_cursor%ROWTYPE;BEGINOPEN employee_count_cursor;LOOPFETCH employee_count_cursor INTO v_record;EXIT WHEN employee_count_cursor%ROWCOUNT = 6; -- showing first 5 row departments;DBMS_OUTPUT.PUT_LINE(v_record.department_name || ' - ' || v_record.how_many);END LOOP;CLOSE employee_count_cursor;END;

3.Inifinite loop. When i tried it , get internal server error and couldnt acces to sql commands for 10 minutes.

Page 12: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

4.DECLARECURSOR employee_count_cursor IS SELECT department_name, COUNT(*) AS how_many FROM departments d, employees eWHERE d.department_id = e.department_idGROUP BY d.department_nameORDER BY how_many DESC;v_record employee_count_cursor%ROWTYPE;BEGINOPEN employee_count_cursor;LOOPFETCH employee_count_cursor INTO v_record;EXIT WHEN (employee_count_cursor%ROWCOUNT = 10) OR (employee_count_cursor%NOTFOUND);DBMS_OUTPUT.PUT_LINE(v_record.department_name || ' - ' || v_record.how_many);END LOOP;CLOSE employee_count_cursor;END;

5.DECLARE CURSOR wf_currencies_cur IS SELECT currency_code, currency_name FROM wf_currencies ORDER BY currency_name; -- v_record wf_currencies_cur%ROWTYPE;BEGIN FOR v_record in wf_currencies_cur LOOP DBMS_OUTPUT.PUT_LINE(v_record.currency_code || ' ' || v_record.currency_name); END LOOP;END;

6.DECLARE CURSOR country_cursor (p_region_id NUMBER) ISSELECT country_name, area FROM wf_countriesWHERE region_id = p_region_id;v_record country_cursor%ROWTYPE;BEGIN OPEN country_cursor (30);LOOPFETCH country_cursor INTO v_record ;exit WHEN country_cursor%NOTFOUND;DBMS_OUTPUT.PUT_LINE(v_record.country_name || ' ' || v_record.area);END LOOP;CLOSE country_cursor;END;

Page 13: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

7.DECLARE CURSOR department_cur ISSELECT department_id, department_name FROM departments;--v_record_department department_cur %ROWTYPE;

CURSOR employee_cur ISSELECT e.department_id, first_name, last_name, salary, department_name FROM departments d, employees eWHERE d.department_id = e.department_id;

--v_record_employee employee_cur %ROWTYPE;

BEGIN FOR v_record_department IN department_cur LOOPDBMS_OUTPUT.PUT_LINE(v_record_department.department_id|| ' ' || v_record_department.department_name );DBMS_OUTPUT.PUT_LINE('---------------------'); FOR v_record_employee IN employee_cur LOOP IF v_record_employee.department_id = v_record_department.department_id THEN DBMS_OUTPUT.PUT_LINE(v_record_employee.first_name|| ' ' || v_record_employee.last_name|| ' ' || v_record_employee.salary); DBMS_OUTPUT.PUT_LINE(''); END IF; END LOOP;END LOOP;END;

Page 14: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #51.It disrupts the normal operation of actual pl/sql statement.

2.Fix the errors or add exception handle in exception section

3.Protect the database from errors (Data lost).Protect the user from errors.Code is more readable.

4.A: The ORA-01422 error occurB: There are 3 rows in Select statement. pl/sql block can SELECT INTO only one row at time without using i.e. cursor. It can be fixed by adding cursor to DECLARE section or catching exception. C:DECLARE v_jobid employees.job_id%TYPE;BEGIN SELECT job_id INTO v_jobid FROM employees WHERE department_id = 80;EXCEPTIONWHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('statement retrieved multiple rows');END;D: Exception message appear and statement processed

5.Predefined Oracle server error.Non-predefined Oracle server error.User-defined error.

6.Predefined error – Not need to declare. They are predefined automatically.Non-predefined error – Declare within the declarative section and allow the Oracle server raise them automatically. User-defined error – Declare within the declarative section but need to raise them explicitly.

7.A: ORA-01403: no data found. Region-id = 1 is not in table wf_world_regions.B: error message displayed. DECLARE v_number NUMBER(6,2) := 100; v_region_id wf_world_regions.region_id%TYPE;

Page 15: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

v_region_name wf_world_regions.region_name%TYPE;BEGIN SELECT region_id, region_name -- INTO v_region_id, v_region_name FROM wf_world_regions WHERE region_id = 1; DBMS_OUTPUT.PUT_LINE('Region: ' || v_region_id || ' is: ' || v_region_name); v_number := v_number / 0;EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Select statement found no rows')END;

C: ORA-01476: divisor is equal to zero Data is returning but value of v_number can’t be divided by zero. Therefore, error message appear. v_number := v_number / 0;

8.There are 2 nested pl/sql blocks. The first row displays last_name of inner block which is “Lorentz” and second row display last_name from outer block which is “King”. The same name of variable can be used this way.

9.Subprograms can be explicitly shared or stored in database. Another advantage is easy maintenance. They can be used or reused in any number of applications. Subprograms can take parameters. They Improved data security and data integrity. Using subprograms helps avoid to compiling code again which improve performance.

10.Procedures are objects with pl/sql block stored in database data dictionary to after reusability.

11.Done

12.A: select * from employees_dup where department_id = 60;B: CREATE OR REPLACE PROCEDURE pay_raise ISBEGIN UPDATE employees_dup SET salary = 30000;END pay_raise;C: BEGIN pay_raise;END;D: select * from employees_dup

Page 16: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

13.ORA-00955: name is already used by an existing object

14.A:CREATE OR REPLACE PROCEDURE get_country_info (p_country_id IN NUMBER)ISv_country_name wf_countries.country_name%TYPE;v_capitol wf_countries.capitol%TYPE;BEGINSELECT country_name, capitol INTO v_country_name, v_capitol FROM wf_countries WHERE country_id = p_country_id;END get_country_info;B: Republic of Turkey AnkaraC: ORA-01403: no data foundD:CREATE OR REPLACE PROCEDURE get_country_info (p_country_id IN NUMBER)ISv_country_name wf_countries.country_name%TYPE;v_capitol wf_countries.capitol%TYPE;BEGINSELECT country_name, capitol INTO v_country_name, v_capitol FROM wf_countries WHERE country_id = p_country_id;DBMS_OUTPUT.PUT_LINE(v_country_name || ' ' || v_capitol);EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUt.PUT_LINE('country with this id not exist, please choose another id.');END get_country_info;Result: country with this id not exist, please choose another id.

Page 17: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #61.A:CREATE OR REPLACE FUNCTION full_name (p_first_name employees.first_name%TYPE,p_last_name employees.first_name%TYPE) RETURN VARCHAR2 IS BEGIN RETURN (p_first_name || ', ' || p_last_name); END full_name;

B:DECLARE v_full_name VARCHAR2(20);BEGINv_full_name :=full_name('Smith','Joe');DBMS_OUTPUT.PUT_LINE(v_full_name);END;

C:BEGINDBMS_OUTPUT.PUT_LINE(full_name('Smith','Joe'));END;

D: SELECT first_name, last_name, full_name(first_name, last_name)full_name FROM employeesWHERE department_id = 50;

2.A:CREATE OR REPLACE FUNCTION divide (a NUMBER, b NUMBER) RETURN NUMBER IS BEGIN RETURN ROUND(a/b,2); END divide;B:BEGINDBMS_OUTPUT.PUT_LINE(divide(50,2));DBMS_OUTPUT.PUT_LINE(divide(25,3));END;C:Error is thrown because divisor is equal to 0.

Page 18: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

D:CREATE OR REPLACE FUNCTION divide (a NUMBER, b NUMBER) RETURN NUMBER IS BEGIN RETURN ROUND(a/b,2); EXCEPTION WHEN ZERO_DIVIDE THEN RETURN 0;END divide;E:Exception in function is raised and 0 is returned.

3.Procedures FunctionsExecute as a PL/SQL Invoke as part of expression Do not contain RETURN clause in the header Must contain RETURN clause in the headerCan return values (if any) in output parameters Must return a single value Can contain a RETURN statement without a value

Must contain at least one RETURN statement

4.SELECT owner, type FROM ALL_SOURCE WHERE type = 'FUNCTION' OR type = 'PROCEDURE'

5.GRANT INSERT,DELETE,SELECT,UPDATE ON wf_countries TO SUSAN;

6.A: NoB: NoC: Yes. Tom give me rights to insert data into the table. D: No. Tom have to give me right to create procedure. GRANT CREATE PROCEDURE TO (user)

Page 19: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #71.A: subprograms, variables, cursors, exceptionsB: Package specification: It is interface for applications. It declares the constructs that are visible to the calling environment. Package body: Contains the executable code of subprograms or own variable declarationsC: Package specification must be created first because body contain executable code according to specification. D: SELECT owner,type FROM ALL_SOURCE WHERE type = 'PACKAGE'

2.CREATE OR REPLACE PACKAGE check_emp_pkg IS g_max_length_of_service CONSTANT NUMBER:= 100; PROCEDURE chk_hiredate (p_date IN employees.hire_date%TYPE); PROCEDURE chk_dept_mgr (p_empid IN employees.employee_id%TYPE, p_mgr IN employees.manager_id%TYPE);END check_emp_pkg;

3.

CREATE OR REPLACE PACKAGE BODY check_emp_pkg ASPROCEDURE chk_hiredate(p_date IN employees.hire_date%TYPE) ISBEGIN IF round(MONTHS_BETWEEN(to_date(sysdate,'DD-MON-YYYY'),to_date(p_date,'DD-MON-YYYY'))) > g_max_length_of_service * 12 THEN RAISE_APPLICATION_ERROR(-20001,'Employee was employed more than 100 years ago'); ELSE DBMS_OUTPUT.PUT_LINE('OK'); END IF;END chk_hiredate;

PROCEDURE chk_dept_mgr(p_empid IN employees.employee_id%TYPE, p_mgr IN employees.manager_id%TYPE) IS v_department_id departments.department_id%TYPE; v_manager_id departments.manager_id%TYPE;BEGIN SELECT department_id INTO v_department_id FROM employees WHERE employee_id = p_empid; SELECT manager_id INTO v_manager_id FROM departments WHERE department_id = v_department_id; IF v_manager_id = p_mgr THEN DBMS_OUTPUT.PUT_LINE('Employee_id is the same as Manager_id'); ELSE

Page 20: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

RAISE_APPLICATION_ERROR(-20002, 'Employee_id and Manager_id are not the same’); END IF;END chk_dept_mgr;END check_emp_pkg;

4.

BEGINcheck_emp_pkg.chk_hiredate(to_date('17-Jan-1987','DD-MON-YYYY')); END;(succeed)

(174,149) - succeed(174,176) - ORA-20002: Employee_id and Manager_id are not the same

5.

We have to change only package body, specifically the RAISE_APPLICATION_ERROR

6.

CREATE OR REPLACE PACKAGE overload IS PROCEDURE what_am_i(p_parameter IN VARCHAR2); PROCEDURE what_am_i(p_parameter IN NUMBER); PROCEDURE what_am_i(p_parameter IN DATE);END;

CREATE OR REPLACE PACKAGE BODY overload IS PROCEDURE what_am_i(p_parameter IN VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE('VARCHAR2'); END; PROCEDURE what_am_i(p_parameter IN NUMBER) IS BEGIN DBMS_OUTPUT.PUT_LINE('NUMBER'); END; PROCEDURE what_am_i(p_parameter IN DATE) IS BEGIN DBMS_OUTPUT.PUT_LINE('DATE'); END;END;

describe overload;

Page 21: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

7.

BEGIN overload.what_am_i('Simon'); overload.what_am_i(TO_DATE('30-MAY-2016')); overload.what_am_i(2016); END;

8.

CREATE OR REPLACE PACKAGE overload IS PROCEDURE what_am_i(p_parameter IN VARCHAR2); PROCEDURE what_am_i(p_parameter IN NUMBER); PROCEDURE what_am_i(p_parameter IN DATE); PROCEDURE what_am_i(p_in IN NUMBER, p_in2 IN VARCHAR2);END;

CREATE OR REPLACE PACKAGE BODY overload IS PROCEDURE what_am_i(p_parameter IN VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE('VARCHAR2'); END; PROCEDURE what_am_i(p_parameter IN NUMBER) IS BEGIN DBMS_OUTPUT.PUT_LINE('NUMBER'); END; PROCEDURE what_am_i(p_parameter IN DATE) IS BEGIN DBMS_OUTPUT.PUT_LINE('DATE'); END; PROCEDURE what_am_i(p_in IN NUMBER, p_in2 IN VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE('NUMBER and VARCHAR2'); END;END;

BEGIN overload.what_am_i('Simon'); overload.what_am_i(TO_DATE('30-MAY-2016')); overload.what_am_i(2016); END;(succeed)

9.Error at line 6: PL/SQL: Declaration ignoredSame declaration of procedures. 10. No it doesn’t.

Page 22: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

Homework Week #8

1.

Triggers allow specified actions to be performed automatically within the database, without have to write extra application code. Triggers increasing power of database and applications. Executes automatically whenever the associated action occurs. (Logging or auditing records, complex data integrity, to prevent invalid DML transactions, security)

2.

• DML operations on a table

• DML operations on a view, with an INSTEAD OF trigger

• DDL statements such as create and alter

• Database system events, such as when a user logs on or the DBA shuts down the

database

3.

All referential cascade actions and constraint checks must be checked and succeed before creating trigger.

4.

Timing, event, trigger_body

5.

A: OK

B: CREATE OR REPLACE TRIGGER emp_tgr AFTER INSERT ON employees_dup BEGIN INSERT INTO audit_table(action,user_name, last_change_date ) VALUES('INSERT',USER, SYSTIMESTAMP); END;C:

INSERT INTO employees_dup VALUES(103, 'Simon', 'Kacmar', 'email', '516.1234.4568', TO_DATE('30/May/1996'), 'AD_PRES', 30000, .4, 103, 95);SELECT * FROM audit_table

Page 23: akela.mendelu.czxkacmar/du_pl/kacmar_home…  · Web viewPL/SQL overcomes this limits, that the basic program logic can be combined with SQL statements. 3. ... The key word BEGIN

D:

DELETE FROM employees_dup

WHERE first_name = 'Simon'

1 row(s) deleted.Nothing is inserted into audit_table

6.

Row Level TriggerRow Level Trigger is fired each time row is affected by Insert, Update or Delete command. If statement doesn’t affect any row, no trigger action happens.

Statement Level Triggertrigger fires when a SQL statement affects the rows of the table. The trigger activates and performs its activity irrespective of number of rows affected due to SQL statement.

7.

True – At least once for the triggering statement, no matter how many rows it affects

8.

BEFORE INSERT statement trigger – 0BEFORE UPDATE statement trigger - 1AFTER UPDATE row trigger - 3AFTER DELETE statement trigger - 0