Вы находитесь на странице: 1из 5

VIEWS

A view is a logical representation of another table or combination of tables. A view derives its data from the tables on which it is based. These tables are called base tables. Base tables might in turn be actual tables or might be views themselves. All operations performed on a view actually affect the base table of the view. You can use views in almost the same way as tables. You can query, update, insert into, and delete from views, just as you can standard tables. Views can provide a different representation (such as subsets or supersets) of the data that resides within other tables and views. Views are very powerful because they allow you to tailor the presentation of data to different types of users. Creating a view: CREATE VIEW sales_staff AS SELECT empno, ename, deptno FROM emp WHERE deptno = 10; Querying from View:select * from sales_staff; Advantages of Views:1) 2) 3) They hide the base table names and their columns. Only selected data can be shown through views. They will not occupy any space for the data storage.

How they work: Whenever a view is called, the base table will get accessed to get the data. The end user will not know whether the data has accessed from a view or a table. In reality the data will be accessed from a base table.

SEQUENCES
Sequences are database objects from which multiple users can generate unique integers. The sequence generator generates sequential numbers, which can help to generate unique primary keys automatically, and to coordinate keys across multiple rows or tables. Without sequences, sequential values can only be produced programmatically. A new primary key value can be obtained by selecting the most recently produced value and incrementing it. This method requires a lock during the transaction and causes multiple users to wait for the next value of the primary key; this waiting is known as serialization. If developers have such constructs in applications, then you should encourage the developers to replace them with access to sequences. Sequences eliminate serialization and improve the concurrency of an application. Creating a sequence:CREATE SEQUENCE emp_sequence INCREMENT BY 1 START WITH 1;

Accessing a seqeunce:SELECT Order_seq.NEXTVAL FROM dual; SELECT Order_seq.CURRVAL FROM dual;

SYNONYMS
A synonym is an alias for a schema object. Synonyms can provide a level of security by masking the name and owner of an object and by providing location transparency for remote objects of a distributed database. Also, they are convenient to use and reduce the complexity of SQL statements for database users. Creating a synonym: CREATE SYNONYM emp_syn FOR scott.emp; Synonym hides the base table name. Synonym can be created for a table, view, stored procedure, function or a synonym.

INDEXES
Indexes are database objects associated with tables. Indexes can be created to increase the performance of data retrieval. Just as the index in this manual helps you quickly locate specific information, an Oracle index provides an access path to table data. When processing a request, Oracle can use some or all of the available indexes to locate the requested rows efficiently. Indexes are useful when applications frequently query a table for a range of rows (for example, all employees with a salary greater than 1000 dollars) or a specific row. Indexes are created on one or more columns of a table. After it is created, an index is automatically maintained and used by Oracle. Changes to table data (such as adding new rows, updating rows, or deleting rows) are automatically incorporated into all relevant indexes with complete transparency to the users. By default B-tree indexes are created.

Creating an Index CREATE INDEX employees_idx1 ON emp (ename);

PL/SQL (Procedural Language/SQL)


It is a procedural extension to SQL. PL/SQL is a completely portable, high-performance transaction processing language that offers the following advantages:

Better Performance Full Portability Support for Object-Oriented Programming Better Performance Without PL/SQL, Oracle must process SQL statements one at a time. Programs that issue many SQL statements require multiple calls to the database, resulting in significant network and performance overhead. With PL/SQL, an entire block of statements can be sent to Oracle at one time. This can drastically reduce network traffic between the database and an application. As Figure 1-1 shows, you can use PL/SQL blocks and subprograms to group SQL statements before sending them to the database for execution. PL/SQL also has language features to further speed up SQL statements that are issued inside a loop. Figure 1-1 PL/SQL Boosts Performance

