SQL2 - Practice

Übungen zur Lehrveranstaltung Datenbanken / H.-G. Hopf

Practice 1: Overview

This practice covers the following topics: • Granting other users privileges to your table • Modifying another user’s table through the privileges granted to you • Creating a synonym • Querying the data dictionary views related to privileges

Copyright © 2004, Oracle. All rights reserved.

Practice 1: Overview

Team up with other students for this exercise about controlling access to database objects.

r O

cl a

In e

rn te

l& a

O

I A

s U

O e

ly n

Oracle Database 10g: SQL Fundamentals II 1-20

Practice 1 To complete questions 6 and higher, you will need to connect to the database using iSQL*Plus. To do this, launch the Internet Explorer browser from the desktop of your client. Enter the URL in the http://machinename:5561/isqlplus/ format and use the oraxx account and the corresponding password and service identifier (in the Tx format) provided by your instructor to log on to the database. 1. What privilege should a user be given to log on to the Oracle server? Is this a system or an object privilege? _____________________________________________________________________ 2. What privilege should a user be given to create tables? _____________________________________________________________________ 3. If you create a table, who can pass along privileges to other users on your table? _____________________________________________________________________ 4. You are the DBA. You are creating many users who require the same system privileges. What should you use to make your job easier? _____________________________________________________________________ 5. What command do you use to change your password? _____________________________________________________________________ 6. Grant another user access to your DEPARTMENTS table. Have the user grant you query access to his or her DEPARTMENTS table. 7. Query all the rows in your DEPARTMENTS table.

r O

cl a

In e

rn te

l& a

O

I A

s U

O e

ly n

Oracle Database 10g: SQL Fundamentals II 1-21

Practice 1 (continued) 8. Add a new row to your DEPARTMENTS table. Team 1 should add Education as department number 500. Team 2 should add Human Resources as department number 510. Query the other team’s table. 9. Create a synonym for the other team’s DEPARTMENTS table. 10. Query all the rows in the other team’s DEPARTMENTS table by using your synonym. Team 1 SELECT statement results:

Team 2 SELECT statement results:

r O

cl a

In e

rn te

l& a

O

I A

s U

O e

ly n

Oracle Database 10g: SQL Fundamentals II 1-22

Remove the row you inserted into the DEPARTMENTS table in step 8 and save the changes. Revoke the SELECT privilege from the other team. Note: Your list may not exactly match the list shown below. Query the ALL_TABLES data dictionary view to see information about all the tables that you can access. Query the USER_TABLES data dictionary to see information about the tables that you own. 14. 12.Practice 1 (continued) 11. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 1-23 . Exclude tables that you own. … 13.

Practice 2: Overview In this practice. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 2-39 . You drop columns and use the FLASHBACK operation. you use the ALTER TABLE command to modify columns and add constraints. You create external tables. You use the CREATE INDEX command to create indexes when creating a table. All rights reserved. Oracle. along with the CREATE TABLE command.Practice 2: Overview This practice covers the following topics: • Altering tables • Adding columns • Dropping columns • Creating indexes • Creating external tables Copyright © 2004.

Create the EMP2 table based on the following table instance chart.Practice 2 1.sql. Confirm that the table is created. and then execute the statement in the script to create the table. Column Name Key Type Nulls/Unique FK Table FK Column Data type Length NUMBER 7 VARCHAR2 25 ID NAME 2. Include only the columns that you need. Populate the DEPT2 table with data from the DEPARTMENTS table. Place the syntax in a script called lab_02_03. Column Name Key Type Nulls/Unique FK Table FK Column Data type Length NUMBER 7 VARCHAR2 25 ID LAST_NAME FIRST_NAME r O cl a In e rn te l& a O VARCHAR2 I A s U O e 7 DEPT_ID ly n NUMBER 25 Oracle Database 10g: SQL Fundamentals II 2-40 . and then execute the statement in the script to create the table. Place the syntax in a script called lab_02_01.sql. Confirm that the table is created. 3. Create the DEPT2 table based on the following table instance chart.

