Перейти к содержимому

Что такое sql назначение языка sql

  • автор:

Что такое sql назначение языка sql

This chapter provides an overview of the Structured Query Language (SQL) and how Oracle Database processes SQL statements.

This chapter includes the following topics:

Introduction to SQL

SQL (pronounced sequel ) is the set-based, high-level declarative computer language with which all programs and users access data in an Oracle database.

Although some Oracle tools and applications mask SQL use, all database tasks are performed using SQL. Any other data access method circumvents the security built into Oracle Database and potentially compromises data security and integrity.

SQL provides an interface to a relational database such as Oracle Database. SQL unifies tasks such as the following in one consistent language:

Creating, replacing, altering, and dropping objects

Inserting, updating, and deleting table rows

Controlling access to the database and its objects

Guaranteeing database consistency and integrity

SQL can be used interactively, which means that statements are entered manually into a program. SQL statements can also be embedded within a program written in a different language such as C or Java.

Oracle Database Development Guide to learn how to choose a programming environment

SQL Data Access

There are two broad families of computer languages: declarative languages that are nonprocedural and describe what should be done, and procedural languages such as C++ and Java that describe how things should be done.

SQL is declarative in the sense that users specify the result that they want, not how to derive it. For example, the following statement queries records for employees whose last name begins with K :

The database performs the work of generating a procedure to navigate the data and retrieve the requested results. The declarative nature of SQL enables you to work with data at the logical level. You need be concerned with implementation details only when you manipulate the data.

The database retrieves all rows satisfying the WHERE condition , also called the predicate , in a single step. The database can pass these rows as a unit to the user, to another SQL statement, or to an application. The application does not need to process the rows one by one, nor does the developer need to know how the rows are physically stored or retrieved.

All SQL statements use the optimizer, a component of the database that determines the most efficient means of accessing the requested data. Oracle Database also supports techniques that you can use to make the optimizer perform its job better.

Oracle Database SQL Language Reference for detailed information about SQL statements and other parts of SQL (such as operators, functions, and format models)

SQL Standards

Oracle strives to follow industry-accepted standards and participates actively in SQL standards committees.

Industry-accepted committees are the American National Standards Institute (ANSI) and the International Organization for Standardization (ISO). Both ANSI and the ISO/IEC have accepted SQL as the standard language for relational databases.

The SQL standard consists of ten parts. One part (SQL/RPR:2012) is new in 2102. Five other parts were revised in 2011. For the other four parts, the 2008 version remains in place.

Oracle SQL includes many extensions to the ANSI/ISO standard SQL language, and Oracle Database tools and applications provide additional statements. The tools SQL*Plus, SQL Developer, and Oracle Enterprise Manager enable you to run any ANSI/ISO standard SQL statement against an Oracle database and any additional statements or functions available for those tools.

Oracle Database SQL Language Reference for an explanation of the differences between Oracle SQL and standard SQL

SQL*Plus User’s Guide and Reference for SQL*Plus commands, including their distinction from SQL statements

Overview of SQL Statements

All operations performed on the information in an Oracle database are run using SQL statements . A SQL statement is a computer program or instruction that consists of identifiers, parameters, variables, names, data types, and SQL reserved words .

SQL reserved words have special meaning in SQL and should not be used for any other purpose. For example, SELECT and UPDATE are reserved words and should not be used as table names.

A SQL statement must be the equivalent of a complete SQL sentence, such as:

Oracle Database only runs complete SQL statements. A fragment such as the following generates an error indicating that more text is required:

Oracle SQL statements are divided into the following categories:

Data Definition Language (DDL) Statements

Data definition language (DLL) statements define, structurally change, and drop schema objects.

DDL enables you to alter attributes of an object without altering the applications that access the object. For example, you can add a column to a table accessed by a human resources application without rewriting the application. You can also use DDL to alter the structure of objects while database users are performing work in the database.

More specifically, DDL statements enable you to:

Create, alter, and drop schema objects and other database structures, including the database itself and database users. Most DDL statements start with the keywords CREATE , ALTER , or DROP .

Delete all the data in schema objects without removing the structure of these objects ( TRUNCATE ).

Unlike DELETE , TRUNCATE generates no undo data , which makes it faster than DELETE . Also, TRUNCATE does not invoke delete triggers

Grant and revoke privileges and roles ( GRANT , REVOKE ).

Turn auditing options on and off ( AUDIT , NOAUDIT ).

Add a comment to the data dictionary ( COMMENT ).

Example 9-1 DDL Statements

The following example uses DDL statements to create the plants table and then uses DML to insert two rows in the table. The example then uses DDL to alter the table structure, grant and revoke read privileges on this table to a user, and then drop the table.

An implicit COMMIT occurs immediately before the database executes a DDL statement and a COMMIT or ROLLBACK occurs immediately afterward. In the preceding example, two INSERT statements are followed by an ALTER TABLE statement, so the database commits the two INSERT statements. If the ALTER TABLE statement succeeds, then the database commits this statement; otherwise, the database rolls back this statement. In either case, the two INSERT statements have already been committed.

Oracle Database Security Guide to learn about privileges and roles

Oracle Database Development Guide to learn about the difference between blocking and nonblocking DDL

Data Manipulation Language (DML) Statements

Data manipulation language ( DML ) statements query or manipulate data in existing schema objects.

Whereas DDL statements change the structure of the database, DML statements query or change the contents. For example, ALTER TABLE changes the structure of a table, whereas INSERT adds one or more rows to the table.