Full Portability Applications written in PL/SQL can run on any operating system and platform where the Oracle database runs. With PL/SQL, you can write portable program libraries and reuse them in different environments. Support for Object-Oriented Programming Object types are an ideal object-oriented modeling tool, which you can use to reduce the cost and time required to build complex applications. Besides allowing you to create software components that are modular, maintainable, and reusable, object types allow different teams of programmers to develop software components concurrently. A cursor is a name for a specific private SQL area in which information for processing the specific statement is kept. PL/SQL uses both implicit and explicit cursors. PL/SQL implicitly declares a cursor for all SQL data manipulation statements on a set of rows, including queries that return only one row. For queries that return more than one row, you can explicitly declare a cursor to process the rows individually.

CURSORS

STORED PROCEDURES
It is one of the PL/SQL Subprograms.

PL/SQL stored procedures are compiled once and stored in executable form, so procedure calls are efficient. Because stored procedures execute in the database server, a single call over the network can start a large job. This division of work reduces network traffic and improves response times. Stored procedures are cached and shared among users, which lowers memory requirements and invocation overhead. Creating a procedure: CREATE or replace procedure emp_insert ( p_in_empno IN number ) As V_new_sal number := 0; Begin Select sal into V_new_sal From emp where empno=p_in_empno; If V_new_sal > 3000 then V_new_sal := V_new_sal + 200; Else V_new_sal := V_new_sal 200; End if; Update emp set sal = V_new_sal where empno = p_in_empno; Commit; End; /

Executing a Procedure: SQL> EXEC emp_insert (7788);

FUNCTIONS
It is one of the PL/SQL Subprograms. A function is a subprogram that computes a value. It is almost like stored procedure except it returns a value Differences:Stored Procedure 1) Need not return a value 2) Cannot be called through SQL Statements. 3) Can be called using EXEC command. Function 1) Must return a value. 2) Can be called through SQL Statements. 3) Not possible to call using EXEC command.

PACKAGES
A package is a database object that groups logically related variables and subprograms. Packages usually have two parts, a specification (spec) and a body; sometimes the body is unnecessary. The specification is the interface to the package. It declares the types, variables, constants, exceptions, cursors, and subprograms that can be

referenced from outside the package. The body defines the queries for the cursors and the code for the subprograms. You can think of the spec as an interface and of the body as a black box. You can debug, enhance, or replace a package body without changing the package spec. Advantages of Packages: Information Hiding

With packages, you can specify which types, items, and subprograms are public (visible and accessible) or private (hidden and inaccessible). For example, if a package contains four subprograms, three might be public and one private. The package hides the implementation of the private subprogram so that only the package (not your application) is affected if the implementation changes. This simplifies maintenance and enhancement. Also, by hiding implementation details from users, you protect the integrity of the package. Better Performance

When you call a packaged subprogram for the first time, the whole package is loaded into memory. Later calls to related subprograms in the package require no disk I/O.

TRIGGERS
Triggers are similar to stored procedures. A trigger stored in the database can include SQL and PL/SQL. However, procedures and triggers differ in the way that they SQL Server are invoked. Oracle

B-Tree indexes. Triggers Oracle occurs, no matter Indexes are implicitly fired byOraclewhen a triggering eventSQL Server Bitmap indexes which user is connected or which application is being used. B-Tree indexes Function-based indexes Advanatges:PL/SQL statements To enforce complex business rules that are not definable using constraints like Java methods (PK,FK, Unique, Not null & Check). Automatically carry onThird-generation some activites on different tables whenever some DML Relational tables T-SQL Statements. operations are done. Relational tables language Programming Temporary tables Temporary tables .NET compatible (3GL) routines Capabilities. Partitioned tables Tables Partitioned tables language. Object-oriented External tables programming tables Index organized
Supported from SQL Server - 2005

A procedure is explicitly run by a user, application, or trigger.

Arrays Triggers

Supported BEFORE triggers AFTER triggers INSTEAD OF triggers Database Event triggers

AFTER triggers INSTEAD OF triggers DDL Triggers


5