Confirm your modification. SALARY. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 2-41 . and DEPARTMENT_ID columns. Modify the EMP2 table to allow for longer employee last names. 12. The constraint should be named at creation. mark the DEPT_ID column as UNUSED. Add a table-level PRIMARY KEY constraint to the EMP2 table on the ID column. Name the constraint my_emp_id_pk. 8. Confirm that both the DEPT2 and EMP2 tables are stored in the data dictionary. FIRST_NAME. In the EMPLOYEES2 table. Confirm your modification by checking the description of the table. and DEPT_ID. Undrop the EMP2 table. 10. Confirm your modification by checking the description of the table. LAST_NAME. 13. Drop all the UNUSED columns from the EMPLOYEES2 table. Drop the EMP2 table. 9. (Hint: USER_TABLES) 6. Confirm your modification by checking the description of the table. respectively. Name the columns in your new table ID. LAST_NAME. Include only the EMPLOYEE_ID. Drop the FIRST_NAME column from the EMPLOYEES2 table. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table.Practice 2 (continued) 4. 7. SALARY. 5. FIRST_NAME. Query the recycle bin to see whether the table is present. 11.

16. Note the types and names of the constraints. 15. Confirm that the constraints were added by querying the USER_CONSTRAINTS view. Notice that the new tables and a new index were created. Name the index for the PRIMARY KEY column as DEPT_PK_IDX. Create the DEPT_NAMED_INDEX table based on the following table instance chart. Name the constraint my_dept_id_pk. Drop the EMP2 and DEPT2 tables so that they cannot be restored. complete the following exercise: 18. 20. Column Name Primary Key Data Type Length Deptno Yes Number 4 VARCHAR2 30 Dname r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 2-42 . Create a PRIMARY KEY constraint to the DEPT2 table using the ID column. The constraint should be named at creation. Name the constraint my_emp_dept_id_fk. scale 2. Modify the EMP2 table. Verify the recycle bin. Display the object names and types from the USER_OBJECTS data dictionary view for the EMP2 and DEPT2 tables. Add a constraint to the COMMISSION column that ensures that a commission value is greater than zero. 17. If you have time. Add a COMMISSION column of NUMBER data type. Add a foreign key reference on the EMP2 table that ensures that the employee is not assigned to a nonexistent department. precision 2.Practice 2 (continued) 14. 19.

Oracle. and track your transactions. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-36 . update and delete data from the table. you add rows to the emp_data table. All rights reserved.Practice 3: Overview This practice covers the following topics: • Performing multitable INSERTs • Performing MERGE operations • Tracking row versions Copyright © 2004. Practice 3: Overview In this practice.

Write a query to do the following: . . Run the lab_03_01. 7.sql script in the lab folder to create the MGR_HISTORY table. Display the structure of the SPECIAL_SAL table.sql script in the lab folder to create the SAL_HISTORY table. and salary into the SAL_HISTORY table. 2. . Display the structure of the MGR_HISTORY table. Run the lab_03_03.Insert the details of the employee ID.000. 5.Insert the details of employee ID. hire date.Retrieve the details of the employee ID. manager ID.Practice 3 1. and manager ID of those employees whose employee ID is less than 125 from the EMPLOYEES table. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-37 .sql script in the lab folder to create the SPECIAL_SAL table. . 4. a. hire date. Run the lab_03_05. insert the details of employee ID and salary into the SPECIAL_SAL table. 6. salary. and salary into the MGR_HISTORY table. Display the structure of the SAL_HISTORY table. 3.If the salary is more than $20.

r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-38 . Display the records from the SAL_HISTORY table. Display the records from the SPECIAL_SAL table. c.Practice 3 (continued) b.

Practice 3 (continued) d. Display the records from the MGR_HISTORY table. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-39 .

f. Run the lab_03_08a. e. a. Display the records from the SALES_SOURCE_DATA table. d.sql script in the lab folder to create the SALES_INFO table. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-40 . Run the lab_03_08c. Display the structure of the SALES_SOURCE_DATA table. c.sql script in the lab folder to create the SALES_SOURCE_DATA table. Run the lab_03_08b. Display the structure of the SALES_INFO table. b.sql script in the lab folder to insert records into the SALES_SOURCE_DATA table.Practice 3 (continued) 8.

b. week ID. 9. Rows are considered matching when the employee’s first and last name are identical. Display the records from the SALES_INFO table. Write a query to do the following: Retrieve the details of employee ID.dat source file in the emp_dir directory. If a row in the EMP_DATA table matches the EMP_HIST table. and sales on Friday from the SALES_SOURCE_DATA table.data.sql to do this. If a row in the EMP_DATA table does not match. h. Retrieve the rows from EMP_HIST after the merge. insert it into the EMP_HIST table. Assume that the data in the external EMP_DATA table is the most up-to-date. Hint: Use a pivoting INSERT statement. first create an external table called EMP_DATA using the emp. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-41 . You want to store the names and e-mail IDs of all employees past and present in a table. update the e-mail column of the EMP_HIST table to match the EMP_DATA table row. Next. Build a transformation such that each record retrieved from the SALES_SOURCE_DATA table is converted into multiple records for the SALES_INFO table. To do this.sql script to create the EMP_HIST table. sales on Monday. sales on Thursday. sales on Wednesday. run the lab_03_10. 10. c. Merge the data in the EMP_DATA table created in the last lab into the data in the EMP_HIST table. You can use the script in lab_03_09.Practice 3 (continued) g. Increase the size of the e-mail column to 45. a. You have the data of past employees stored in a flat file called emp. sales on Tuesday.