DML statements are the most frequently used SQL statements and enable you to:

Retrieve or fetch data from one or more tables or views ( SELECT ).

Add new rows of data into a table or view ( INSERT ) by specifying a list of column values or using a subquery to select and manipulate existing data.

Change column values in existing rows of a table or view ( UPDATE ).

Update or insert rows conditionally into a table or view ( MERGE ).

Remove rows from tables or views ( DELETE ).

View the execution plan for a SQL statement ( EXPLAIN PLAN ).

Lock a table or view, temporarily limiting access by other users ( LOCK TABLE ).

The following example uses DML to query the employees table. The example uses DML to insert a row into employees , update this row, and then delete it:

A collection of DML statements that forms a logical unit of work is called a transaction . For example, a transaction to transfer money could involve three discrete operations: decreasing the savings account balance, increasing the checking account balance, and recording the transfer in an account history table. Unlike DDL statements, DML statements do not implicitly commit the current transaction.

Oracle Database 2 Day Developer’s Guide to learn how to query and manipulate data

SELECT Statements

A query is an operation that retrieves data from a table or view.

SELECT is the only SQL statement that you can use to query data. The set of data retrieved from execution of a SELECT statement is known as a result set.

The following table shows two required keywords and two keywords that are commonly found in a SELECT statement. The table also associates capabilities of a SELECT statement with the keywords.

Table 9-1 Keywords in a SQL Statement

Specifies which columns should be shown in the result. Projection produces a subset of the columns in the table.

An expression is a combination of one or more values, operators, and SQL functions that resolves to a value. The list of expressions that appears after the SELECT keyword and before the FROM clause is called the select list .

Specifies the tables or views from which the data should be retrieved.

Specifies a condition to filter rows, producing a subset of the rows in the table. A condition specifies a combination of one or more expressions and logical (Boolean) operators and returns a value of TRUE , FALSE , or UNKNOWN .

Specifies the order in which the rows should be shown.

Joins

A join is a query that combines rows from two or more tables, views, or materialized views.

The following example joins the employees and departments tables ( FROM clause), selects only rows that meet specified criteria ( WHERE clause), and uses projection to retrieve data from two columns ( SELECT ). Sample output follows the SQL statement.

The following graphic represents the operations of projection and selection in the join shown in the preceding query.

Figure 9-1 Projection and Selection

Most joins have at least one join condition , either in the FROM clause or in the WHERE clause, that compares two columns, each from a different table. The database combines pairs of rows, each containing one row from each table, for which the join condition evaluates to TRUE . The optimizer determines the order in which the database joins tables based on the join conditions, indexes, and any available statistics for the tables.

Join types include the following:

An inner join is a join of two or more tables that returns only rows that satisfy the join condition. For example, if the join condition is employees.department_id=departments.department_id , then rows that do not satisfy this condition are not returned.

An outer join returns all rows that satisfy the join condition and also returns rows from one table for which no rows from the other table satisfy the condition.

The result of a left outer join for table A and B always contains all records of the left table A , even if the join condition does not match a record in the right table B . If no matching row from B exists, then B columns contain nulls for rows that have no match in B . For example, if not all employees are in departments, then a left outer join of employees (left table) and departments (right table) retrieves all rows in employees even if no rows in departments satisfy the join condition ( employees.department_id is null).

The result of a right outer join for table A and B contains all records of the right table B , even if the join condition does not match a row in the left table A . If no matching row from A exists, then A columns contain nulls for rows that have no match in A . For example, if not all departments have employees, a right outer join of employees (left table) and departments (right table) retrieves all rows in departments even if no rows in employees satisfy the join condition.

A full outer join is the combination of a left outer join and a right outer join.

If two tables in a join query have no join condition, then the database performs a Cartesian join . Each row of one table combines with each row of the other. For example, if employees has 107 rows and departments has 27, then the Cartesian product contains 107*27 rows. A Cartesian product is rarely useful.

Oracle Database SQL Language Reference for detailed descriptions and examples of joins

Subqueries

A subquery is a SELECT statement nested within another SQL statement. Subqueries are useful when you must execute multiple queries to solve a single problem.

Each query portion of a statement is called a query block . In the following query, the subquery in parentheses is the inner query block:

The inner SELECT statement retrieves the IDs of departments with location ID 1800. These department IDs are needed by the outer query block, which retrieves names of employees in the departments whose IDs were supplied by the subquery.

The structure of the SQL statement does not force the database to execute the inner query first. For example, the database could rewrite the entire query as a join of employees and departments , so that the subquery never executes by itself. As another example, the Virtual Private Database (VPD) feature could restrict the query of employees using a WHERE clause, so that the database queries the employees first and then obtains the department IDs. The optimizer determines the best sequence of steps to retrieve the requested rows.

Transaction Control Statements

Transaction control statements manage the changes made by DML statements and group DML statements into transactions.

These statements enable you to:

Make changes to a transaction permanent ( COMMIT ).

Undo the changes in a transaction, since the transaction started ( ROLLBACK ) or since a savepoint ( ROLLBACK TO SAVEPOINT ). A savepoint is a user-declared intermediate marker within the context of a transaction.

The ROLLBACK statement ends a transaction, but ROLLBACK TO SAVEPOINT does not.

Set a point to which you can roll back ( SAVEPOINT ).

Establish properties for a transaction ( SET TRANSACTION ).

