Presentation is loading. Please wait.

Presentation is loading. Please wait.

PL/SQL Dynamic SQL and Oracle Architecture.

Similar presentations


Presentation on theme: "PL/SQL Dynamic SQL and Oracle Architecture."— Presentation transcript:

1 PL/SQL Dynamic SQL and Oracle Architecture

2 Topics To Be Covered Introduction and Objectives Dynamic SQL
Oracle Server Architecture Oracle Database Oracle Database Physical and Logical Structure Data Dictionary Summary

3 Introduction and Objectives
Module Introduction Some applications accept and process a variety of SQL statements at runtime. Such statements can change from execution to execution. They are aptly called dynamic SQL statements. This technique adds flexibility and functionality to the user's applications. An Oracle database is a collection of data considered as a unit. The purpose of a database is to store and retrieve the related information. A database server, is the key to solve the problems of information management. Oracle database is the first database designed for enterprise grid computing. It is the most flexible and cost effective way to manage information and applications. This course first explains the dynamic SQL statements. Then you will learn the Oracle server architecture and database. Later, the course describes the Oracle database physical and logical structure. Finally, the course explains the data dictionary.

4 Objectives After completing this course, you will be able to:
Explain the usage of EXECUTE IMMEDIATE statement in the PL/SQL block. Implement DDL statements in PL/SQL block. Describe the concept of USING clause with the help of an example. Explain the Oracle server architecture. Describe Oracle database and Oracle instance. Explain Oracle server components. Explain Oracle memory structure. Describe the features of data dictionary.

5 Dynamic SQL

6 Dynamic SQL EXECUTE IMMEDIATE Statement in PL/SQL Block
Native dynamic SQL processes most of the dynamic SQL statements by using the EXECUTE IMMEDIATE statement. It is the part of the native dynamic SQL, which has the SQL built into the PL/SQL kernel. This is faster than regular dynamic SQL.

7 Syntax for EXECUTE IMMEDIATE
dynamnic_string define_variable record_name INTO collection_name host_array_name COLLECT BULK IN OUT USING bind_argument returning_clause Syntax for EXECUTE IMMEDIATE

8 Dynamic SQL EXECUTE IMMEDIATE Statement in PL/SQL Block
EXECUTE IMMEDIATE dynamic_sql_string [INTO {define_variable,… | INTO record_name} ] [USING [IN|OUT] bind_argument,…] [RETURN[ING] INTO bind_argument,…]; Keywords: dynamic_sql_string: The SQL statement string or PL/SQL block. define_variable: One variable receives each column value returned by the query. record_name: A record based on a user-defined TYPE or %ROWTYPE that receives an entire row returned by a query. bind_argument An expression whose value is passed to the SQL statement or PL/SQL block INTO clause. Use for single-row queries; for each column value returned by the query, one must supply an individual variable or field in a record of compatible type.