Track the changes to Kochhar using the Row Versions feature.Practice 3 (continued) … 11. change the department for Kochhar to 50 and commit your change. Create table EMP3 using the lab_03_11. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 3-42 . In the EMP3 table change the department for Kochhar to 60 and commit your change. Next.sql script.

You will also use GROUPING SETS. All rights reserved.Practice 4: Overview This practice covers using: • ROLLUP operators • CUBE operators • GROUPING functions • GROUPING SETS Copyright © 2004. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-24 . Oracle. Practice 4: Overview In this practice. you use the ROLLUP and CUBE operators as extensions of the GROUP BY clause.

Job ID and total salary for every job ID for employees who report to the same manager .Total salary of those managers. irrespective of the job IDs … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-25 . Write a query to display the following for those employees whose manager ID is less than 120: .Total salary of those managers .Manager ID .Practice 4 1.

Write a query using the GROUPING function to determine whether the NULL values in the columns corresponding to the GROUP BY expressions are caused by the ROLLUP operation.Practice 4 (continued) 2. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-26 . Observe the output from question 1.

Cross-tabulation values to display the total salary for every job.Job and total salaries for every job for employees who report to the same manager . irrespective of the manager .Practice 4 (continued) 3. Write a query to display the following for those employees whose manager ID is less than 120: .Total salary of those managers .Manager ID .Total salary irrespective of all job titles … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-27 .

Practice 4 (continued) 4. Observe the output from question 3. Write a query using the GROUPING function to determine whether the NULL values in the columns corresponding to the GROUP BY expressions are caused by the CUBE operation. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-28 .

Using GROUPING SETS.department_id. manager_id.manager_id.Practice 4 (continued) 5. job_id . job_id . … … … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 4-29 . write a query to display the following groupings: .department_id. job_id The query should calculate the sum of the salaries for each of these groups.

You also set time zones and use the EXTRACT function. and the LOCALTIMESTAMP. you display time zone offsets. Oracle. Copyright © 2004. CURRENT_TIMESTAMP. Practice 5: Overview In this practice. CURRENT_DATE. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 5-35 .Practice 5: Overview This practice covers using the datetime functions. All rights reserved.

Write a query to display the DBTIMEZONE and SESSIONTIMEZONE. d. and LOCALTIMESTAMP are all sensitive to the session time zone.US/Pacific-New - Singapore - Egypt b. 2. a. and LOCALTIMESTAMP for this session. and LOCALTIMESTAMP for this session. Note: Observe in the preceding practice that CURRENT_DATE. e. c. Alter the session to set the TIME_ZONE parameter value to the time zone offset of US/Pacific-New. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 5-36 . based on the date when the command is executed. 3.Practice 5 1. . CURRENT_TIMESTAMP. CURRENT_TIMESTAMP. Display the CURRENT_DATE. Write queries to display the time zone offsets (TZ_OFFSET). Note: The output might be different. CURRENT_TIMESTAMP. Alter the session to set the NLS_DATE_FORMAT to DD-MON-YYYY HH24:MI:SS. for the following time zones. Alter the session to set the TIME_ZONE parameter value to the time zone offset of Singapore. Display the CURRENT_DATE.

Write a query to extract the YEAR from the HIRE_DATE column of the EMPLOYEES table for those employees who work in department 80. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 5-37 . Alter the session to set NLS_DATE_FORMAT to DD-MON-YYYY.Practice 5 (continued) 4. … 5.

Try to modify the data type of the DATE_COL column and change it to TIMESTAMP WITH TIME ZONE. otherwise display not this year! Name the review status column Review.sql to create the SAMPLE_DATES table and populate it. What happens? 7. display Needs Review for the review status. a.Practice 5 (continued) 6. Sort the results by the HIRE_DATE column. Select from the table to view the data. Select from the table and view the data. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 5-38 . If the year hired was 2000. c. Create a query to retrieve last names from the EMPLOYEES table and calculate review status. Examine and run script lab_05_06. Modify the data type of the DATE_COL column and change it to TIMESTAMP. Hint: Use a CASE expression with EXTRACT function to calculate the review status. b.

