You are on page 1of 4

Week 10 Final Submissions From Class

I believe this is what you are asking Professor, its out of Fundamentals I Volume I Lesson 10. It has to do with views, to me it was a tricky subject. Department 40 needs access to its employee data. Create a view named DEPT40 that contains the employee numbers, employee last names, and department numbers for all employees in department 40. They have requested that you label the view columns EMPNO, EMPLOYEE, and DEPTNO. For security purposes, do not allow an employee to be reassigned to another department through the view. Answer: CREATE VIEW dept40 AS SELECT employee_id empno, last_name employee, department_id deptno FROM employees WHERE department_id = 40 WITH CHECK OPTION CONSTRAINT emp_dept_40; Output: SQL> CREATE VIEW dept40 AS 2 SELECT employee_id empno, last_name employee, department_id deptno 3 FROM employees 4 WHERE department_id = 40 5 WITH CHECK OPTION CONSTRAINT emp_dept_40; View created.

Problem: Produce a list of jobs for departments 10, 50, and 20, in that order. Display job ID and department ID using set operators. Solution: 1. SELECT job_id, department_id FROM employees WHERE department_id = 10 UNION SELECT job_id, department_id

FROM employees WHERE department_id = 50 UNION SELECT job_id, department_id FROM employees WHERE department_id = 20 ORDER BY department_id;

Problem: The HR department needs a report with the following specifications: Last name and department ID of all the employees from the EMPOLYEES table, regardless of whether or not they belong to a department Department ID and department name of all the departments from the DEPARTMENTS table, regardless of whether or not they have employees working in them Where a compound query to accomplish this: Solution: SELECT last_name, department_id, TO_CHAR (null) FROM employees UNION SELECT TO_CHAR (null), department_id, department_name FROM departments;

A user of a database wants to see if there are any tables that have not been used recently.

Create a query that will show the object name and the date and time of the last action that the user preformed for all tables they have access to. SELECT object_name, timestamp FROM ALL_OBJECTS WHERE object_type = 'TABLE' ORDER BY last_ddl_time;

I think the grouping functions used in Fundamentals II, Volume 1, exercise 4 were very useful. Here is my example: Write a query to display the jobid and salary for employees that report to manager 100. Total the salary for the manager and for the jobids. select manager_id, job_id, sum(salary) from employees where manager_id = 100 group by rollup(manager_id, job_id); MANAGER_ID JOB_ID SUM(SALARY) ----------------------------100 AD_VP 34000 100 MK_MAN 13000 100 PU_MAN 11000 100 SA_MAN 61000 100 ST_MAN 36400 100 155400 155400 7 rows selected.

One of the lessons we learned which i believed was one that would be used alot (atleast when it concerns relational databases) would be the section on primary and foreign key designations. One example being: Add a foreign key reference on the EMP2 table that ensures that the employee is not assigned to a nonexistent department. Name the constraint my_emp_dept_id_fk. SQL> ALTER TABLE emp2 2 ADD CONSTRAINT my_emp_dept_id_fk 3 FOREIGN KEY (dept_id) REFERENCES dept2 (id);

Question: Write a query to display the number of people with the same job. Solution: SELECT job_id, COUNT(*) FROM employees GROUP BY job_id;

CREATE TABLE web_orders (ord_id number primary key, order_date TIMESTAMP WITH TIME_ZONE) ; INSERT INTO web orders values (ord_seq.nextval, current_dates) ; SELECT * FROM web_orders ;

You might also like