Top Banner
Q2-1 A view is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used. For example, if we frequently issue the following query SELECT empid FROM emp; I might well want to make this a view (the reality is that we would probably never create a view for a statement this simple but we wanted to use an easy example). To create a view use the create view command as seen in this example CREATE VIEW view_empASSELECT empid FROM emp; Q2-2 Benefits of Oracle Views Oracle views offer some compelling benefits. These include: * Commonality of code being used. Since a view is based on one common set of SQL, this means that when it is called it's less likely to require parsing. This is because the basic underlying SQL that is called is always the same. However, since you can add additional where clauses when calling a view, you still need to use bind variables. Additional where clauses without a bind variable can still cause a hard parse! * Security. Views have long been used to hide the tables that actually contain the data you are querying. Also, views can be used to restrict the columns that a given user has access to. Using views for security on less complex databases is probably not a bad thing. As databases become more complex, this solution becomes harder to scale and other solutions will be needed. * Predicate pushing. Oracle supports pushing of predicates into a given view.
15
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: cs457k_hw#8_12538_manoj

Q2-1

A view is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used. For example, if we frequently issue the following query

SELECT empid FROM emp;

I might well want to make this a view (the reality is that we would probably never create a view for a statement this simple but we wanted to use an easy example).

To create a view use the create view command as seen in this example

CREATE VIEW view_empASSELECT empid FROM emp;

Q2-2

Benefits of Oracle Views

Oracle views offer some compelling benefits. These include:

* Commonality of code being used. Since a view is based on one common set of SQL, this means that when it is called it's less likely to require parsing. This is because the basic underlying SQL that is called is always the same. However, since you can add additional where clauses when calling a view, you still need to use bind variables. Additional where clauses without a bind variable can still cause a hard parse!

* Security. Views have long been used to hide the tables that actually contain the data you are querying. Also, views can be used to restrict the columns that a given user has access to. Using views for security on less complex databases is probably not a bad thing. As databases become more complex, this solution becomes harder to scale and other solutions will be needed.

* Predicate pushing. Oracle supports pushing of predicates into a given view. 

Q2-3

Rules for performing DML Operations on a view:

•You can perform DML operations on Simple views.•You cannot always perform DML through Complex views.•You cannot delete a row if the view contains the following:

Group functions such as SUM, MIN, MAX, AVG and ......A GROUP BY clause.The DISTINCT keyword.

Page 2: cs457k_hw#8_12538_manoj

• You cannot update data in a view if it contains,

Group functions such as SUM, MIN, MAX, AVG and ......A GROUP BY clause.The DISTINCT keyword.Columns defined by expressions such as SALARY*1.2The ROWNUM pseudo column.

• You cannot insert data in a view if it contains,

Group functions such as SUM, MIN, MAX, AVG and ......A GROUP BY clause.The DISTINCT keyword.Columns defined by expressions such as SALARY*1.2The ROWNUM pseudo column.There are NOT NULL columns in the base tables that are not selected by the view.

Q2-4

An inline view is a SELECT statement in the FROM-clause of another SELECT statement. In-line views

are commonly used to simplify complex queries by removing join operations and condensing several

separate queries into a single query.

This feature is commonly referred to in the MSSQL community as a derived table, and in the Postgres

community simply refers to it as a subselect (subselects are inline views + subqueries in Oracle

nomenclature).

Q2-5

Q2-6

A sequence is a schema object that can generate unique sequential values. These values are often used for primary and unique keys. You can refer to sequence values in SQL statements with these pseudocolumns:

CURRVAL: Returns the current value of a sequence NEXTVAL: Increments the sequence and returns the next value

You must qualify CURRVAL and NEXTVAL with the name of the sequence:

sequence.CURRVALsequence.NEXTVAL

To refer to the current or next value of a sequence in the schema of another user, you must have been granted either SELECT object privilege on the sequence or SELECT ANY SEQUENCE system privilege, and you must qualify the sequence with the schema containing it:

Page 3: cs457k_hw#8_12538_manoj

schema.sequence.CURRVALschema.sequence.NEXTVAL

To refer to the value of a sequence on a remote database, you must qualify the sequence with a complete or partial name of a database link:

[email protected]@dblink

Q2-7

An index is a schema object that contains an entry for each value that appears in the indexed column(s) of the table or cluster and provides direct, fast access to rows. Oracle Database supports several types of index:

Normal indexes. (By default, Oracle Database creates B-tree indexes.) Bitmap indexes, which store rowids associated with a key value as a bitmap Partitioned indexes, which consist of partitions containing an entry for each

value that appears in the indexed column(s) of the table Function-based indexes, which are based on expressions. They enable you to

construct queries that evaluate the value returned by an expression, which in turn may include built-in or user-defined functions.

Domain indexes, which are instances of an application-specific index of type indextype

Q2-8

Use the CREATE INDEX statement to create an index on:

One or more columns of a table, a partitioned table, an index-organized table, or a cluster

One or more scalar typed object attributes of a table or a cluster A nested table storage table for indexing a nested table column

Q2-9

A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table,

Page 4: cs457k_hw#8_12538_manoj