Specify whether a deferrable integrity constraint is checked following each DML statement or when the transaction is committed ( SET CONSTRAINT ).

The following example starts a transaction named Update salaries . The example creates a savepoint, updates an employee salary, and then rolls back the transaction to the savepoint. The example updates the salary to a different value and commits.

Oracle Database SQL Language Reference to learn about transaction control statements

Session Control Statements

Session control statements dynamically manage the properties of a user session .

A session is a logical entity in the database instance memory that represents the state of a current user login to a database. A session lasts from the time the user is authenticated by the database until the user disconnects or exits the database application.

Session control statements enable you to:

Alter the current session by performing a specialized function, such as setting the default date format ( ALTER SESSION ).

Enable and disable roles, which are groups of privileges, for the current session ( SET ROLE ).

The following statement dynamically changes the default date format for your session to ‘YYYY MM DD-HH24:MI:SS’ :

Session control statements do not implicitly commit the current transaction.

Oracle Database SQL Language Reference for ALTER SESSION syntax and semantics

System Control Statement

A system control statement changes the properties of the database instance .

The only system control statement is ALTER SYSTEM . It enables you to change settings such as the minimum number of shared servers, terminate a session, and perform other system-level tasks.

Examples of the system control statement include:

The ALTER SYSTEM statement does not implicitly commit the current transaction.

Oracle Database SQL Language Reference for ALTER SYSTEM syntax and semantics

Embedded SQL Statements

Embedded SQL statements incorporate DDL, DML, and transaction control statements within a procedural language program.

Embedded statements are used with the Oracle precompilers. Embedded SQL is one approach to incorporating SQL in your procedural language applications. Another approach is to use a procedural API such as Open Database Connectivity (ODBC) or Java Database Connectivity (JDBC).

Embedded SQL statements enable you to:

Define, allocate, and release a cursor ( DECLARE CURSOR , OPEN , CLOSE ).

Specify a database and connect to it ( DECLARE DATABASE , CONNECT ).

Assign variable names ( DECLARE STATEMENT ).

Initialize descriptors ( DESCRIBE ).

Specify how error and warning conditions are handled ( WHENEVER ).

Parse and run SQL statements ( PREPARE , EXECUTE , EXECUTE IMMEDIATE ).

Retrieve data from the database ( FETCH ).

Overview of the Optimizer

To understand how Oracle Database processes SQL statements, it is necessary to understand the part of the database called the optimizer (also known as the query optimizer or cost-based optimizer ). All SQL statements use the optimizer to determine the most efficient means of accessing the specified data.

Use of the Optimizer

The optimizer generates execution plans describing possible methods of execution.

The optimizer determines which execution plan is most efficient by considering several sources of information. For example, the optimizer considers query conditions, available access paths, statistics gathered for the system, and hints.

To execute a DML statement, Oracle Database may have to perform many steps. Each step either retrieves rows of data physically from the database or prepares them for the user issuing the statement. The steps that the database uses to execute a statement greatly affect how quickly the statement runs. Many different ways of processing a DML statement are often possible. For example, the order in which tables or indexes are accessed can vary.

When determining the best execution plan for a SQL statement, the optimizer performs the following operations:

Evaluation of expressions and conditions

Inspection of integrity constraints to learn more about the data and optimize based on this metadata

Choice of optimizer goals

Choice of access paths

Choice of join orders

The optimizer generates most of the possible ways of processing a query and assigns a cost to each step in the generated execution plan. The plan with the lowest cost is chosen as the query plan to be executed.

You can obtain an execution plan for a SQL statement without executing the plan. Only an execution plan that the database actually uses to execute a query is correctly termed a query plan.

You can influence optimizer choices by setting the optimizer goal and by gathering representative statistics for the optimizer. For example, you may set the optimizer goal to either of the following:

The ALL_ROWS hint instructs the optimizer to get the last row of the result to the client application as fast as possible.

Initial response time

The FIRST_ROWS hint instructs the optimizer to get the first row to the client as fast as possible.

A typical end-user, interactive application would benefit from initial response time optimization, whereas a batch-mode, non-interactive application would benefit from total throughput optimization.

Oracle Database SQL Tuning Guide for more information about the optimizer and using hints

Optimizer Components

The optimizer contains three main components: the transformer, estimator, and plan generator.

The following diagram depicts the components:

Figure 9-2 Optimizer Components

The input to the optimizer is a parsed query. The optimizer performs the following operations:

The optimizer receives the parsed query and generates a set of potential plans for the SQL statement based on available access paths and hints.

The optimizer estimates the cost of each plan based on statistics in the data dictionary. The cost is an estimated value proportional to the expected resource use needed to execute the statement with a particular plan.

The optimizer compares the costs of plans and chooses the lowest-cost plan, known as the query plan, to pass to the row source generator.

Query Transformer

The query transformer determines whether it is helpful to change the form of the query so that the optimizer can generate a better execution plan. The input to the query transformer is a parsed query, which the optimizer represents as a set of query blocks.

Estimator

The estimator determines the overall cost of a given execution plan.

The estimator generates three different types of measures to achieve this goal:

This measure represents a fraction of rows from a row set. The selectivity is tied to a query predicate, such as last_name=’Smith’ , or a combination of predicates.

This measure represents the number of rows in a row set.

This measure represents units of work or resource used. The query optimizer uses disk I/O, CPU usage, and memory usage as units of work.

If statistics are available, then the estimator uses them to compute the measures. The statistics improve the degree of accuracy of the measures.

