SQL Advanced Topics 1

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

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: "#$AT$ V%$& sales'staff A( ($)$"T empno, ename, deptno *#+, emp &-$#$ deptno . /01 Querying from View:select 2 from sales'staff1 Advantages of Views:1) $) &) They hide the ase ta !e names and their "o!umns# %n!y se!e"ted data "an e shown through views# They wi!! not o""u'y any s'a"e for the data storage#

(ow they wor): &henever a view is called, the base table will get accessed to get the data. The end user will not 3now whether the data has accessed from a view or a table. %n reality the data will be accessed from a base table.

SEQ*E+CES
(equences are database objects from which multiple users can generate unique integers. The sequence generator generates sequential numbers, which can help to generate unique primary 3eys automatically, and to coordinate 3eys across multiple rows or tables. &ithout sequences, sequential values can only be produced programmatically. A new primary 3ey value can be obtained by selecting the most recently produced value and incrementing it. This method requires a loc3 during the transaction and causes multiple users to wait for the ne4t value of the primary 3ey1 this waiting is 3nown as seriali5ation. %f developers have such constructs in applications, then you should encourage the developers to replace them with access to sequences. (equences eliminate seriali5ation and improve the concurrency of an application. Creating a se,uen"e:"#$AT$ ($67$8"$ emp'sequence %8"#$,$8T BY / (TA#T &%T- /1

A""essing a se,eun"e:($)$"T +rder'seq.8$9TVA) *#+, dual1 ($)$"T +rder'seq."7##VA) *#+, dual1

S-+%+-.S
A synonym is an alias for a schema object. (ynonyms can provide a level of security by mas3ing 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 comple4ity of (6) statements for database users. Creating a synonym: "#$AT$ (Y8+8Y, emp'syn *+# scott.emp1 (ynonym hides the base table name. (ynonym can be created for a table, view, stored procedure, function or a synonym.

I+/E0ES
%nde4es are database objects associated with tables. %nde4es can be created to increase the performance of data retrieval. :ust as the inde4 in this manual helps you quic3ly locate specific information, an +racle inde4 provides an access path to table data. &hen processing a request, +racle can use some or all of the available inde4es to locate the requested rows efficiently. %nde4es are useful when applications fre,uent!y ,uery a table for a range of rows for e4ample, all employees with a salary greater than /000 dollars! or a specific row. %nde4es are created on one or more columns of a table. After it is created, an inde4 is automatically maintained and used by +racle. "hanges to table data such as adding new rows, updating rows, or deleting rows! are automatically incorporated into all relevant inde4es with complete transparency to the users. By default B;tree inde4es are created.

Creating an Inde1 "#$AT$ %8<$9 employees'id4/ +8 emp ename!1

PL/SQL (Procedural Language/SQL)


%t is a procedural e4tension to (6). =)>(6) is a completely portable, high;performance transaction processing language that offers the following advantages? Better =erformance *ull =ortability (upport for +bject;+riented =rogramming

2etter 3erforman"e &ithout =)>(6), +racle must process (6) statements one at a time. =rograms that issue many (6) statements require multiple calls to the database, resulting in significant networ3 and performance overhead. &ith =)>(6), an entire bloc3 of statements can be sent to +racle at one time. This can drastically reduce networ3 traffic between the database and an application. As *igure /;/ shows, you can use =)>(6) bloc3s and subprograms to group (6) statements before sending them to the database for e4ecution. =)>(6) also has language features to further speed up (6) statements that are issued inside a loop. *igure /;/ =)>(6) Boosts =erformance

4u!! 3orta i!ity Applications written in =)>(6) can run on any operating system and platform where the +racle database runs. &ith =)>(6), you can write portable program libraries and reuse them in different environments. Su''ort for % 5e"t-%riented 3rogramming +bject types are an ideal object;oriented modeling tool, which you can use to reduce the cost and time required to build comple4 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.

C*6S%6S
A cursor is a name for a specific private (6) area in which information for processing the specific statement is 3ept. =)>(6) uses both implicit and e4plicit cursors. =)>(6) implicitly declares a cursor for all (6) data manipulation statements on a set of rows, including queries that return only one row. *or queries that return more than one row, you can e4plicitly declare a cursor to process the rows individually.