providing the basis for both rapid random lookups and efficient access of ordered records.

Q2-10

First, they consume space. This may be inconsequential, but if your table is particularly large, it may have an impact.

Second, and more importantly, you need to remember that indexes jave a performance penalty when it comes to INSERTing new rows, DELETEing old ones or UPDATEing existing values of the indexed column, as now the DML statement need not only modify the table's data, but the index's one too. Once again, this depends largely on your application usecase. If DMLs are so rare that the performance is a non-issue, this may not be a consideration.

Third (although this ties in storngly to my first point), remember that each time you create an another database object, you are creating an additional maintenance overhead - it's another index you'd have to occasionally rebuild, collect statistics for (depending on the RDBMS you're using, of course), another objet to clatter the data dictionary, etc.

Screen shots:

1.Write a SQL statement to create a logically represent subset (Emp_MKT) of data that contains details of employee in the department 20. Then query the view and how many rows are displayed? (5 Points)

CREATE VIEW Emp_MKT AS SELECT EMPLOyee_ID, FIRST_NAME, LAST_NAME, JOB_ID from employee where department_id=20;

Page 5: cs457k_hw#8_12538_manoj

2. Write a SQL statement to create a logically represent subset (Emp_Sales) of data by using column aliases in the sub-query that contain columns (Employee ID, First Name and Annual Salary) and aliases name as (ID, Name, Annual_Salary) of employee in the department 50. The query the view and how many rows are displayed? (5 Points)

CREATE VIEW Emp_SALES AS SELECT employee_id id,first_name,salary*12 annual_ salary from employee where department_id=50;

Page 6: cs457k_hw#8_12538_manoj

3. Write a SQL statement to modify a logically represent subset (Emp_Sales) by combine column two columns “First Name” and “Last Name” with space in between into “Name” column, other condition are remain as same. Then query the view and how many rows are displayed? (5 Points)

CREATE OR REPLACE VIEW Emp_SALES (ID, NAME, ANNUAL_SALARY) AS SELECT EMPLOYEE_ID,FIRST_NAME ||' ' || LAST_NAME, SALARY*12 FROM EMPLOYEE WHERE DEPARTMENT_ID=50;

Page 7: cs457k_hw#8_12538_manoj

4. Write a SQL statement to create a complex view that display department name and group functions as MIN (), MAX (), AVG () by using column salary from two tables (employee & department). Then query the view and how many rows are displayed? (5 Points)

CREATE VIEW complex_dept (DEPT_NAME, MINSAL, MAXSAL, AVGSAL) AS SELECT D.DEPARTMENT_NAME, MIN (E.SALARY), MAX (E.SALARY), AVG (E.SALARY)

2 FROM EMPLOYEE E, DEPARTMENT D WHERE E.DEPARTMENT_ID=D.DEPARTMENT_ID

Page 8: cs457k_hw#8_12538_manoj

3 GROUP BY D.DEPARTMENT_NAME;

5. Write a SQL statement that denying DML operations for Emp_Sales. (5 Points)

create or replace view emp_sales (ID, NAME, ANNUAL_SALARY) AS SELECT EMPLOYEE_ID,FIRST_NAME ||' ' || LAST_NAME, SALARY*12 FROM EMPLOYEE WHERE DEPARTMENT_ID=50 with read only;

Page 9: cs457k_hw#8_12538_manoj

6. Write a SQL statement that show top 5 salary employees. The display columns please follow as No., Name and Salary. (Name Column please display both first name and last name and a space in between) (5 Points)

SELECT ROWNUM AS NO, NAME, SALARY FROM (SELECT FIRST_NAME ||' '|| LAST_NAME "NAME", SALARY FROM EMPLOYEE ORDER BY SALARY DESC) WHERE ROWNUM <=5;

Page 10: cs457k_hw#8_12538_manoj

7. Write a SQL statement to create a sequence that name as DEPT_DEPTID_SEQ, start with number 1, every time increase 1, maximize number 999999, and never recycle the number? (5 Points)

CREATE SEQUENCE DEPT_DEPTID_SEQ INCREMENT BY 1 START WITH 1 MAXVALUE 999999 NOCACHE NOCYCLE;

Page 11: cs457k_hw#8_12538_manoj

8. Write a SQL statement that display all sequences created by you and their minimize number, maximize number, incremental number and last number write to disk? (5 Points)

SELECT SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY, LAST_NUMBER FROM USER_SEQUENCES;

Page 12: cs457k_hw#8_12538_manoj

9. Write a SQL statement that gets the sequence name DEPT_DEPTID_SEQ next value and current value? (5 Points)

SELECT DEPT_DEPTID_SEQ.NEXTVAL, DEPT_DEPTID_SEQ.CURRVAL FROM DUAL;

Page 13: cs457k_hw#8_12538_manoj

10.Write a SQL statement that change the sequence name DEPT_DEPTID_SEQ increase value to 2 and maximize number to 99999999? (5 Points)

SELECT SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY, LAST_NUMBER FROM USER_SEQUENCES;

Page 14: cs457k_hw#8_12538_manoj