Plan Generator

The plan generator tries out different plans for a submitted query. The optimizer chooses the plan with the lowest cost.

For each nested subquery and unmerged view, the optimizer generates a subplan. The optimizer represents each subplan as a separate query block . The plan generator explores various plans for a query block by trying out different access paths, join methods, and join orders.

The adaptive query optimization capability changes plans based on statistics collected during statement execution. All adaptive mechanisms can execute a final plan for a statement that differs from the default plan. Adaptive optimization uses either dynamic plans, which choose among subplans during statement execution, or reoptimization, which changes a plan on executions after the current execution.

Oracle Database SQL Tuning Guide to learn about the optimizer components and adaptive optimization

Access Paths

An access path is the technique that a query uses to retrieve rows.

For example, a query that uses an index has a different access path from a query that does not. In general, index access paths are best for statements that retrieve a small subset of table rows. Full scans are more efficient for accessing a large portion of a table.

The database can use several different access paths to retrieve data from a table. The following is a representative list:

Full table scans

This type of scan reads all rows from a table and filters out those that do not meet the selection criteria. The database sequentially scans all data blocks in the segment, including those under the high water mark (HWM) that separates used from unused space (see «Segment Space and the High Water Mark» ).

The rowid of a row specifies the data file and data block containing the row and the location of the row in that block. The database first obtains the rowids of the selected rows, either from the statement WHERE clause or through an index scan, and then locates each selected row based on its rowid.

This scan searches an index for the indexed column values accessed by the SQL statement (see «Index Scans» ). If the statement accesses only columns of the index, then Oracle Database reads the indexed column values directly from the index.

A cluster scan retrieves data from a table stored in an indexed table cluster , where all rows with the same cluster key value are stored in the same data block (see «Overview of Indexed Clusters» ). The database first obtains the rowid of a selected row by scanning the cluster index. Oracle Database locates the rows based on this rowid.

A hash scan locates rows in a hash cluster, where all rows with the same hash value are stored in the same data block (see «Overview of Hash Clusters» ). The database first obtains the hash value by applying a hash function to a cluster key value specified by the statement. Oracle Database then scans the data blocks containing rows with this hash value.

The optimizer chooses an access path based on the available access paths for the statement and the estimated cost of using each access path or combination of paths.

Optimizer Statistics

The optimizer statistics are a collection of data that describe details about the database and the objects in the database. The statistics provide a statistically correct picture of data storage and distribution usable by the optimizer when evaluating access paths.

Optimizer statistics include the following:

These include the number of rows, number of blocks, and average row length.

These include the number of distinct values and nulls in a column and the distribution of data.

These include the number of leaf blocks and index levels.

These include CPU and I/O performance and utilization.

Oracle Database gathers optimizer statistics on all database objects automatically and maintains these statistics as an automated maintenance task. You can also gather statistics manually using the DBMS_STATS package. This PL/SQL package can modify, view, export, import, and delete statistics.

Optimizer statistics are created for the purposes of query optimization and are stored in the data dictionary. Do not confuse these statistics with performance statistics visible through dynamic performance views.

Optimizer Statistics Advisor is built-in diagnostic software that analyzes how you are currently gathering statistics, the effectiveness of existing statistics gathering jobs, and the quality of the gathered statistics. Optimizer Statistics Advisor maintains rules, which embody Oracle best practices based on the current feature set. In this way, the advisor always provides the most up-to-date recommendations for statistics gathering.

Optimizer Hints

A hint is a comment in a SQL statement that acts as an instruction to the optimizer.

Sometimes the application designer, who has more information about a particular application’s data than is available to the optimizer, can choose a more effective way to run a SQL statement. The application designer can use hints in SQL statements to specify how the statement should be run. The following examples illustrate the use of hints.

Example 9-2 Execution Plan for SELECT with FIRST_ROWS Hint

Suppose that your interactive application runs a query that returns 50 rows. This application initially fetches only the first 25 rows of the query to present to the end user. You want the optimizer to generate a plan that gets the first 25 records as quickly as possible so that the user is not forced to wait. You can use a hint to pass this instruction to the optimizer as shown in the SELECT statement and AUTOTRACE output in the following example:

In this example, the execution plan shows that the optimizer chooses an index on the employees.department_id column to find the first 25 rows of employees whose department ID is over 50. The optimizer uses the rowid retrieved from the index to retrieve the record from the employees table and return it to the client. Retrieval of the first record is typically almost instantaneous.

Example 9-3 Execution Plan for SELECT with No Hint

Assume that you execute the same statement, but without the optimizer hint:

In this case, the execution plan joins two indexes to return the requested records as fast as possible. Rather than repeatedly going from index to table as in Example 9-2, the optimizer chooses a range scan of EMP_DEPARTMENT_IX to find all rows where the department ID is over 50 and place these rows in a hash table . The optimizer then chooses to read the EMP_EMP_ID_PK index. For each row in this index, it probes the hash table to find the department ID.

In this case, the database cannot return the first row to the client until the index range scan of EMP_DEPARTMENT_IX completes. Thus, this generated plan would take longer to return the first record. Unlike the plan in Example 9-2, which accesses the table by index rowid, the plan uses multiblock I/O, resulting in large reads. The reads enable the last row of the entire result set to be returned more rapidly.

Oracle Database SQL Tuning Guide to learn how to use optimizer hints

Overview of SQL Processing

