Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Environment.

Similar presentations


Presentation on theme: "SQL Environment."— Presentation transcript:

1 SQL Environment

2 C/S Architecture Clients and servers server clients database

3 Three-Tier Architecture
A common environment for using a database has three tiers of processes: Web servers --- talk to the user. Application servers --- execute the business logic. Database servers --- get what the app servers need from the database.

4 SQL Environment The context in which SQL-data exists and SQL-statements are executed.

5 SQL-Environment(SQL:2003)
Components: 1 SQL-agent 1 SQL-implementation 0+ SQL-client modules, containing externally-invoked procedures available to the SQL-agent. 0+ authorization identifiers 0+ catalogs, each of which contains 1+ SQL-schemas. The sites, principally base tables, that contain SQL-data. This data may be thought of as “the database”, but the term is not used in ISO/IEC 9075

6 Organization of DB Elements
DB elements defined in an SQL environment: tables, views, triggers, ... They are organized into a hierarchy of structures: Schemas Catalogs Clusters

7 Schemas Schema = {base tables, views, domains, UDTs, constraints, triggers, char sets, colations, …} Basic units of organization Close to but less than what we call a “database”

8 Schemas (cont.) Declaration
CREATE SCHEMA schema_name CREATE TABLE ... CREATE VIEW ... CREATE ASSERTION ... ...... AUTHORIZATION au_id Schema modification: DROP, ALTER schema elements. But in which schema? “Current” schema SET SCHEMA schema_name;

9 Catalogs Catalog = {INFORMATION_SCHEMA, Schema1, Schema2, ...}
Basic unit for supporting unique, accessible terminology. Qualified name: cat1.schema2.obj3 SQL Server: server1.db2.owner3.obj4 SQL does NOT define a statement like CREATE CATALOG catalog_name CREATE SCHEMA ... But SQL provide a statement to set “current” catalog SET CATALOG catalog_name; new schemas go into the current catalog

10 Clusters Cluster = {catalogs}
Not precise in SQL Each user has an associated cluster of all catalogs accessible to the user. A cluster is the maximum scope of DB operations. “The database” as seen by a particular user.

11 Catalogs A catalog is a named collection of SQL-schemas, foreign server descriptors, and foreign data wrapper descriptors in an SQL-environment. The mechanisms for creating and destroying catalogs are implementation-defined. The default catalog name for dynamically prepared statements in the current SQL-session through the execution of PREPARE and EXECUTE IMMEDIATE statements is initially implementation-defined but may be changed by the use of SET CATALOG statement.

12 SQL-Schemas An SQL-schema, often referred to simply as a schema, is a persistent, named collection of descriptors. Any object whose descriptor is in some SQL-schema is known as an SQL-schema object. A schema, the schema objects in it, and the SQL-data described by them are said to be owned by the authorization identifier associated with the schema. SQL-schemas are created and destroyed by execution of SQL-schema statements (or by implementation-defined mechanisms).

13 The Information Schema
Every catalog contains an INFORMATION_SCHEMA that includes the descriptors of a number of schema objects, mostly view definitions, that together allow every descriptor in that catalog to be accessed, but not changed, as though it was SQL-data. The data available through the views in an Information Schema includes the descriptors of the Information Schema itself. It does not include the schema objects or base tables of the Definition Schema. Each Information Schema view is so specified that a given user can access only those rows of the view that represent descriptors on which that user has privileges.

14 SQL-data SQL-data is data described by SQL-schemas — data that is under the control of an SQL-implementation in an SQL-environment. SQL-data consists entirely of table variables, called base tables.

15 SQL Client and SQL Server
SQL clients and SQL servers Two special processes in SQL environment In terms of 3-tier architecture: SQL client = application server SQL server = database server

16 Connections between C/S
Open a connection CONNECT TO server_name AS conn_name AUTHORIZATION user and password server_name may be DEFAULT Default connection: simply executing SQL statements at a host with a SQL client. States of connections active vs. dormant: many opened, one active SET CONNECTION conn_name terminated: DISCONNECT conn_name

17 Sessions SQL operations performed while a connection is active form a session. Coextensive with the connection Each session has a current catalog, a current schema within that catalog, and an authorized user

18 Modules Module: SQL term for an application program.
Three kinds of modules: Generic SQL interface: each query or other statement is a module by itself. Embedded SQL: the compiled host-language program is a module. True modules: a collection of stored procedures/functions. SQL agent: an execution of a module module vs. SQL agent like program vs. process

19 SQL C-S Interaction SQL-agent Module Environment connection SQL-client
SQL-server session

20 SQL-Implementation An SQL-implementation is a processor that executes SQL-statements, as required by the SQL-agent. The concept denotes an installed instance of some software (DBMS) An SQL-implementation, as perceived by the SQL-agent, includes one SQL-client, to which that SQL-agent is bound, and one or more SQL-servers. SQL-client and SQL-server software may have been obtained from different vendors

21 SQL-Client An SQL-client is a processor, perceived by the SQL-agent as part of the SQL-implementation, that establishes SQL-connections between itself and SQL-servers. One or more SQL-connections may be established for an SQL-client

22 SQL-Server Each SQL-server is a processor, perceived by the SQL-agent as part of the SQL-implementation, that manages SQL-data. Manages the SQL-session taking place over the SQL-connection between itself and the SQL-client. Executes SQL-statements received from the SQL-client, receiving and sending data as required. Maintains the state of the SQL-session, including the authorization identifier and certain session defaults.