ST%6E/ 36%CE/*6ES
%t is one of the =)>(6) (ubprograms.

=)>(6) stored procedures are compiled once and stored in e4ecutable form, so procedure calls are efficient. Because stored procedures e4ecute in the database server, a single call over the networ3 can start a large job. This division of wor3 reduces networ3 traffic and improves response times. (tored procedures are cached and shared among users, which lowers memory requirements and invocation overhead. Creating a 'ro"edure: CREATE or replace procedure emp_insert ( p_in_empno IN number ) As V_new_sal number := 0 !e"in #elect sal into V_new_sal $rom emp w%ere empno=p_in_empno I& V_new_sal ' (000 t%en V_new_sal := V_new_sal ) *00 Else V_new_sal := V_new_sal + *00 End i& ,pdate emp set sal = V_new_sal w%ere empno = p_in_empno Commit End -

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


%t is one of the =)>(6) (ubprograms. A function is a subprogram that computes a value. %t is almost li3e stored procedure e4cept it returns a value /ifferen"es:Stored 3ro"edure /! 8eed not return a value @! "annot be called through (6) (tatements. 4un"tion /! ,ust return a value. @! "an be called through (6) (tatements.

4*+CTI%+S

A! "an be called using $9$" command.

A! 8ot possible to call using $9$" command.

PACKAGES
A pac3age is a database object that groups logically related variables and subprograms. =ac3ages usually have two parts, a specification spec! and a body1 sometimes the body is unnecessary. The specification is the interface to the pac3age. %t declares the types, variables, constants, e4ceptions, cursors, and subprograms that can be referenced from outside the pac3age. The body defines the queries for the cursors and the code for the subprograms. You can thin3 of the spec as an interface and of the body as a blac3 bo4. You can debug, enhance, or replace a pac3age body without changing the pac3age spec. Advantages of 3a")ages: Information (iding

&ith pac3ages, you can specify which types, items, and subprograms are public visible and accessible! or private hidden and inaccessible!. *or e4ample, if a pac3age contains four subprograms, three might be public and one private. The pac3age hides the implementation of the private subprogram so that only the pac3age 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 pac3age. 2etter 3erforman"e

&hen you call a pac3aged subprogram for the first time, the whole pac3age is loaded into memory. )ater calls to related subprograms in the pac3age require no dis3 %>+.

T6I77E6S
Triggers are similar to stored procedures. A trigger stored in the database can include (6) and =)>(6). -owever, procedures and triggers differ in the way that they are invo3ed. A procedure is e4plicitly run by a user, application, or trigger. Triggers are implicitly fired by +racle when a triggering event occurs, no matter which user is connected or which application is being used. Advanatges: To enforce comple4 business rules that are not definable using constraints li3e =B,*B, 7nique, 8ot null C "hec3!. Automatically carry on some activites on different tables whenever some <,) operations are done.

Oracle Indexes B Tree indexes! Bitmap indexes F"n#tion based indexes Relational tables Temporary tables Partitioned tables External tables Index organized tables

SQL Server B Tree indexes

Tables

Relational tables Temporary tables Partitioned tables

Triggers

BEFORE triggers AFTER triggers INSTEAD OF triggers Database Event triggers

AFTER triggers INSTEAD OF triggers DDL Triggers

Oracle Programming Capabilities. P$%S&$ statements 'ava met(ods T(ird generation lang"age )*+$, ro"tines +bject;oriented programming

SQL Server T-SQL Statements. .NET compatible language.

Arrays

Supported

Supported from SQL Server - 2005

You might also like