Solutions Chapter 14: Security Test Your Thinking

To complete the exercises below, create a new user called SCHOOL with the password PROGRAM, and grant CONNECT and RESOURCE privileges to it. Then logon as the STUDENT user.

Solution:

-- Create SCHOOL user
CONN SYSTEM/MANAGER
CREATE USER school IDENTIFIED BY program;
GRANT CONNECT, RESOURCE TO school;
  1. Create two roles: one called REGISTRAR, the other called INSTRUCTOR.

    Solution:

    CONN student/learn
    CREATE ROLE registrar;
    CREATE ROLE instructor;
    
  2. Create a view called CURRENT_REGS reflecting all students that registered today. Grant SELECT privileges on the new view to the REGISTRAR role.

    Solution:

    
    CREATE OR REPLACE VIEW current_regs AS
    SELECT first_name, last_name
      FROM student
     WHERE TRUNC(registration_date) = TRUNC(SYSDATE);
    
    GRANT SELECT ON current_regs TO registrar;
    
  3. Create a view called roster reflecting all students taught by the instructor Marilyn Frantzen. Grant SELECT privileges on the new view to the INSTRUCTOR role.

    Solution:

    CREATE OR REPLACE VIEW roster AS
    SELECT se.course_no course, se.section_id section,
           s.first_name first, s.last_name last,
           e.student_id
      FROM student s, enrollment e, section se, instructor i
     WHERE s.student_id = e.student_id
       AND e.section_id = se.section_id
       AND se.instructor_id = i.instructor_id
       AND i.first_name = 'Marilyn'
       AND i.last_name = 'Frantzen';
    
    GRANT SELECT ON roster TO instructor;
    
  4. Grant the REGISTRAR and INSTRUCTOR roles to the new user SCHOOL.

    Solution:

    GRANT registrar, instructor TO school;
  5. Start a SQL*Plus session as the new user SCHOOL, and select from the two views created previously.

    Solution:

    CONNECT school/program
    
    SELECT *
      FROM student.current_regs;
    
    SELECT *
      FROM student.roster;
    

    The output from the first SELECT statement returns 0 rows. The second query results in 31 rows.

Select Another Chapter

  1. SQL and Data
  2. SQL: The Basics
  3. Character and Number Functions
  4. Date, Conversion, and Miscellaneous Functions
  5. Aggregate Functions, GROUP BY, and HAVING
  6. Equijoins
  7. Subqueries
  8. Set Operators
  9. Complex Joins
  10. Insert, Update, and Delete
  11. Create, Alter, and Drop Tables
  12. Views, Indexes, and Sequences
  13. The Data Dictionary and Dynamic SQL Scripts
  14. Security
  15. Advanced SQL Queries
  16. SQL Optimization