This section explains how Oracle Database processes SQL statements. Specifically, the section explains the way in which the database processes DDL statements to create objects, DML to modify data, and queries to retrieve data.

Stages of SQL Processing

The general stages of SQL processing are parsing, optimization, row source generation, and execution. Depending on the statement, the database may omit some of these steps.

The following figure depicts the general stages:

Figure 9-3 Stages of SQL Processing

SQL Parsing

The first stage of SQL processing is SQL parsing . This stage involves separating the pieces of a SQL statement into a data structure that can be processed by other routines.

When an application issues a SQL statement, the application makes a parse call to the database to prepare the statement for execution. The parse call opens or creates a cursor , which is a handle for the session-specific private SQL area that holds a parsed SQL statement and other processing information. The cursor and private SQL area are in the PGA .

During the parse call, the database performs the following checks:

Shared pool check

The preceding checks identify the errors that can be found before statement execution . Some errors cannot be caught by parsing. For example, the database can encounter a deadlock or errors in data conversion only during statement execution.

SQL Optimization

Query optimization is the process of choosing the most efficient means of executing a SQL statement.

The database optimizes queries based on statistics collected about the actual data being accessed. The optimizer uses the number of rows, the size of the data set, and other factors to generate possible execution plans, assigning a numeric cost to each plan. The database uses the plan with the lowest cost.

The database must perform a hard parse at least once for every unique DML statement and performs optimization during this parse. DDL is never optimized unless it includes a DML component such as a subquery that requires optimization.

Oracle Database SQL Tuning Guide for detailed information about the query optimizer

SQL Row Source Generation

The row source generator is software that receives the optimal execution plan from the optimizer and produces an iterative plan, called the query plan , that is usable by the rest of the database.

The query plan takes the form of a combination of steps. Each step returns a row set . The rows in this set are either used by the next step or, in the last step, are returned to the application issuing the SQL statement.

A row source is a row set returned by a step in the execution plan along with a control structure that can iteratively process the rows. The row source can be a table, view, or result of a join or grouping operation.

SQL Execution

During execution, the SQL engine executes each row source in the tree produced by the row source generator. This is the only mandatory step in DML processing.

During execution, if the data is not in memory, then the database reads the data from disk into memory. The database also takes out any locks and latches necessary to ensure data integrity and logs any changes made during the SQL execution. The final stage of processing a SQL statement is closing the cursor.

If the database is configured to use In-Memory Column Store (IM column store), then the database transparently routes queries to the IM column store when possible, and to disk and the database buffer cache otherwise. A single query can also use the IM column store, disk, and the buffer cache. For example, a query might join two tables, only one of which is cached in the IM column store.

Oracle Database SQL Tuning Guide for detailed information about execution plans and the EXPLAIN PLAN statement

Differences Between DML and DDL Processing

Oracle Database processes DDL differently from DML.

For example, when you create a table, the database does not optimize the CREATE TABLE statement. Instead, Oracle Database parses the DDL statement and carries out the command.

In contrast to DDL, most DML statements have a query component. In a query, execution of a cursor places the row generated by the query into the result set.

The database can fetch result set rows either one row at a time or in groups. In the fetch, the database selects rows and, if requested by the query, sorts the rows. Each successive fetch retrieves another row of the result until the last row has been fetched.

Oracle Database Development Guide to learn about processing DDL, transaction control, and other types of statements

Часть 6. Введение в язык SQL

В предыдущих двух статьях данного цикла, опубликованных в № 6 и 7 нашего журнала, мы рассмотрели различные механизмы доступа к данным, включая ADO, BDE и их альтернативы. Теперь мы знаем, как выбрать технологию доступа к данным для той или иной пары «СУБД — средство разработки».

Располагая технологией доступа к данным, можно наконец подумать и о том, каким образом следует манипулировать самими данными и метаданными. Способы манипуляции могут быть специфичными для данной СУБД (например, использование объектов клиентской части этой СУБД для доступа к объектам баз данных) или для данного механизма доступа к данным. Тем не менее существует более или менее универсальный способ манипуляции данными, поддерживаемый почти всеми серверными реляционными СУБД и большинством универсальных механизмов доступа к данным (в том числе при использовании их совместно с настольными СУБД). Этот способ — применение языка SQL (Structured Query Language — язык структурированных запросов). Ниже мы рассмотрим назначение и особенности этого языка, а также изучим, как с его помощью извлекать и суммировать данные, добавлять, удалять и модифицировать записи, защищать данные от несанкционированного доступа, создавать базы данных. Для более подробного изучения SQL мы можем порекомендовать книги Мартина Грабера «Введение в SQL» (М., Лори, 1996) и «SQL. Справочное руководство» (М., Лори, 1997).

Введение

Structured Query Language представляет собой непроцедурный язык, используемый для управления данными реляционных СУБД. Термин «непроцедурный» означает, что на данном языке можно сформулировать, что нужно сделать с данными, но нельзя проинструктировать, как именно это следует сделать. Иными словами, в этом языке отсутствуют алгоритмические конструкции, такие как метки, операторы цикла, условные переходы и др.

Язык SQL был создан в начале 70-х годов в результате исследовательского проекта IBM, целью которого было создание языка манипуляции реляционными данными. Первоначально он назывался SEQUEL (Structured English Query Language), затем — SEQUEL/2, а затем — просто SQL. Официальный стандарт SQL был опубликован ANSI (American National Standards Institute — Национальный институт стандартизации, США) в 1986 году (это наиболее часто используемая ныне реализация SQL). Данный стандарт был расширен в 1989 и 1992 годах, поэтому последний стандарт SQL носит название SQL92. В настоящее время ведется работа над стандартом SQL3, содержащим некоторые объектно-ориентированные расширения.