23 SQL-Agent An SQL-agent is that which causes the execution of SQL-statements. In the case of the direct invocation of SQL, it is implementation-defined. Alternatively, it may consist of one or more compilation units that, when executed, invoke externally-invoked procedures in an SQL-client module. Compilation unit: A segment of executable code, possibly consisting of one or more subprograms. An SQL-agent is bound to an SQL-client of an SQL-implementation.

24 SQL/Host-Language Interface
Directly Embedded SQL

25 Two Modes of SQL Generic SQL interface In host-language program:
Ad hoc queries Rarely used In host-language program: Directly embedded SQL Call-level interface

26 How it Works? Host language + Embedded SQL DBMS Preprocessors
CLI Host language + Function calls Host language compiler SQL Library Object code

27 Impedance Mismatch SQL vs. Conventional Prog. Language
Different data model eg. Set at a time in SQL, record at a time in CPL Use a single language? No! We need both. SQL for DB operations CPL for general-purpose programming

28 SQL/HL Interface To distinguish SQL with HL statements
EXEC SQL sql statement Data exchange between SQL and HL shared variables: v in HL and :v in SQL SQLSTATE: return code of SQL execution system, for use by HL ‘00000’: no error ‘02000’: tuple not found

29 Declaring Shared Variables
Shared variables are declared in EXEC SQL BEGIN DECLARE SECTION; HL variable declarations … EXEC SQL END DECLARE SECTION; Only types that SQL can deal with Example char snoInput[8], nameInput[10]; char SQLSTATE[6];

30 Using Shared Variables
Shared variables can be used in SQL statements in place of a constant. EXEC SQL INSERT INTO S(sno,name) VALUES (:snoInput,:nameInput);

31 Embeddable SQL Statements
Must not be a query with a result returned. INSERT, DELETE, UPDATE, CREATE, DROP, … SELECT … FROM cannot be used directly. It producees a set of tuples. Impedance mismatch: HL doesn’t support set type directly.

32 Single-row SELECT A query producing a single tuple can store each component in a shared variable. SELECT A1, A2, … INTO :v1, :v2, … FROM … WHERE … Example EXEC SQL SELECT * INTO :sno,:name,:age,:dept FROM S WHERE sno = ‘007’;

33 Set-valued SELECT Cursor: a handle associated with a relation (stored or query result) that ranges over all tuples of the relation and fetch tuples into shared variables one at a time. Usage 1. EXEC SQL DECLARE cursor_name CURSOR FOR query 2. EXEC SQL OPEN cursor_name 3. EXEC SQL FETCH FROM cursor_name INTO shared variables 4. EXEC SQL CLOSE cursor_name

34 Notes on Cursors query may be a base relation or a select-from-where
query is executed when cursor is OPENed, not when it is DECLAREed. OPEN initializes the cursor, ready to retrieve the first tuple. FETCH get the next tuple and move forward. After cursor is CLOSEd, it can be reinitialized.

35 Typical Cursor Code declare shared variables v
EXEC SQL DECLARE c CURSOR FOR SELECT…FROM…WHERE…; EXEC SQL OPEN c; EXEC SQL FETCH FROM c INTO :v; while (strcmp(SQLSTATE,“02000”)){ processing this tuple } EXEC SQL CLOSE c;

36 Modifying DB by Cursor For a cursor ranging over a base table, we can update or delete the current tuple. Delete current tuple DELETE FROM … WHERE CURRENT OF cursor_name Update current tuple UPDATE … SET …

37 Example EXEC SQL DECLARE cursorS CURSOR FOR Student;
EXEC SQL OPEN cursorS; EXEC SQL FETCH FROM cursorS INTO :v1,:v2,:v3,:v4; EXEC SQL DELETE FROM S WHERE CURRENT OF cursorS; EXEC SQL CLOSE cursorS; Delete the 3rd tuple of Student.

38 Cursor Options Protecting against concurrent updates
DECLARE … INSENSITIVE CURSOR FOR SELECT … FROM … Concurrent changes made between one opening and closing are invisible to this cursor.

39 Cursor Options (cont.) Read-only cursors DECLARE … CURSOR FOR
SELECT … FROM … FOR READ ONLY; Read only cursors can run simultaneously with an insensitive cursor for the same relation.

40 Full Cursor Syntax DECLARE cursor_name
[SENSITIVE | INSENSITIVE | ASENSITIVE] [SCROLL | NO SCROLL] CURSOR [WITH HOLD | WITHOUT HOLD] [WITH RETURN | WITHOUT RETURN] FOR query [ORDER BY …] [FOR {READ ONLY | UPDATE [OF column]}]

41 Full Cursor Syntax (cont.)
Sensitivity: default to ASENSITIVE, defined by implementation Scroll: default to NO SCROLL Durability: whether auto close after transaction commit WITH HOLD: remain open until explicit CLOSE Default to WITHOUT HOLD

42 Full Cursor Syntax (cont.)
Return: applicable only to cursors in SQL-callable routines Default to WITHOUT RETURN

43 Dynamic SQL SQL statements are not known at compile time, are read and executed at run-time. Eg. User input SQL at program’s prompts Dynamic SQL support two usages: Two steps EXEC SQL PREPARE v FROM string; parse and produce a query plan EXEC SQL EXECUTE v; Combined EXEC SQL EXECUTE IMMEDIATE string;

44 End


Download ppt "SQL Environment."

Similar presentations


Ads by Google