9 Dynamic SQL DDL Statement in PL/SQL Block
CREATE 0R REPLACE PROCEDURE drop_table (p_table_name IN VARCHAR2 ) IS temp VARCHAR2 (30); record_locked EXCEPTION; PRAGMA EXCEPTION_INIT (record_locked, -54); BEGIN SELECT tname INTO temp FROM tab WHERE tname=UPPER (p_table_name); EXECUTE IMMEDIATE ‘DROP TABLE’ || UPPER(p_table_name); DBMS_OUTPUT .PUT_LINE (‘Table dropped :’ || p_table_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT .PUT_LINE (‘Table does not exist’); WHEN record_locjed THEN /*Record was already licked, so just keep on going */ DBMS_OUTPUT .PUT_LINE (‘Resource is busy’); END;

10 Dynamic SQL USING Clause
USING clause allows the supply of bind arguments for the SQL string. This clause is used for both dynamic SQL and PL/SQL, which is the reason one can specify a parameter mode. This usage is only relevant for PL/SQL. However, the default parameter is IN, which is the only kind of bind argument one would have for SQL statements.

11 Dynamic SQL USING Clause
Let us have a look at the example of USING clause. CREATE OR REPLACE PROCEDURE change_msisdn ( servReq_column VARCHAR2, column_value VARCHAR2, column_name VARCHAR2) IS v_column VARCHAR2(30); sql_stmt VARCHAR2(250); BEGIN -- determine if a valid column name has been given as input

12 Dynamic SQL USING Clause
Let us have a look at the example of USING clause. SELECT COLUMN_NAME INTO v_column FROM USER_TAB_COLS WHERE TABLE_NAME = ‘SERVICE_REQUEST’ AND COLUMN_NAME = UPPER (servReq_column); sql_stmt := ‘UPDATE service_request SET sr_priority= :1 Where ‘ || v_column || ‘ = :2’; EXECUTE IMMEDIATE sql_stmt USING amount, column_value;

13 Dynamic SQL USING Clause
Let us have a look at the example of USING clause. IF SQL%ROWCOUNT > 0 THEN DBMS_OUTPUT.PUT_LINE('updation done'); ELSE DBMS_OUTPUT.PUT_LINE(‘No such record found'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('invalid column: ' || servReq_column); END raise_emp_salary;

14 Knowledge Check Start

15 Select the correct alternative by which native dynamic SQL processes most of the dynamic SQL statements. USING Clause EXECUTE IMMEDIATE DDL Statement None of the above

16 Which of the following alternatives allow the supply of bind arguments for the SQL string?
USING Clause EXECUTE IMMEDIATE DDL Statement None of the above

17 The Results

18 Oracle Server Architecture

19 Oracle Server Architecture
Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file Oracle Server INSTANCE LGWR CKPT Others + PMON SMON DBWR Oracle Server An Oracle server provides an open, comprehensive, and integrated approach to the information management. An Oracle server consists of an Oracle instance and an Oracle database.

20 Oracle Server Architecture
Oracle Database Physical Structure Data Files Control Files Redo Log Files Logical structure Table Spaces Segments Extents Blocks Oracle Instance Memory Structure Background Processes Oracle Server Architecture An Oracle database has physical as well as logical structure. The physical storage of data can be managed without affecting the access to logical storage structures. The Oracle instance comprises of background processes and memory structure. Let us take a look at the Oracle server architecture.

21 Oracle Server Architecture
The figure explains the Oracle server connection through the network. Client Application NETWORK (Tnsnames.ora) (Listener.ora) Host:Port:Sid Client/Server Architecture Database Server

22 Oracle Server Architecture
Database Connectivity Let us take a look at the process of database connectivity. Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file Oracle Server INSTANCE LGWR CKPT Others PMON SMON DBWR conn Network Protocol Tnsnames.ora For an application to connect to a database through an Oracle Listener, the application must have information about that service like service name, host address, protocol, listener port, etc. This information is stored In tnsnames.ora Default location of tnsnames.ora %ORACLE_HOME%\network\admin

23 Oracle Server Architecture
Database Connectivity Let us take a look at the process of database connectivity. Listener process is the gateway to the Oracle instance for al nonlocal user connections. INSTANCE Shared Pool System Global Area Network Protocol conn Library Cache Database Buffer Cache Redo log Buffer Cache Data Dictionary Cache PMON SMON DBWR LGWR CKPT Others Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file Tnsnames.ora listener.ora Oracle Server

24 [root@oracle admin]# cat listener.ora
# listener.ora Network Configuration File: # /u01/oracle/product/11.2.0/dbhome/network/admin/listener.ora # Generated by Oracle configuration tools. STD_LIST_LISTENER = (STD_LIST = (STO_DESC = (GLOBAL_DBNAM E = itpdb) (ORACLE_HOME = /U01/oracle/product/11.2.0/dbhome) (STD_NAME=itpdb) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ) (PORT = 1521)) ADR_BASE_LISTENER = /uOl/oracle admin]#

25 Oracle Server Architecture
Database Connectivity Let us take a look at the process of database connectivity. User Process Server Process INSTANCE Connection Established If the CONNECT packet requests a valid service name, the listener spawns a Server process to deal with the connection Shared Pool System Global Area Library Cache Database Buffer Cache Redo log Buffer Cache Incoming connection request Data Dictionary Cache conn PMON SMON DBWR LGWR CKPT Others Network Protocol Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file Tnsnames.ora listener.ora Oracle Server

26 Oracle Server Architecture
Oracle Server Components Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file Oracle Server INSTANCE LGWR CKPT Others PMON SMON DBWR In Oracle server components, the system global area and background processes form the Oracle instance. The background processes such as DBWR and LGWR interact with the physical database. Let us take a look at the Oracle server components in detail. Server Server Process PGA User Process

27 Oracle Server Architecture
Oracle Memory Structure Oracle memory structure is divided into two parts. These are system global area and program global area. Now. let us learn the Oracle memory structure in detail. Oracle Memory Structure System Global Area Program Global Area SGA It is allocated at instance startup, and is a fundamental component of an Oracle instance PGA It is allocated when the server process is started

28 Oracle Server Architecture
Oracle Memory Structure SGA stores database information. The memory structures of SGA are shared pool, database buffer cache, and redo log buffer. Now, let us take a look at the SGA structure. Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache SGA Structure

29 Oracle Server Architecture
Shared Pool The shared pool is a portion of the SGA that contains shared memory constructs such as shared SQL areas. The two components of shared pool are library cache and data dictionary cache. Now. let us learn the components of shared pool in detail. Shared Pool Library Cache Data Dictionary Cache

30 Oracle Server Architecture
Shared Pool Library Cache The library cache stores information about the most recently used SQL and PL/SQL statements. Features of library cache are: It enables the sharing of commonly used statements. It is managed by a Least Recently Used (LRU) algorithm. It consists of two structures: Shared SQL area Shared PL/SQL area Shared pool sizing determines its size.

31 Oracle Server Architecture
Shared Pool Data Dictionary Cache The data dictionary cache is a collection of the most recently used definitions in the database. It is also referred to as the row cache. Features of data dictionary cache are: It includes information about database files, tables, indexes, columns, users, privileges, and other database objects. During the parse phase, the server process looks at the data dictionary for information to resolve object names and validate access. Caching the data dictionary information into memory improves response time on queries. The size of the data dictionary cache is determined by the shared pool sizing and is managed internally by the database.

32 Oracle Server Architecture
Database Buffer Cache Database Buffer Cache The database buffer cache is the cache area, where data blocks are read from the data segments, such as tables and indexes. Here, the data is read from, and written to the data files. The size of each buffer in the db buffer cache, is equal to the size of an Oracle data block. This space is managed by the LRU algorithm. Let us take a look at the database buffer cache. Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR

33 Oracle Server Architecture
Redo Log Buffer The redo log buffer of the SGA, stores the log of changes made to the database. The redo entries stored in the redo log buffers are written to an online redo log file. This log file is used if database recovery is necessary. Let us take a look at the working of redo log buffer. Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR Server Process User Process

34 Oracle Server Architecture
Oracle Memory Structure: PGA The Program Global Area or Process Global Area (PGA) is a memory region that contains data and the control information for a single server process or a single background process. When a process is created, the PGA is allocated and when a process is terminated the PGA is de-allocated. The PGA components are: Sort Area: it is used for any sorts that may be required to process the SQL statement. Session Information: It includes user privileges and performance statistics for the session. Cursor State: It indicates the stage in the processing of the SQL statements that are currently used by the session. Stack Space: It contains other session variables.

35 Knowledge Check Start

36 Which of the following does an Oracle server consist?
Oracle database Network

37 Which of the following does an Oracle server consist?
Oracle memory Oracle instance

38 How many minimum redo logs does a database require?
1 2 3 4

39 Who maintains the relationship between physical and memory structure?
Oracle database Oracle background processes Oracle memory Oracle process structure

40 The Results

41 Oracle Database

42 Oracle Database Data Files:
Oracle database is a collection of data that is treated as a unit. The three types of Oracle database files are control files, data files, and online redo log files. Data Files Control Files Online Redo Log Files Password File Parameter File Alert & Trace Log Files Archived Log Files Oracle Database Data Files: There should be at least one data file for each tablespace. But, it should belong to only one tablespace. Data files contain tables, indexes, rollback segments, and temp segments.

43 Oracle Database Control Files:
Data Files Control Files Online Redo Log Files Password File Parameter File Alert & Trace Log Files Archived Log Files Oracle Database Control Files: Control files contain information required to maintain and verify database integrity. A database needs at least one control file. The init<SID>.ora file lists the control files location using variable control_files

44 Oracle Database Online Redo Log Files: Data Files Control Files
Oracle Database Data Files Control Files Online Redo Log Files Password File Parameter File Alert & Trace Log Files Archived Log Files Oracle Database Online Redo Log Files: Online redo log files record the changes made in the database to enable recovery of the data in case of failures. A database requires at least two redo log files. The log writer writes to one redo log file and when it is filled, starts writing to the second redo log file. Meanwhile, if the data is critical, the first redo log file can be archived. After the second file is filled, the log writer will start writing to the first redo log file, after the archiving is complete.

45 Other Key File Structures
Oracle Database Other Key File Structures The Oracle server also uses some other files that are not part of the database. Let us take a look at the other key file structure used in database. Parameter File: The parameter file defines the characteristics of an Oracle instance. For example, it contains parameters that size some of the memory structures in the SGA. Alert & Trace Log Files Archived Log Files Password File Parameter File The password file authenticates whether users are privileged to start up and shut down an Oracle instance. Password File: Archived redo log files are offline copies of the redo log files that may be necessary to recover from media failures. Archived Log Files: Alert and trace files contain information on the health of the database and gives warning when problems occur. Alert & Trace Log Files:

46 Oracle Database Other Files of Database
Database also uses some other files. They are: Listener.ora It resides on the host node. Client requests use the listener processes for connecting to the database It resides on the client machine. It stores Oracle net services names. Tnsnames.ora

47 Oracle Database User Execution of SQL Query
Let us look at the execution of SQL query. When a user connects to the database using an application, user process is created. User

48 Oracle Database User User Process Server Process
Database Buffer Cache Execution of SQL Query Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR User User Process Server process searches requested data in the DB buffer cache. User process sends query to Server process SELECT * FROM customer; Server Process

49 Parameter file (int.ora)
Oracle Database Database Buffer Cache Execution of SQL Query User Process Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR User SELECT * FROM customer; Server Process If the requested data is not available in the buffer cache, the server process reads data from the data files.

50 Oracle Database User User Process Server Process
Execution of SQL Query Database Buffer Cache User Process Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR User Server process records data into the DB buffer cache SELECT * FROM customer; Server Process

51 Oracle Database User User Process Server Process
Execution of SQL Query Database Buffer Cache User Process Retrieved data is sent to User process Shared Pool System Global Area Library Cache Data Dictionary Cache Database Buffer Cache Redo log Buffer Cache Data files Control files Redo log files Database Archives Log files Parameter file (int.ora) Password file INSTANCE LGWR CKPT Others PMON SMON DBWR User SELECT * FROM customer; Server Process

52 Knowledge Check Start

53 The parameter file defines the characteristics of an Oracle instance
True False

54 Which of the following files are used by the Database?
Tsnames.ora Tnsames.ora

55 Which of the following files are used by the Database?
Listener.ora Listen.ora

56 The Results

57 Oracle Database Physical and Logical Structure

58 Oracle Database Physical and Logical Structure
Oracle Process Structure Oracle processes are responsible for successful database operations. Let us learn the oracle process structure in detail. Oracle Processes User Process Server Process Background Process User Process: It starts when the database user requests connection to the Oracle server. Server Process: It is connected to the Oracle instance and is started when a user establishes a session. Background Process: It is available when an Oracle instance is started,

59 Oracle Database Physical and Logical Structure
Background Processes The relationship between the physical structure and memory structure is maintained and enforced by the Oracle's background processes. Let us learn these background processes In detail. LGWR DBWR SMON PMON ARCn CKPT Process Monitor (PMON): It cleans up the instance when the processes fail. It rolls back transactions by releasing database locks and resources, and restarting deceased processes. System Monitor (SMON): It manages system recovery by opening the database, rolling forward changes from the online redo log files, and rolling back uncommitted transactions. SMON also coalesces free space. . Database Writer (DBWR): It writes the data to files when, checkpoints reach, dirty buffers reach their threshold, timeouts occur. Also it writes when real application cluster ping requests are made, tables drop or truncate, and tablespaces begin backup processing. Log Writer (LGWR): LGWR writes: when the user commits after three second intervals; when there is 1 MB of redo instructions; before the database writer writes. Checkpoint (CKPT): It signals the database writer at checkpoints and updates the file header information for database and control files. Archiver (ARCn) [optional]: Archiver process is critical to recover databases. When an Oracle database instance is in archive mode, the archiver writes to the redo log file and then they are mirrored in the archive log files as the database.

60 Oracle Database Physical and Logical Structure
Logical Structure of Oracle Database Oracle database is divided into logical storage spaces allocating the data accordingly. There are various logical units in the Oracle database. Database System Tablespace SysAux Tablespace Temp Users Undo Extend 1 Extend 2 Extend 3 Segment 1 Service Segment 2 SR_PK Segment 3 Customer Database Block Data blocks are the smallest unit of I/O in the database

61 Oracle Database Physical and Logical Structure
Logical Structure of Oracle Database Tablespaces A database is divided into logical storage units called tablespaces, which group related logical structures together. Each database is logically divided into one or more tablespaces. One or more datafiles are created for each tablespace to physically store the data of all logical structures in a tablespace. System Tablespace SYSAUX Tablespace Temp Data File

62 Oracle Database Physical and Logical Structure
Logical Structure of Oracle Database Extents The space allocated to a segment is in terms od extents. One or more extents make up a segment. Extent is a set of contiguous Oracle blocks. An extent cannot span data files. An extent must exist in one data file. Extend 1 Extend 2 Extend 3 Segment 2

63 Oracle Database Physical and Logical Structure
Logical Structure of Oracle Database Segments Segments are spaces allocated for a specific logical structure within a tablespace, A tablespace may consist of one or more segments. A segment cannot span tablespaces; however, a segment can span multiple data files that belong to the same tablespace. Each segment is made up of one or more extents. Segment 1 Service Segment 2 SR_PK Segment 3 Customer Tablespace

64 Oracle Database Physical and Logical Structure
Logical Structure of Oracle Database Database Blocks At the finest level of granularity, the data is stored in data blocks. One data block corresponds to one or more OS blocks allocated from the existing data file. Data block size should be a multiple of the OS block size to avoid unnecessary I/O. Database Block

65 Knowledge Check Start

66 Oracle processes are not responsible for succesful database operations.
True False

67 Process Monitor (PMON)
_____ signals the database writer at checkpoints and updates the file header information for database and control files. Process Monitor (PMON) System Monitor (SMON) Checkpoint (CKPT) Log Writer (LGWR)

68 The Results

69 Data Dictionary

70 Data Dictionary Tables
Introduction to Data Dictionary The data dictionary is a set of read-only tables and views that record, verify, and provide information about its associated database. It describes the database and its objects. During database creation, the Oracle server creates additional object structures within the data files. These objects are data dictionary tables and dynamic performance tables. When users create any database object, Oracle automatically inserts data into the data dictionary tables to keep track of the object. Data Dictionary Data Dictionary Tables CREATE TABLE customer (Cust_id NUMBER (4), Name VARCHAR(20)); service(sr_id Sr Name VARCHAR 2 (20)); Database

71 Data Dictionary Base Tables
Let us take a look al the Features of base tables. Base tables are the underlying tables, which store information about the database. The base tables are the first objects created in any Oracle database. They are automatically created when the Oracle server runs the sql.bsq script at the time the database is created. Only the Oracle server should write to these tables. Users can rarely access them directly, because most of the data is stored in a cryptic format.

72 Data Dictionary Views Data dictionary views are the base table summaries, which provide for a more useful display of base table information. The data dictionary views are created using the catalog.sql script. For example, in the data dictionary views, object names are used instead of only object numbers. The data dictionary view consists of three main sets of static views distinguished from each other by their scope. DBA ALL USER Data dictionary views are static views that answer questions such as: Was the object ever created? What is the object a part of? Who owns the object? What privileges do users have? What are the restrictions on the object?

73 Data Dictionary Contents
A data dictionary contains the definitions of all schema objects in the database, space allocated, names of Oracle users, privileges and roles granted to users, column, and integrity information. The data dictionary has three primary uses to find information about: Users Schema objects Storage structures The Oracle server modifies it when a DDL statement is executed. Users and DBAs can use it as a read-only reference for information about the database

74 Knowledge Check Start

75 It is connected to the Oracle instance and is started when a user establishes a session.
User Process Server Process Background process

76 It starts when the database user requests connection to the Oracle server.
User Process Server Process Background process

77 It is avaliable when an Oracle instance is started.
User Process Server Process Background process

78 The Results

79 Summary

80 Summary In this module, you have learned to:
Explain the usage of EXECUTE IMMEDIATE statement in the PL/SQL block. Implement DDL statements in PL/SQL block. Describe the concept of USING clause with the help of an example. Explain the Oracle server architecture. Describe Oracle database and Oracle instance. Explain Oracle server components. Explain Oracle memory structure. Describe the features of data dictionary.


Download ppt "PL/SQL Dynamic SQL and Oracle Architecture."

Similar presentations


Ads by Google