Существует три уровня соответствия стандарту ANSI — начальный, промежуточный и полный. Многие производители серверных СУБД, такие как IBM, Informix, Microsoft, Oracle и Sybase, применяют собственные реализации SQL, основанные на стандарте ANSI (отвечающие как минимум начальному уровню соответствия стандарту) и содержащие некоторые расширения, специфические для данной СУБД.

Более подробную информацию о соответствии стандарту версии SQL, используемой в конкретной СУБД, можно найти в документации, поставляемой с этой СУБД.

Как работает SQL

Давайте рассмотрим, как работает SQL. Предположим, что имеется база данных, управляемая с помощью какой-либо СУБД. Для извлечения из нее данных используется запрос, сформулированный на языке SQL. СУБД обрабатывает этот запрос, извлекает запрашиваемые данные и возвращает их. Этот процесс схематически изображен на рис. 1.

Как мы увидим позже, SQL позволяет не только извлекать данные, но и определять структуру данных, добавлять и удалять данные, ограничивать или предоставлять доступ к данным, поддерживать ссылочную целостность.

Обратите внимание на то, что SQL сам по себе не является ни СУБД, ни отдельным продуктом. Это язык, применяемый для взаимодействия с СУБД и являющийся в определенном смысле ее неотъемлемой частью.

Операторы SQL

SQL содержит примерно 40 операторов для выполнения различных действий внутри СУБД. Ниже приводится краткое описание категорий этих операторов.

Data Definition Language (DDL)

Data Definition Language содержит операторы, позволяющие создавать, изменять и уничтожать базы данных и объекты внутри них (таблицы, представления и др.). Эти операторы перечислены в табл. 1.

Лекция 12. Введение в sql

Цель занятия: усвоить знания о назначении языка SQL; об основных командах языка.

Назначение языка, классификация операторов.

Структура команды языка.

Работа SQL со множеством пользователей.

1. Назначение языка, классификация операторов

Большинство современных СУБД содержат средства генерации запросов, позволяющих изменять данные (и др. операции). Один из способов манипулирования данными – QBE (queries by example), запрос по образцу. Это средство для визуального связывания таблиц и выбора полей, которые надо отобразить в результате выполнения запроса.

Другое средство – язык запросов SQL (Structured Query Language), представляет собой непроцедурный язык, используемый для управления данными и метаданными реляционных СУБД. Термин «непроцедурный» означает, что на этом языке можно сформулировать, что именно нужно сделать с данными, но нельзя проинструктировать, как это следует сделать. Иными словами, в языке SQL отсутствуют алгоритмические конструкции, такие кап метки, операторы цикла, условные переходы и т.п.

Язык SQL был создан в начале 70-х годов в результате исследовательского проекта IBM, целью которого была разработав языка манипуляции реляционными данными. Первоначально он назывался SEQUEL (Structured English Query Language), затем — просто SQL. Официальный стандарт SQL был опубликован ANSI (American National Standards Institute — Американский национальный институт стандартизации) в 1981 году, и это наиболее часто используемая ныне реализация SQL. Данный стандарт был расширен в 1989 и 1992 годах, поэтому последний стандарт SQL носит название SQL92. В настоящее время ведется работа над стандартом SQL3, содержащим некоторые объектно-ориентированные расширения.

Существует три уровня соответствия стандарту ANSI: начальный, промежуточный и полный. Многие производители серверных СУБД такие как IBM, Informix, Microsoft, Oracle и Sybase, применяют собственные реализации SQL, основанный на стандарте ANSI (отвечающие как минимум начальному уровню соответствия стандарту) и содержащие некоторые расширения, специфические для той или иной СУБД.

Классификация операторов языка

SQL позволяет не только извлекать данные, но и определять структуру данных, добавлять, удалять данные, ограничивать или предоставлять доступ к данным, поддерживать ссылочную целостность.

SQL содержит примерно 40 операторов для выполнения различных действий внутри СУБД большинство из которых представляют собой разного рода манипуляции с данными и метаданными. Эти операторы подразделяются на несколько категорий, которые кратко рассматриваются ниже.

Data Definition Language (DDL) содержит операторы, позволяющие создавать, изменять и уничтожать базы данных и объекты внутри них (таблицы, представления и др)

Create Table — Добавление новой таблицы к базе

Drop Table — Удаление таблицы из базы данных

Alter Table — Изменение структуры имеющейся таблицы

Create View — Добавление нового представления к базе данных

Drop View — Удаление представления из базы данных

Create Index — Создание индекса для данного поля

Drop Index — Удаление существующего индекса

CREATE SCHEMA — Создание новой схемы в базе данных

Data Manipulation Language (DML) содержит операторы, позволяющие выбирать, добавлять, удалять и модифицировать данные. Обратите внимание на то, что эти операторы не обязательно должны завершать транзакцию, внутри которой or. вызваны.

Insert — Добавление строк к таблице

Delete — Удаление строк из таблицы

Update — Изменение данных

Иногда оператор SELECT относят к отдельной категории, называемой Data Query Language (DQL).J

Data Control Language (DCL), иногда называемые операторами Access Control Language, применяются для осуществления административных функций, присваивающих или отменяющих право (привилегию) использовать базу данных, таблицы в базе данных, а также выполнять те или иные операторы SQL.

