DBMS Experiment - Lab 7
DBMS Experiment - Lab 7
DBMS Experiment - Lab 7
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
A view is a virtual table. That is, a view looks like a table and acts like a table
as far as a user is concerned, but it doesn’t require physical storage. A view is
actually a composition of a table in the form of a predefined query, which is
stored in the database. For example, a view can be created from EMPLOYEE
that contains only the employee’s name and address, instead of all columns in
EMPLOYEE. A view can contain all rows of a table or selected rows from a table.
A view can be created from one or many tables. When a view is created, a
SELECT statement is actually run against the database, which defines the view.
The SELECT statement that defines the view might simply contain column
names from the table, or it can be more explicitly written using various
functions and calculations to manipulate or summarize the data that the user
sees. Table
(holds data)
SQL Queries Derived From
View
(refers data)
School of Computer Engineering
Views cont…
4
Syntax:
CREATE VIEW view_name AS SELECT columns FROM table1 [INNER | LEFT
OUTER | RIGHT OUTER | FULL OUTER] JOIN ON table 2 [conditions] [WHERE
conditions] ;
Examples:
CREATE VIEW EMPLOYEE_SUMMARY AS SELECT E.EMP_ID, E.LAST_NAME,
P.POSITION, P.DATE_HIRE, P.PAY_RATE FROM EMPLOYEE E, EMPLOYEE PAY
P WHERE E.EMP_ID = P.EMP_ID;
CREATE VIEW EMPLOYEE_SUMMARY AS SELECT E.EMP_ID, E.LAST_NAME,
P.POSITION, P.DATE_HIRE, P.PAY_RATE FROM EMPLOYEE E INNER JOIN
EMPLOYEE PAY P ON E.EMP_ID = P.EMP_ID;
Note
When selecting data from multiple tables, the tables must be joined by
common columns.
Table
Derived From
A view can be created from another view using the following format:
CREATE VIEW4 AS SELECT * FROM VIEW1;
CREATE VIEW6 AS SELECT * FROM VIEW1 UNION SELECT * FROM VIEW3;
Note
You can create a view from a view many layers deep (a view of a view of a view, and so on). How
deep you can go is implementation specific. The only problem with creating views based on other
views is their manageability. For example, suppose that you create VIEW2 based on VIEW1 and then
create VIEW3 based on VIEW2. If VIEW1 is dropped, VIEW2 and VIEW3 are no good. The underlying
information that supports these views no longer exists. Therefore, always maintain a good
understanding of the views in the database and on which other objects those views rely.
School of Computer Engineering
Views and the ORDER BY Clause
8
ORDER BY clause cannot be used in the CREATE VIEW statement; however, the
GROUP BY clause has the same effect as an ORDER BY clause when it’s used in
the CREATE VIEW statement.
Example –
CREATE VIEW EMPLOYEE _NAMES_VIEW AS
SELECT LAST_NAME || ’, ‘ || FIRST_NAME || ’ ‘ ||MIDDLE_NAME NAME
FROM EMPLOYEE
GROUP BY LAST_NAME || ’, ‘ || FIRST_NAME || ’ ‘ || MIDDLE_NAME;
Access : This Oracle CREATE VIEW would create a virtual table based on the
result set of the SELECT statement. You can now query the VIEW as follows:
SELECT * FROM EMPLOYEE _NAMES_VIEW;
SELECT * FROM EMPLOYEE _NAMES_VIEW WHERE LENGTH(NAME) > 10;
Updation: You can modify the definition of an VIEW without dropping it by
using the CREATE OR REPLACE VIEW statement.
Syntax – CREATE OR REPLACE VIEW view_name AS SELECT columns
FROM table WHERE conditions;
Example – CREATE OR REPLACE VIEW EMPLOYEE_VIEW AS SELECT *
FROM EMPLOYEE;
Drop: Once the VIEW has been created, it can be dropped with the DROP VIEW
Statement.
Syntax – DROP VIEW view_name;
Example – DROP VIEW EMPLOYEE_VIEW;
You can update the underlying data of a view under certain conditions:
The view must not involve joins.
The view must not contain a GROUP BY clause.
The view must not contain a UNION statement.
The view cannot contain a reference to the pseudo-column ROWNUM.
The view cannot contain group functions.
The DISTINCT clause cannot be used.
The WHERE clause cannot include a nested table expression that includes a
reference to the same table as referenced in the FROM clause.
Conclusion
The view can perform INSERT, UPDATE, and DELETE as long as they honor these
caveats.
Dropping Synonym
Syntax:
DROP [PUBLIC] SYNONYM [schema .] synonym_name;
Example:
DROP SYNONYM CUST;
DROP SYNONYM PRODUCT;
An index is typically stored separately from the table for which the index was
created. An index’s main purpose is to improve the performance of data retrieval.
Indexes can be created or dropped with no effect on the data. However, after an
index is dropped, performance of data retrieval might be slowed. Indexes do take
up physical space and can often grow larger than the table. Therefore, you should
consider them when estimating your database storage needs.
How do Indexes Work?
When an index is created, it records the location of values in a table that are
associated with the column that is indexed. Entries are added to the index when
new data is added to the table. When a query is executed against the database
and a condition is specified on a column in the WHERE clause that is indexed, the
index is first searched for the values specified in the WHERE clause. If the value is
found in the index, the index returns the exact location of the searched data in
the table. Figure shown next illustrates the functioning of an index.
NAME index is
referenced to resolve
the location of all
names equal to
SMITH. After the
location is
determined, the data
can quickly be
retrieved from the
table. The data, in this
case, names is
alphabetized in the
index.
Rename an Index
Syntax : ALTER INDEX index_name RENAME TO new_index_name;
Example : ALTER INDEX NAME_IDX RENAME TO EMP_NAME_IDX;
Drop an Index
Syntax : DROP INDEX index_name;
Example : DROP INDEX EMP_NAME_IDX;
The DCL commands are used to enforce database security i.e. to perform any
operation in the database, such as for creating tables, sequences or views, we
need privileges.
Privileges are of two types
System : creating session, table etc are all types of system privilege.
Object : any command or query to work on tables comes under object
privilege.
There are two types of DCL commands. They are GRANT and REVOKE.
GRANT - This command is used to provide access or privileges on the
database objects to the users.
REVOKE - This command removes the user access rights or privileges to
the database objects.
Only Database Administrator's or owner's of the database object can
provide/remove privileges on a database object.
TCL commands are used to manage transactions in database. These are used to manage
the changes made by DML statements. It also allows statements to be grouped together
into logical transactions.
Commit command : Commit command is used to permanently save any transaction
into database. Syntax : commit;
Rollback command : This command restores the database to last committed state. It
is also use with savepoint command to jump to a savepoint in a transaction. Syntax:
rollback to savepoint-name;
Savepoint command : This command is used to temporarily save a transaction so
that you can rollback to that point whenever necessary. Syntax: savepoint savepoint-
name;
Examples:
INSERT into class values(5,'Rahul'); INSERT into class values(6,'Chris');
COMMIT; SAVEPOINT B;
UPDATE class set name=‘Abhijit' where id='5'; ROLLBACK TO A;
SAVEPOINT A;
School of Computer Engineering
21
Thank You
End of Lab 7
School of Computer Engineering
Tables
22
12. Create index on First Name, Middle Name and Last Name in Employee
13. Save the transaction as save point.
14. Rename the index created for question 12
15. Drop the index renamed for question 14
16. Rename the view created for question 3
17. Drop the view renamed in question 16
18. Commit transaction done for the steps from 1 to 17
19. Drop the view created for question 1
20. Rollback the entire transaction