Practice 5 (continued) 8. then print maybe next year! Sort the results by the HIRE_DATE column. If the employee has been employed 15 or more years. then print 10 years of service. Create a query to print the last names and the number of years of service for each employee. If none of these conditions match. then print 5 years of service. If the employee has been employed five or more years. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 5-39 . If the employee has been employed 10 or more years. Hint: Use CASE expressions and TO_YMINTERVAL. Use EMPLOYEES table. then print 15 years of service.

Practice 6: Overview In this practice. You also solve problems by writing the WITH clause. Oracle. you write multiple-column subqueries. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-27 . All rights reserved.Practice 6: Overview This practice covers the following topics: • Creating multiple-column subqueries • Writing correlated subqueries • Using the EXISTS operator • • Using scalar subqueries Using the WITH clause Copyright © 2004. and correlated and scalar subqueries.

Create a query to display the employees who earn a salary that is higher than the salary of all of the sales managers (JOB_ID = 'SA_MAN'). … … 2. 4. Note: Do not display Kochhar in the result set. Display the last name. and salary of any employee whose salary and commission match the salary and commission of any employee located in location ID 1700. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-28 . department number. Sort the results on salary from highest to lowest. Write a query to display the last name. and salary for all employees who have the same salary and commission as Kochhar.Practice 6 1. Create a query to display the last name. 3. and salary of any employee whose department number and salary both match the department number and salary of any employee who earns a commission. hire date. department name.

6.Practice 6 (continued) 5. department ID. Display the details of the employee ID. Write a query to find all employees who earn more than the average salary in their departments. and department ID of those employees who live in cities whose name begins with T. Use aliases for the columns retrieved by the query as shown in the sample output. salary. and the average salary for the department. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-29 . Display last name. Sort by average salary. last name.

Find all employees who are not supervisors. a. Write a query to display the last names of the employees who earn less than the average salary in their departments. or why not? 8. First do this using the NOT EXISTS operator. … b. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-30 .Practice 6 (continued) 7. Can this be done by using the NOT IN operator? How.

… 10. Note: Use a scalar subquery to retrieve the department name in the SELECT statement. Write a query to display the last names of the employees who have one or more coworkers in their departments with later hire dates but higher salaries.Practice 6 (continued) 9. last names. Write a query to display the employee ID. and department names of all employees. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-31 .

Practice 6 (continued) 11. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 6-32 . Use the WITH clause to write this query. Name the query SUMMARY. Write a query to display the department names of those departments whose total salary cost is above one-eighth (1/8) of the total salary cost of the whole company.

you gain practical experience in producing hierarchical reports.Practice 7: Overview This practice covers the following topics: • Distinguishing hierarchical queries from nonhierarchical queries • Walking through a tree • Producing an indented report by using the LEVEL pseudocolumn • Pruning the tree structure • Sorting the output Copyright © 2004. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 7-15 . Oracle. All rights reserved. Note: Question 1 is a paper-based question. Practice 7: Overview In this practice.

Practice 7 1. Look at the following output examples. Exhibit 1: … Exhibit 2: r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 7-16 . Are they the result of a hierarchical query? Explain why or why not.

salaries.Practice 7 (continued) Exhibit 3: 2. Produce a report showing an organization chart for Mourgos’s department. 3. Display his immediate manager first. Create a report that shows the hierarchy of the managers for the employee Lorentz. Print last names. and department IDs. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 7-17 .

Print the employee’s last name.Practice 7 (continued) 4. and exclude De Haan and those employees who report to De Haan. Give alias names to the columns as shown in the sample output. Create an indented report showing the management hierarchy starting from the employee whose LAST_NAME is Kochhar. manager ID. exclude all people with a job ID of IT_PROG. and department ID. Start with the person at the top level. complete the following exercise: 5. Produce a company organization chart that shows the management hierarchy. … r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 7-18 . If you have time.

Practice 8: Overview This practices covers searching and replacing data using regular expressions. All rights reserved. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 8-15 .Practice 8: Overview This practice covers using regular expressions. Oracle. Copyright © 2004.

Write a query to search the EMPLOYEES table for all employees whose first names start with “Ne” or “Na.Practice 8 1.” 2. Create a query that removes the spaces in the STREET_ADDRESS column of LOCATIONS table in the display. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 8-16 .

Be careful that you do not affect any rows that already have “Street” in them. r O cl a In e rn te l& a O I A s U O e ly n Oracle Database 10g: SQL Fundamentals II 8-17 .Practice 8 (continued) 3. Display only those rows which are affected. Create a query that displays “St” replaced by “Street” in the STREET_ADDRESS column of LOCATIONS table.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.