Grant — Присвоение привилегии.

Revoke — Отмена привилегии.

Транзакция – группа операций над данными, которые либо выполняются, либо отменяются все вместе. Завершение транзакции означает, что все ее операции завершены, и результат их работы сохранен в базе данных. Откат транзакции означает, что все уже выполненные операции, входящие в состав транзакции, отменяются, и все объекты базы данных возвращаются в исходное состояние (для этого СУБД ведет журнал транзакций). Транзакция может состоять из нескольких вложенных транзакций. Некоторые СУБД поддерживают двухфазное завершение транзакций – процесс, позволяющий осуществлять транзакции над несколькими БД, относящимися к одной и той же СУБД. Для поддержки распределенных транзакций (над базами данных, управляемых разными СУБД) используются мониторы транзакций.

Программное обеспечение работы с современными базами данных

История возникновения языка SQL восходит к 1970 году [ [ 3.1 ] ], когда доктор Е.Ф. Кодд предложил реляционную модель в качестве новой модели базы данных. Для доказательства жизнеспособности новой модели данных внутри компании IBM был создан мощный исследовательский проект, получивший название System/R. Проект включал разработку собственно реляционной СУБД и специального языка запросов к базе данных. Так в начале 70-х годов появился первый исследовательский прототип реляционной СУБД. Для этого прототипа разрабатывались и опробовались разные языки запросов, один из которых получил название SEQUEL (Structured English Query Language). С момента создания и до наших дней этот язык претерпел массу изменений, но идеология осталась неизменной.

Период с 1979 года (окончание проекта System/R) до настоящего времени характеризуется развитием и совершенствованием языка SQL и его постоянно увеличивающейся ролью в индустрии, связанной с созданием и эксплуатацией баз данных. Совершенно очевидно, что язык никогда не получил бы мирового признания, если бы на него не было никаких стандартов. Стандартизация – важная часть технологических процессов конца XX века. Именно наличие разработанных и официально признанных стандартов позволило утвердиться многим современным технологиям (не только в индустрии разработки программного обеспечения, но и в многих других сферах человеческой деятельности). Как обстоит дело со стандартами языка SQL и их поддержкой в распространенных СУБД?

Когда ведут речь о стандартах в области, связанной с разработкой программного обеспечения, обычно подразумевают две организации:

  • ANSI (American National Standards Institute) – Американский национальный институт стандартов;
  • ISO ( International Standards Organization) – Международную организацию по стандартизации.

Работа над официальным стандартом языка SQL началась в 1982 году [8] в рамках комитета ANSI. В 1986 году (обратите внимание, сколько времени ушло на разработку стандарта и согласование деталей!) был утвержден первый вариант стандарта ANSI, а в 1987 году этот стандарт был утвержден и ISO. В 1989 году стандарт претерпел незначительные изменения, но именно этот вариант получил название SQL-1 или SQL-89 . В чем особенность SQL-89 ? За время разработки стандарта (1982–1989 гг.) были созданы, представлены на рынке и активно использовались несколько различных СУБД, в которых в том или ином виде был реализован некоторый диалект языка SQL. С учетом того, что разработкой стандартов занимались те же люди, кто внедрял SQL в СУБД, стандарт SQL-89 представлял собой плод множества компромиссов, приведших к наличию в нем большого количества «белых пятен», т.е. мест, которые не были описаны, а отданы на усмотрение разработчиков диалекта. В результате чуть ли не все имеющиеся диалекты стали совместимыми со стандартом, но особой пользы это не принесло.

Следующая реализация стандарта была призвана решить эту проблему. В результате длительных обсуждений и согласований в 1992 году был принят новый стандарт ANSI SQL-2 или SQL-92. SQL-92, который заполнил многие «белые пятна», впервые добавив в стандарт возможности, еще не реализованные в существующих коммерческих СУБД.

Работа над стандартизацией продолжается и далее. Появились стандарты SQL-1999 , SQL-2003. Тем не менее, все эти стандарты не решили всех проблем, связанных с наличием нескольких диалектов языка. Как правило, разработчики как игнорировали, так и игнорируют некоторые положения стандарта, с одной стороны, отказываясь реализовывать некоторые его части и, с другой стороны, реализуя то, что отсутствует в стандарте. Несмотря на имеющиеся отличия, все коммерческие СУБД поддерживают некоторое ядро языка, описанное в стандарте, одинаково. Отличий не очень много, они не носят слишком принципиального характера. Хотя каждая СУБД по-прежнему поддерживает свой диалект языка.

В систему управления базами данных Microsoft SQL Server входит язык Transact-SQL, разработанный на основе одного из стандартов SQL.

11.3.2. Достоинства языка SQL

Для ознакомления с достоинствами языка обратимся к соответствующей литературе [ [ 3.1 ] ]. Вот некоторые из них:

  • межплатформенная переносимость;
  • наличие стандартов;
  • одобрение и поддержка компанией IBM (СУБД DB2);
  • поддержка со стороны компании Microsoft (СУБД SQL Server, протокол ODBC и технология ADO);
  • реляционная основа;
  • высокоуровневая структура;
  • возможность выполнения специальных интерактивных запросов ;
  • обеспечение программного доступа к базам данных;
  • возможность различного представления данных;
  • полноценность как языка, предназначенного для работы с базами данных;
  • возможность динамического определения данных;
  • поддержка архитектуры клиент/сервер;
  • поддержка корпоративных приложений;
  • расширяемость и поддержка объектно-ориентированных технологий;
  • возможность доступа к данным в Интернете;
  • интеграция с языком Java (протокол JDBC);
  • промышленная инфраструктура.
