Oracle Lesson3
Oracle Lesson3
Oracle Lesson3
1. DELETE FROM students; This SQL statement will: A. Not execute due to wrong syntax B. Delete the first row from STUDENTS C. Delete all rows from STUDENTS D. None of the above
3. Write a SQL statement to create the above table. CREATE TABLE grocery_items (product_id NUMBER(3) PRIMARY KEY, brand VARCHAR2(20), description VARCHAR2(20));
4. Write and execute three SQL statements to explicitly add the above data to the table.
INSERT INTO grocery_items (product_id, brand, description) VALUES (110,'Colgate','Toothpaste'); INSERT INTO grocery_items (product_id, brand, description) VALUES (111,'Ivory','Soap'); INSERT INTO grocery_items (product_id, brand, description) VALUES (112,'Heinz','Ketchup');
5. Write and execute a SQL statement that will explicitly add your favorite beverage to the table. INSERT INTO grocery_items (product_id, brand, description) VALUES (108, Chocolate Milk , Milk );
6. Write and execute a SQL statement that modifies the description for Heinz ketchup to tomato catsup . UPDATE grocery_items SET description = 'tomato catsup' WHERE product_id = 112;
7. Write and execute a SQL statement that will implicitly add your favorite candy to the table. INSERT INTO grocery_items VALUES (100, Caramilk , Candy Bar );
8. Write and execute a SQL statement that changes the soap brand from Ivory to Dove. UPDATE grocery_items SET brand = 'Dove' WHERE product_id = 111;
9. Write and execute SQL statements to create the new_items table and populate it with the data in the table. CREATE TABLE new_items (product_id NUMBER(3) PRIMARY KEY, brand VARCHAR2(20), description VARCHAR2(20)); INSERT INTO new_items (product_id, brand, description) VALUES (110,'Colgate','Dental paste'); INSERT INTO new_items (product_id, brand, description) VALUES (175,'Dew','Soda'); INSERT INTO new_items (product_id, brand, description) VALUES (275,'Palmolive','Dish detergent');
10. Write a SQL statement that will update the grocery_items table with the brand and description from the new_items table when the product ID values match. If they don t match, add a new row to the grocery_items table. DO NOT EXECUTE YOUR STATEMENT YET. MERGE INTO grocery_items g USING new_items i ON (g.product_id = i.product_id) WHEN MATCHED THEN UPDATE SET g.brand = i.brand, g.description = i.description WHEN NOT MATCHED THEN INSERT VALUES(i.product_id, i.brand, i.description);
11. How many rows will be updated by the SQL statement in question 10? ____1 row____________________?
12. How many rows will be inserted by the SQL statement in question 10? __________2 rows______________?
13. Which of the following is true about the SQL statement in question 10? A. new_items is the source table and grocery_items is the target table.
14. Execute the SQL statement you wrote in question 10, then SELECT all data from the target table to verify your answers to questions 11 and 12.
SELECT * FROM grocery_items; SECTION 3 LESSON 2 - Retrieving Data in PL/SQL 1. State whether each of the following SQL statements can be included directly in a PL/SQL block.
2. Create a PL/SQL block that selects the maximum department_id in the departments table and stores it in the v_max_deptno variable. Display the maximum department_id. A. Declare a variable v_max_deptno of the same datatype as the department_id column. B. Start the executable section with the keyword BEGIN and include a SELECT statement to retrieve the highest department_id from the departments table. C. Display the variable v_max_deptno and end the executable block. D. Execute your block. DECLARE v_max_deptno departments.department_id%TYPE; BEGIN SELECT MAX(department_id) INTO v_max_deptno FROM departments; DBMS_OUTPUT.PUT_LINE('The max department_id is : ' || v_max_deptno); END;
3. The following code is supposed to display the lowest and highest elevations for a country name entered by the user. However, the code does not work. Fix the code by following the guidelines for retrieving data that you learned in this lesson.
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('Lowest elevation is '||v_country_name||' is '||v_lowest_elevation ||' and the highest elevation is '|| v_highest_elevation||'.'); END;
4. Enter and run the following anonymous block, observing that it executes successfully. DECLARE v_emp_lname employees.last_name%TYPE; v_emp_salary employees.salary%TYPE; BEGIN SELECT last_name, salary INTO v_emp_lname, v_emp_salary FROM employees WHERE job_id = 'AD_PRES'; DBMS_OUTPUT.PUT_LINE(v_emp_lname||' '||v_emp_salary); END;
A. Now modify the block to use IT_PROG instead of AD_PRES and re-run it. Why does it fail this time? B. Now modify the block to use IT_PRAG instead of IT_PROG and re-run it. Why does it still fail?
5. Use the following code to answer this question: DECLARE last_name VARCHAR2(25) := 'Fay'; BEGIN UPDATE emp_dup SET first_name = 'Jennifer' WHERE last_name = last_name; END; What do you think would happen if you ran the above code? Write your answer here and then follow the steps below to test your theory. A. Create a table called emp_dup that is a duplicate of employees. B. Select the first_name and last_name values for all rows in emp_dup. C. Run the above anonymous PLSQL block. D. Select the first_name and last_name columns from emp_dup again to confirm your theory. E. Now we are going to correct the code so that it changes only the first name for the employee whose last name is Chen . Drop emp_dup and re-create it. F. Modify the code so that for the employee whose last_name= Fay , the first_name is updated to Jennifer. Run your modified block. G. Confirm that your update statement worked correctly.
_______False________?
4. SQL%FOUND, SQL%NOTFOUND, and SQL%ROWCOUNT are ___, and are available when you use _______________ cursors. _______________? _______________?
The following questions use a copy of the departments table. Execute the following SQL statement to create the copy table. CREATE TABLE new_depts AS SELECT * FROM departments;
5. Examine and run the following PL/SQL code, which obtains and displays the maximum department_id from new_depts.
DECLARE v_max_deptno new_depts.department_id%TYPE; BEGIN SELECT MAX(department_id) INTO v_max_deptno FROM new_depts; DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno); END;
6. Modify the code to declare two additional variables, (assigning a new department name to one of them) by adding the following two lines to your Declaration section: v_dept_name new_depts.department_name%TYPE := 'A New Department' ; v_dept_id new_depts.department_id%TYPE;
7. Modify the code to add 10 to the current maximum department number and assign the result to v_dept_id.
v_dept_id := v_max_deptno+10;
8. Modify the code to include an INSERT statement to insert a new row into the new_depts table, using v_dept_id and v_dept_name to populate the department_id and department_name columns. Insert NULL into the location_id and manager_id columns. Save your code. DECLARE v_max_deptno new_depts.department_id%TYPE; v_dept_name new_depts.department_name%TYPE := 'A New Department' ; v_dept_id new_depts.department_id%TYPE; BEGIN SELECT MAX(department_id) INTO v_max_deptno FROM new_depts; v_dept_id := v_max_deptno + 10; INSERT INTO new_depts (department_id, department_name) VALUES (v_dept_id, v_dept_name); DBMS_OUTPUT.PUT_LINE('The max department id is: '|| v_max_deptno); END;
9. Execute the block and check that the new row has been inserted.
11. Now modify the block, removing the INSERT statement and adding a statement that will UPDATE all rows with location_id = 1700 to location_id = 1400. Execute the block again to see how many rows were updated.
DECLARE v_max_deptno new_depts.department_id%TYPE; v_dept_name new_depts.department_name%TYPE := 'A New Department' ; v_dept_id new_depts.department_id%TYPE; BEGIN SELECT MAX(department_id) INTO v_max_deptno FROM new_depts; UPDATE new_depts SET location_id = 1400 WHERE location_id = 1700; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows affected.'); DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno); END;
______________?
2. Create the endangered_species table by running the following statement in Application Express: CREATE TABLE endangered_species (species_id NUMBER(4) CONSTRAINT es_spec_pk PRIMARY KEY, common_name VARCHAR2(30)
CONSTRAINT es_com_name_nn NOT NULL, scientific_name VARCHAR2(30) CONSTRAINT es_sci_name_nn NOT NULL);
3. Examine the following block. If you were to run this block, what data do you think would be saved in the database? BEGIN INSERT INTO endangered_species VALUES (100, 'Polar Bear','Ursus maritimus'); SAVEPOINT sp_100; INSERT INTO endangered_species VALUES (200, 'Spotted Owl','Strix occidentalis'); SAVEPOINT sp_200; INSERT INTO endangered_species VALUES (300, 'Asiatic Black Bear','Ursus thibetanus'); ROLLBACK TO sp_100; COMMIT; END; One row, 100 Polar Bears
4. Run the block to test your theory. Select from the table to confirm the result.
5. Examine the following block. If you were to run this block, what data do you think would be saved in the database?
BEGIN INSERT INTO endangered_species VALUES (400, 'Blue Gound Beetle','Carabus intricatus'); SAVEPOINT sp_400; INSERT INTO endangered_species VALUES (500, 'Little Spotted Cat','Leopardus tigrinus'); ROLLBACK; INSERT INTO endangered_species VALUES (600, 'Veined Tongue-Fern','Elaphoglossum nervosum'); ROLLBACK TO sp_400; END;
____No data would be saved, the roll back statement would reverse the additions in the database ?
Tis true.