11.3.2. Общая характеристика SQL

Язык запросов SQL основан на операциях реляционной алгебры и, таким образом ориентирован на работу с множествами (отношениями), а не с отдельными записями. Как и в реляционной алгебре , операндами языка являются отношения (таблицы), результатами выполнения операции также являются отношения (таблицы). Таким образом, язык SQL предназначен для выполнения операций над таблицами, причем как над таблицами в целом (создание, удаление, изменение структуры), так и над данными таблиц (выборка, изменение, добавление и удаление). Отметим, что в явном виде язык SQL не является универсальным языком программирования в обычном понимании. В нем отсутствуют операторы условного перехода, организации циклов, позволяющие управлять ходом выполнения программы. Поэтому язык SQL относится к классу непроцедурных языков программирования. Это именно язык запросов к базе данных , который служит исключительно для организации базы данных и работы с ней. Как уже отмечалось выше, для разработки прикладных программ необходимо использовать другие базовые средства программирования, в который операторы языка SQL будут встраиваться. Языку SQL посвящено большое количество литературы, в том числе и учебников. Подробное изучение языка SQL не входит в задачи настоящего курса, это может занимать отдельный курс. Заметим, что этому языку посвящено большое количество литературы, в том числе и учебников. В связи с этим, здесь будут изложены только общие сведения о языке, как фундаментальном инструменте работы с базами данных.

Терминология

Под запросом, реализуемым с помощью языка SQL- запросов к базе данных, понимается команда, предназначенная для выполнения (и выполняемая) системой управления базами данных определяемого этой командой действия с базой данных.

Запрос реализуется с помощью операторов языка SQL. Операторы состоят из отдельных логических частей, называемых предложениями. Стандарты языка SQL регламентируют синтаксис операторов. Несмотря на то, что язык SQL работает с реляционной базой данных, вместо термина «отношение» здесь используется термин «таблица», вместо терминов «кортеж» и «атрибут» используются соответственно термины «строка» и «столбец».

Разновидности SQL

Как отмечалось выше, в отличие от «обычных» языков программирования в SQL отсутствует возможность объявления переменных, нет инструкции IF, нет цикла FOR и т.д. Собственно программирование (разработка прикладных программ) на подобном языке практически невозможно. Поэтому к настоящему моменту используются следующие технологии (режимы) работы с базой данных на языке SQL (в некоторых источниках эти технологии называют разновидностями языка SQL):

  • формирование непосредственно пользователем запроса на языке SQL в интерактивном режиме ( интерактивный SQL );
  • формирование запроса на языке SQL в прикладной программме (программный или встроенный SQL):
    • статическое формирование запроса ( статический SQL );
    • динамическое формирование запроса ( динамический SQL );
    • формирование запроса с помощью библиотек ( API – интерфейсы вызова подпрограмм ).

    В интерактивном режиме работы с базой данных: пользователь работает с базой данных в прямом диалоге: вводит запрос на языке SQL – получает результат, вводит другой запрос – получает другой результат и т.д.

    Встроенный SQL представляется операторами языка SQL, встроенные в прикладные программы, написанные на других языках программирования (в других программных средах). Это дает возможность работы с базой данных с помощью прикладных программ, написанных на других алгоритмических языках, но требует включения дополнительных средств, обеспечивающих интерфейс между операторами языка SQL и соответствующим языком программирования.

    При статическом использовании языка ( статический SQL ) в текст прикладной программы включаются конкретные операторы SQL, и после компиляции исходной программы в выполняемый модуль жестко включаются соответствующие этим операторам функции SQL. Изменения в вызываемых функциях могут здесь определяться только изменениями параметров операторов SQL, инициируемых с помощью переменных языка программирования.

    При динамическом использовании языка ( динамический SQL ) формирование SQL-запросов, соответствующие вызовы SQL-функций для обращения к базе данных осуществляется динамически в ходе выполнения программы.

    Еще одним способом динамического формирования SQL-запросов в прикладной программе является обращение к соответствующим SQL-функциям с помощью специальных интерфейсов программирования приложений (библиотек функций, разработанных для связи прикладной программы и СУБД посредством SQL-запросов).

    В настоящем пособии для всех указанных технологий (разновидностей SQL) будут приведены основные идеи и рассмотрены ключевые концепции. Интерактивный SQL будет рассмотрен более подробно, чем программный. Детальное рассмотрение статического, динамического SQL и различных API-интерфейсов (ODBC, JDBC, DB Library и др.) выходит за рамки нашего курса.

    Краткие итоги: В лекции рассматриваются общие принципы организации программного обеспечения работы с реляционными базами данных, включающего

    • создание и ведение базы данных;
    • создание пользовательских приложений, включающих разработку пользовательского интерфейса по работе с базой данных.

    Рассматриваются подходы к организации доступа к данным (навигационный подход и подход, основанный на использовании интерпретируемых языков запросов). Дается общее представление о языке SQL (история возникновения и стандарты языка SQL , достоинства языка SQL , основная терминология, технологии работы).

    По языку SQL написано достаточно много литературы. Для более подробного знакомства можно указать, в частности [ [ 3.1 ] — [ 5.4 ] ].

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *