Sie sind auf Seite 1von 56

SQL

Structured Query Language


The SQL is an abbreviation for structured query language. SQL is a computer language that is used to interact with database. SQL works with relational database. In many practical applications database is required. For business database is required to store inventory, production, sales, payroll data etc. The computer program that controls the database is called database management system. SQL is used to control all of the functions that a DBMS provides for its users including 1) Data Definition SQL lets the user to define the structure and organization of data stored and relationship among the stored data items. 2) Data Retrieval SQL allows a user or an application program to retrieve stored data from the database and use it. 3) Data manipulation SQL allows a user or an application program to update the database by adding new data, removing old data, and modifying the previously stored data. 4) Access Control SQL can be used to restrict a users ability to retrieve, add, and modify data, thus protecting stored data against unauthorized access. 5) Data Sharing SQL is used to co-ordinate data sharing by concurrent users ensuring that they do not interfere with one another. 6) Data Integrity SQL defines integrity constraints in the database, protecting it from corruption due to inconsistent updates or system failures. SQL is thus a comprehensive language for controlling and interacting with a DBMS SQL is not a complete computer language like COBOL, C< C++, or Java. SQL does not contain if statement for testing conditions, goto statement, or for statement for program flow control. Infact SQL is a database sublanguage, consisting of about forty statements specialized for database management tasks. These statements can be embedded into other languages such as COBOL or C to extend the language for use with database. The Role Of SQL SQL plays many different roles as given below. 1) SQL is an interactive language Users type SQL commands into an interactive SQL program to retrieve data and display it on screen, providing a convenient, easy to use for ad-hoc queries. 2) SQL is a database programming language. Programmers include SQL commands into their application programs to access the data from database. Both user written program and database utility programs (such as report writers, and data entry tools) use this technique for data base access.
Page No : 1 of 56

SQL

3) SQL is a database administration language. The database administration responsible for managing a minicomputer or mainframe database uses SQL to define database structure and control access to the stored data. 4) SQL is a client/server language Personal computer programs use SQL to communicate over a network with database servers that stored shared data. 5) SQL is an internet data access language. Internet web-servers that interact with corporate data and internet applications servers all use SQL as standard language for accessing corporate databases. 6) SQL is a distributed database language. Distributed database management systems use SQL to help distribute data access across many connected computer systems. The DBMS software on each system uses SQL to communicate with other systems sending requests for data access. 7) SQL is a database gateway language. In a computer network with a mix of different DBMS products, SQL is often used in a gateway that allows one brand of DBMS to communicate with other brand. SQL Features and Benefits 1) 2) 3) 4) 5) Vendor independence Portability across computer systems SQL Standards IBM Endorsement (DB2) Microsoft Commitment (ODBC and ADO Open database connectivity, Active/x data objects) 6) Relational foundation 7) High-Level, English like structure 8) Interactive ad-hoc queries 9) Programmatic database access 10) Multiple views of data 11) Complete database language 12) Dynamic data definition 13) Client/Server architecture 14) Extensibility and object technology 15) Internet database access 16) Java integration( JDBC) Java data base connectivity Relational Database Database management systems organize and structure data so that it can be retrieved and manipulated by users and application program. The data structure and access techniques provided by a particular DBMS are called its data model. File management system Before the introduction of database management system, all data permanently stored on a computer system, was stored in files. A file management system usually provided by the computer manufacturer as a part of operating system, kept track of names and location of files. File management system had no data model that is it did not have any idea about the content of files. Knowledge about the contents of a file was embedded in the application

Page No : 2 of 56

SQL

programs. Normally data files contained an item called file description (FD) that described the lay out the data in the file. The problems associated with large file based systems led to the development of DBMS. The idea behind these systems was simple, take the definition of a files contents and structure out of individual programs and store it together with data in database. Hierarchical database One of the most important applications for earlier database management systems was production planning for manufacturing companies. In this model, the product had to be decomposed into assemblies, which were further decomposed into sub assemblies and so on. Handling this list of parts, known as bill of materials. The bill of materials for a product has natural hierarchical structure. In this model, each record is the representation of a specific part. The records had parent/child relationship. To access data in the database program could 1. 2. 3. 4. Find a particular part by a number. Move down to the first child. Move up to its parents. Move side ways to the next child.

Retrieving data in a hierarchical database thus required navigating through the records, moving up, down, and sideways one record at time. One of the most popular hierarchical database management systems was IBMs Information Management System (IMS). Advantages of IMS are given below: 1. Simple structure: The organization of IMS database easy to understand. The database hierarchy paralled that of a company organization chart or a family tree. 2. Parent/Child organization: An IMS database was excellent for representing parent/child relationships such as A is a part of B or A is owned by B. 3. Performance: IMS stored parent child relationships as physical pointers from one data record to another, so that movement through the database was rapid. Because its structure was simple, IMS could place parent and child records close to one another on the disk minimizing the disk input output. IMS is still widely used DBMS on IBM main frames, for application such as ATM transactions, verifying credit card numbers etc.

Page No : 3 of 56

SQL

FIGURE: Hierarchical Database example.


CAR

ENGINE BODY

CHASSIS

ROOF LEFT DOOR RIGHT DOOR HOOD

HANDLE

WINDOW

LOCK

Network Database The simple structure of hierarchical database became its disadvantage when the data had more complex structure, e.g. in an order processing database, a single order may participate in three different parent/child relationships, linking the order to customer who placed it, the sales person who took it, and the product ordered. The structure of this type of data did not fit into IMS; to deal with such complex applications network data model was developed. The network model extended the hierarchal model by allowing a record to participate in multiple parent child relationships, as shown in the following figure CUSTOMERS
A B C

PRODUCTS
D

ORDERS
Page No : 4 of 56

SQL

These relationships were known as sets in the network model. Official standard of network model is known as CODASYL. For programmer, accessing a network database was similar to accessing a hierarchal database. An application program could, 1. 2. 3. 4. Find a specific parent by Key. Move down to the first child in particular set. Move side ways from one child to next in the set. Move up from a child to its parent in another set.

A network database has following advantages. 1. Flexibility Multiple parents/child relationship allowed network database to represent data that did not have simple hierarchal structure. 2. Standardization The CODASYL standard boosted the popularity of network model and mini computer venders such as Digital Equipments Corporation (DEC) and Data General implemented network database. 3. Performance Despite their greater complexity, network databases boosted performance approaching that of hierarchal database. Sets were representing by pointers to physical data records and on some systems database administrator could specify data structuring based on set relationship. Like hierarchical databases network databases were very rigid. The set relationships and structure of records had to be specified in advance. Changing the database structure requires rebuilding the entire database. The Relational Database Relational data model does not allow parent/child structure. It represents the database as a simple row/column of tables of data values. A relational database is a database where all data visible to the user is organized strictly as tables of data values and where all database operations work on these tables. A relational database consists of one or more tables containing data values. Tables The organizing principle in a relational database is the table, a rectangular row/column arrangement of data values. Each table in a database has unique table name that identifies its contents. Consider the table OFFICES given below. OFFICES Office City 10 21 25 05 08 Delhi Mumbai Banglore Chennai Kolkota Region Northern Western South South East Mgr 108 360 200 150 120 Target Sales

10,00,000.00 1,00,000.00 5,00,000.00 20,00,000.00 3,00,000.00 3,50,000.00 30,00,000.00 1,50,000.00 50,000.00 10,000.00
Page No : 5 of 56

SQL

Each row of the above table represents a single physical entity. All the data contained in a particular row of a table applies to that physical entity. Each vertical column of table represents one item of that is stored in the database. For example, the city column holds the location of each office. Each row of a table contains exactly one data value in each column. Each column of a table, all data values in that column hold the same type of data. Each column in a data table has a column name, which is usually written as a heading at the top of column. The columns of a table must have different names, but there is prohibition against two columns in two different tables having identical names. The columns have left to right order, which is defined when the table is first created. A table should have at least one column; the maximum limit on number of columns in a table is 255. Unlike the columns rows do not have any order. Two consecutive database queries to display the contents of a table may list the rows in different order. A table can have any number of rows. A table with zero rows is legal and called empty table. An empty table still has its structure imposed by its columns but contains no data. Primary Keys Because the rows of a relational table are unordered, you cannot select a specific row by its position in the table. In a well designed relational database every table has some column or combination of columns whose values are uniquely identify each row in the table. This column (or columns) is called the primary key of the table. From the previous table OFFICES it can be seen that column office and city may be chosen as primary key. However, if company opens a office in the same city it cannot be chosen as primary key. The primary key has a different unique value for each row in a table, so no two rows of a table with a primary key are exact duplicates of one another. A table where every row is called different from all other is called a relation in mathematical terms. The name relational database comes from this term, because relations are at the heart of a relational database. Relationships One of the major differences between relational model and earlier data models is that explicit pointers, such as parent/child relationships of a hierarchical database are banned from relational databases. These relationships do exist in relational databases, for example consider the following tables of a database OFFICES Office 22 11 10 SALESREPS Emp_no 105 106 101 City New Delhi Mumbai Bangalore Name Ravi Kiran Subhash Region North West South Age 30 22 25 Rep_office 22 11 10
Page No : 6 of 56

SQL

Rows of OFFICES table have relationships with rows of SALESREPS table. Parent/child relationship between a sales office and the people who work there is not lost by the relational model, but not represented by an explicit pointers stored in the database. Instead, the relationship is represented by column data values stored in the two tables. Foreign Keys A column in one table whose value matches the primary key in some other table is called a foreign key. Consider the above given table of a database Rep_office column is a foreign key for the OFFICES table. Although Rep_office is a column in the SALESREPS table, the values that this column contains are office numbers. They match values in the office columns, which is the primary key for the OFFICES table. Primary key and Foreign key together create parent/child relationship. Just as a combination of columns can serve as primary key of a table, a foreign key can also be a combination of columns. Codds Twelve Rules The information Rule : All information in the relational database is represented explicitly at the logical and in the exactly one way by values in the tables. Guaranteed access rule : Each and every datum in a RDB is guaranteed to be logically accessible by resorting to a combination of table name, primary key value and column name. Systematic Treatment of NULL values : NULL values are supported in a fully relational DBMS for representing missing information and in application, in a systematic way in dependent of data types. Dynamic Online Catalogue based on relational model : The data base description is represented at logical level in the same way as ordinary data, so that authorized users can apply the relational language to its interrogation as they apply to the regular data. Comprehensive data sub language: A relational system may support several languages and various models of terminal use. There must be at least one language whose statement are expressible, per some well defined syntax, as character strings and that is comprehensive in supporting following items. Data definition View definition Data manipulation Integrity constraints Authorization Transaction boundaries

View updating rule: All the views that are theoretically updateable are also updateable by the system. High level Insert, Update, and Delete : the capability of handling a base relation or a derived relation as a single operand not only to retrieval of data but also to the insertion, update and deletion of data.
Page No : 7 of 56

SQL

Physical Data independence: Application programs and terminal activities remain logically unimpaired whenever changes are made in either storage representations or access methods. Logical data independence : Application programs and terminal activities remain logically unimpaired when information changes of any kind that theoretically permit unimpairment are made to the database tables. Integrity Independence : Integrity constraints specific to a particular relational database must be definable in the relational data sub language and storable in the catalog, not in application programs. Distribution Independence: A relational DBMS has distribution independence. Nonsubversion Rule : If a relational system has a low level language that low level can not be used to subvert or bypass the integrity rules and constraints expressed in the higher level relational language.

SQL Basics
The main body of SQL language consists of 40 statements as given below. Data manipulation Statements SELECT INSERT DELETE UPDATE Retrieves data from database Adds new rows of data to the database Removes rows of data from database Modifies existing database

Data Definition statements CREATE TABLE DROP TABLE ALTER TABLE CREATE VIEW DROP VIEW CREATE INDEX DROP INDEX CREATE SCHEMA DROP SCHEMA CREATE DOMAIN ALTER DOMAIN DROP DOMAIN Access Control GRANT REVOKE Transaction Control COMMIT ROLLBACK SET TRANSACTION Adds new table to the database Removes table form database Changes the structure of existing table Adds new view to the database Removes view form database Builds an index for a column Removes an index for a column Adds new schema to the database Removes schema form database Adds a new data value domain Changes a domain definition Removes a domain from database Grants user access privileges Removes user access privileges Ends the current transaction Aborts the current transaction Defines data access characteristics of the current transaction
Page No : 8 of 56

SQL

Programmatic SQL Statements DECLARE EXPLAIN OPEN FETCH CLOSE PREPARE EXECUTE DESCRIBE Defines a cursor for query Describes the data access plan for a query Opens a cursor to retrieve query results Retrieves a row of query results Closes a cursor Prepares a SQL statement for dynamic execution Executes a SQL statement dynamically Describes a prepared query

A view is a virtual table in the database whose contents are defined by a query. To the database user, the view appears just like a real table with a set of named columns and rows of data. But, unlike a real table, a view does not exist in the database a stored set of data values. An index is a structure that provides rapid access to the rows of table based on the values of one or more columns. Schema is a collection of tables. The set of data values that a column can contain is called the domain of the column. A transaction is a sequence of one or more SQL statements that together form a logical unit of work. The SQL statements that form the transaction are typically closely related and perform independent actions. Each statement in the transaction performs some part of a task, but all of them are required to complete the task. Grouping the statements as a single transaction tells the DBMS that entire sequence should be executed automatically all statements must be completed for the database to be in a consistent state. Each statement requests a specific action from DBMS, such as creating a new table, retrieving data, or inserting new data into the database. Assigning them unique names identifies the objects in a SQL based database. Names are used in SQL statements to identify the database object on which the statement should act. The most fundamental named objects in a relational database are table names, column names and user name. The names are extended to schemas, constraints, domains, and several other types of objects. The original ANSI/ISO standard specified that SQL names must contain 1 to 18 characters, must begin with a letter and may not contain any spaces or special punctuation characters. The SQL2 standard increased the maximum number to 128 characters. Table names are chosen to be short and descriptive. Different user can create tables with same names. DBMS uses appropriate table depending on which user is requesting data. With prior permission one can refer to the tables owned by other users, by using qualified table name. The qualified table name specifies both the name of tables owner and name of table separated by a period(.). Example SHYAM.BIRTHDAYS. When a column name is specified in a SQL statement, SQL determines from the context which column is intended. If the statement involves two columns with same name from two different tables one must use qualified column name. A qualified column name specifies both name of table containing the column and the name of column separated by period. For example: SALESREPS.SALES
Page No : 9 of 56

SQL

Data types: Typical data types of SQL are, Integers Decimal numbers Floating-point numbers Fixed length character string Variable length character string This type allows a column to store character strings that vary in length from row to row, upto some maximum length. Money amounts SQL supports MONEY or CURRENCY type data, which is usually stored in decimal or floating-point numbers. Dates and Time SQL supports dates and times along with the required precision (tenths or hundredths of second) Boolean data Long text SQL supports columns that store long text strings (typically 32000 or 65000 characters). This allows database to store entire documents, product descriptions, technical papers, resumes, etc. to be included in columns. Unstructured byte stream Columns containing this data are used to store compressed video images, executable codes etc. Asian characters Following table shows the data types.
CHAR(length) CHARACTER (length)

Fixed length character string variable length character string Fixed length national character string variable length national character string integer numbers small integers Fixed length bit string variable length bit string
Page No : 10 of 56

VARCHAR(length) CHAR VARYING(length) CHARACTER VARYING(length) NCHAR(length) NATIONAL CHAR(length) NATIONAL CHARACTER (length) NCHAR VARYING(length) NATIONAL CHAR VARYING(length) NATIONAL CHARACTER VARYING(length) INTEGER INT SMALLINT BIT(length) BIT VARYING(length)

SQL

NUMERIC(precision, scale) DECIMAL(precision, scale) DEC(precision, scale) FLOAT(precision) REAL DOUBLE PRECISION DATE TIME(precision) TIME STAMP(precision) INTERVAL

decimal numbers floating point numbers low precision floating point numbers high precision floating point numbers calendar date clock time date and time time interval

The SELECT statement retrieves data from database and returns it in the form of query results. Example of SELECT statement is given below SELECT CITY, TARGET, SALE FROM OFFICES For this statement query result is given below CITY New Delhi Mumbai Bangalore Channai TARGET 30,00,000.00 10,00,000.00 5,00,000.00 8,00,000.00 SALES 15,00,000.00 10,00,000.00 8,00,000.00 10,00,000.00

SELECT statement specifies the column and FROM clause mentions the name of table from which data is to be read. SELECT statement can be used in a variety of ways as discussed below Calculated columns In the SELECT statement arithmetic expressions can be included as columns and calculated query results can be made available as given below. SELECT CITY, REGION, (SALES TARGET) FROM OFFICES Query results CITY New Delhi Mumbai Bangalore Chennai REGION North West South South (SALES TARGET) -15,00,000.00 0.00 3,00,000.00 2,00,000.00

SQL statements can also include constants in the SELECT list. SELECT CITY, HAS SALES OF, SALES

Page No : 11 of 56

SQL

Query result will be CITY New Delhi Mumbai Bangalore HAS SALES OF HAS SALES OF HAS SALES OF HAS SALES OF SALES 3,00,000.00 5,00,000.00 8,00,000.00

The following statement can select all columns of a table SELECT * FROM OFFICES SELECT ALL statement can also be used with calculated columns as given below SELECT *, (DALES TARGET) FROM OFFICES DISTINCT keyword can be used to eliminate duplication of rows in a query result. Consider the following examples SELECT MGR FROM OFFICES MGR 108 106 104 108 In this result 108 is duplicated SELECT DISTINCT MGR FROM OFFICES MGR 104 106 108 WHERE clause is used to specify the rows to be retrieved. SELECT CITY, SALES, TARGET FROM OFFICES WHERE SALES > TARGET CITY Mumbai Bangalore SALES 6,00,000.00 8,00,000.00 TARGET 5,00,000.00 3,00,000.00

Any particular row can also be selected as given below SELECT NAME, SALES, QUOTA FROM SALESREPS WHERE EMP_NO = 105 NAME Ravi SALES 10,00,000.00 QUOTA 8,00,000.00
Page No : 12 of 56

SQL

Search conditions The five basic search conditions are given below Comparison test Comparison test compares the value of one expression to the value of another expression. The operators available to comparison are = <> < > <= >= Example : SELECT CITY, SALES, TARGET FROM OFFICES WHERE SALES < (0.8*TARGET) CITY Delhi SALES 3,00,000.00 TARGET 5,00,000.00

SELECT CITY,MGR FROM OFFICES WHERE MGR <> 108 CITY Mumbai Bangalore Range Test Range test, tests whether the value of an expression falls within a specified range of values. SELECT ORDER_NO, AMOUNT FROM ORDERS WHERE AMOUNT BETWEEN 20,000.00 AND 29,999.99 ORDER_N0 AMOUNT 1134 22,500.00 1234 25,000.00 Set Membership test Checks whether the value of an expression matches one of a set of values. It tests whether a data value matches one of a list of target values. SELECT NAME, QUOTA, SALES FROM SALESREPS WHERE RE_OFFICE IN (11,13,22) NAME Bill Adams Mary Jones Sam clerk Nancy Angeli QUOTA 3,00,000.00 3,00,000.00 2,50,000.00 5,00,000.00 SALES 3,50,000.00 2,50,000.00 2,99,000.00 2,00,000.00 MGR 106 104

Page No : 13 of 56

SQL

Pattern Matching Test The pattern matching test ( LIKE ) checks to see whether the data value in a column matches a specified pattern. SELECT COMPANY, CREDIT_LIMIT FROM CUSTOMERS WHERE COMPANY LIKE Smith% Corp Smith corp, Smithson Corp, Smithsen Corp But this will not be Smith Inc The percent sign ( %) wild card character matches any sequence of zero or more characters. The underscore wild card matches any single character. To match the wild card character escape characters are used. In the following example $ is used as escape character. SELECT ORDER_NO, PRODUCT FROM ORDERS WHERE PRODUCT LIKE A$%BC% ESCAPE $ The first percent sign in the pattern , which follows as escape character is treated as literal percent sign, the second percent sign is treated as a wild card. Null Value Test Checks whether a column has a NULL value ( unknown ) SELECT NAME FROM SALESREPS WHERE REP_OFFICE IS NULL NAME Tom Snyder Compound Search Conditions To form the compound search conditions AND, OR and NOT operators are available in SQL. SELECT NAME, QUOTA, SALES FROM SALESREPS WHERE SALES < QUOTA OR SALES< 30,000.00 NAME Sam Clerk Bob Smith QUOTA 45,000.00 20,000.00 SALES 25,000.00 15,000.00

Sorting Query Results Like the rows of a table in the data base, the rows of query results are not arranged in any particular order. Query results can be arranged by ORDER BY Clause. SELECT CITY, REGION, SALES FROM OFFICES ORDER BY REGION, CITY Results are as Below :
Page No : 14 of 56

SQL

CITY Kolkata Delhi Banglore Chennai Mumbai

REGION Eastern Northern Southern Southern Western

SALES 30,000.00 15,000.00 10,000.00 20,000.00 30,000.00

Combining Query Results Occasionally it is convenient to combine the query results of two or more queries into a single table of query results. SQL supports the facility through the UNION clause. SELECT MFR_ID, PRODUCT_ID FROM PRODUCTS WHERE PRICE > 2,000.00 UNION SELECT DISTINCT MFR, PRODUCT FROM ORDERS WHERE AMOUNT > 3,000.00 ACI ACI IMM REI REI 4100Y 4100Z 775C 2A44L 2A44R

Duplicate rows can also be included by using UNION ALL clause SELECT MFR_ID, PRODUCT_ID FROM PRODUCTS WHERE PRICE > 2,000.00 UNION ALL SELECT DISTINCT MFR, PRODUCT FROM ORDERS WHERE AMOUNT > 3,000.00 ACI REI ACI IMM REI REI 4100Y 2A44L 4100Z 775C 2A44L 2A44R

The combined query results of UNION clause can be sorted by using ORDER BY clause. SELECT MFR_ID, PRODUCT_ID FROM PRODUCTS WHERE PRICE > 2,000.00 UNION SELECT DISTINCT MFR, PRODUCT FROM ORDERS WHERE AMOUNT > 3,000.00 ORDER BY 1,2 ACI ACI IMM REI REI 4100Y 4100Z 775C 2A44L 2A44R In this example sorting is done by manufacturer and product.

Page No : 15 of 56

SQL

Multiple Unions can also be formed. SELECT * FROM A UNION (SELECT * FROM B UNION SELECT * FROM C ) Multi Table Queries (JOINS) Consider the following tables ORDERS ORDER_NO ORDER_DATE 12134 14-DEC-89 11356 11-JAN-90 12567 15-FEB-92

CUST 2117 2008 2218

REP 106 104 108

QTY 7 100 10

AMT 30,000.00 50,000.00 10,000.00

CUSTOMERS CUST_NO COMPANY CUST_REP CREDIT 55,000.00 106 BPL 2008 10,000.00 104 HMT 2010 50,000.00 108 BHEL 2117 Following example shows the reading of data from these tables. SELECT ORDER_NO, AMT, COMPANY FROM ORDERS, CUSTOMERS WHERE CUST=CUST_NO ORDER_NO 12134 11356 AMT 30,000.00 50,000.00 COMPANY BHEL BPL

The process of forming pairs of rows by matching the contents of related columns is called joining the tables. The resulting table is called a join between the two tables. Joining columns from different tables is essential because rows in a table are not ordered. A join based on an exact match between two columns is called equi-join. Parent /Child Queries The most common multi table queries involve two tables that have a natural parent/child relationship. The query about orders and customers in the previous example is an example of such a query. Each order (child) has an associated customer (parent) and each customer (parent) can have many associated orders (children). The pair of rows that generate the query results are parent/child row combinations. OFFICES OFFICE 22 11 14 13 10 CITY Mumbai Delhi Banglore Chennai Kolkota REGION Western Northern Southern Southern Eastern MGR TARGET 108 10,00,000.00 106 5,00,000.00 104 5,00,000.00 104 3,50,000.00 105 10,00,000.00 SALES 5,00,000,00 3,50,000.00 3,00,000.00 3,00,000.00 3,50,000.00
Page No : 16 of 56

SQL

SALESREPS EMPNO 108 106 104 104 105 NAME Ravi Kiran Subhash Subhash Prakash AGE 30 28 32 32 30 REPOFF 22 11 14 13 10 TITLE MGR MGR MGR MGR MGR

Consider the example SELECT NAME, CITY, REGION FROM SALESREPS, OFFICES WHERE REPOFF = OFFICE NAME Ravi Kiran Subhash Subhash Prakash CITY Mumbai Delhi Banglore Chennai Kolkota REGION Western Northern Southern Southern Eastern

The SALESREPS (Child) table contains REPOFF, a foreign key for OFFICES table. This relationship is used to find the correct OFFICES row for each salesperson, so that the correct city and region can be included in the query results. The search condition that specifies the matching columns in a multi table query can be combined with other search conditions to further restrict the contents of a query results. SELECT CITY, NAME, TITLE FROM OFFICES, SALESREPS WHERE MGR = EMPNO AND SALES > 3,00,000.00 CITY Mumbai Delhi Kolkata NAME Ravi Kiran Prakash TITLE MGR MGR MGR

Multiple columns can be matched to form a join as given in the following example. SELECT NAME, CITY, REGION FROM OFFICES, SALESREPS WHERE OFFICE = REPOFF AND EMPNO = MGR NAME Ravi Kiran Subhash Subhash Prakash CITY Mumbai Delhi Banglore Chennai Kolkata REGION Western Northern Southern Southern Eastern

SQL can combine data from three or more tables using same techniques used for two table queries.

Page No : 17 of 56

SQL

SELECT ORDER_NO, AMT, COMPANY,NAME FROM ORDERS, CUSTOMERS, SALESREPS WHERE CUST = CUST_NO AND REP = EMPNO AND AMT > 3,00,000.00 ORDER_N0 2010 2117 Non-Equi joins The term join applies to any query that combines data from two tables by comparing the values in a pair of columns from tables. Although joins based on equality between matching columns are most common joins, SQL allows you to join the tables based on other comparison operators. SELECT NAME, TARGET, CITY, SALES FROM SALESREPS, OFFICES WHERE TARGET > SALES NAME Ravi Kiran Subhash Subhash Prakash TARGET 1,00,000.00 3,50,000.00 5,00,000.00 5,50,000.00 1,00,000.00 CITY Mumbai Delhi Banglore Chennai Kolkata SALES 5,00,000.00 3,50,000.00 3,00,000.00 3,00,000.00 35,000.00 AMT 1,00,000.00 5,00,000.00 COMPANY HMT BHEL NAME Subhash Ravi

Qualified column Names If select list includes a column name, which is existing in two or more tables referred in the FROM clause, leads to ambiguity. For example consider the following example. SELECT NAME, SALES, CITY FROM SALESREPS, OFFICES WHERE REPOFF = OFFICE Query result Error :- Ambiguous column name SALES. In such cases qualified column names should be used. SELECT NAME, SALESREPS.SALES, CITY FROM SLAESREPS, OFFICES WHERE REPOFF = OFFICE NAME Ravi Kiran Subhash Subhash Prakash SALESREPS.SALES CITY 1,00,000.00 Mumbai 3,50,000.00 Delhi 5,00,000.00 Banglore 5,50,000.00 Chennai 1,00,000.00 Kolkata

Page No : 18 of 56

SQL

In the multi-table query the asterisk (*) selects all columns of all tables in the FROM clause. SELECT * FROM SALESREPS, OFFICES WHERE REPOFF = OFFICE Following example selects all columns of SALESREPS table and selects only two columns of OFFICES table SELECT SALESREPS.*, CITY, REGION FROM SALESREPS, OFFICES WHERE REPOFF = OFFICE Self-Joins Some multi-table queries involve a relationship that a table has with itself. For example consider the following example. SALESREPS EMPNO NAME 105 Kiran 109 Robert 102 Dev 104 Tom 106 Clerk 107 Prakash AGE 37 31 30 28 32 30 REPOFF 13 11 21 22 21 14 TITLE SR SR VPS SM SM SR HIRE_DATE 12-JUN-98 15-FEB-99 14-FEB-80 19-MAY-85 20-APR-89 10-APR-89 MGR 104 106 NULL 102 102 104

If it is required to list the names of sales persons and their managers, the statement can be written as SELECT NAME, NAME FROM SALESREPS, SALESREPS WHERE MGR = EMPNO Above statement is illegal because it has duplicate references to SALESREPS table in FROM clause. If we write the statement as SELECT NAME, NAME FROM SALESREPS WHERE MGR = EMPNO Above statement is legal but does not produce any result because search condition is not true for any row of the table. In such cases table alias is used. Table alias is an imaginary duplication of a table.
SELECT EMPS.NAME, MGRS.NAME FROM SALESREPS EMPS, SALESREPS MGRS WHERE EMPS.MGR = MGRS.EMPNO

EMPS.NAME Kiran Robert Tom Clerk Prakash

MGRS.NAME Tom Clerk Dev Dev Tom


Page No : 19 of 56

SQL

In the above example the table SALESREPS is referred to by different names EMPS and MRS. Therefore, EMPS and MGRS are the aliases of this table. Above statement can also be written as SELECT EMPS.NAME, MGRS.NAME FROM SALESREPS, SALESREPS MGRS WHERE EMPS.MGR = MGRS.EMPNO Table Multiplication The product of two tables is another table (product table), which consists of all possible pairs of rows from the two tables. The columns of product are all the columns of first table followed by all the columns of second table. Consider the following tables. PERSON1 NAME Ravi Kiran PERSON2 NAME Prakash Dev CITY Mumbai Banglore CITY Kolkata Banglore

The product can be written as SELECT PERSON1.NAME, PERSON2.CITY FROM PERSON1, PERSON2 PERSON1.NAME Ravi Ravi Kiran Kiran PERSON2.NAME Kolkata Banglore Kolkata Banglore

Notice that above statement does not have WHERE clause. Select all can also be used in the product of tables statement. Rules of Multi-table query processing. 1. If the statement is a UNION of SELECT statements apply steps 2 to 5 to each of the statements to generate their individual query results. 2. Form the product of tables named in the FROM clause. If the FROM clause has a single table name, the product is that table itself. 3. If there is a WHERE clause, apply its search condition to each row of the product table, retain those rows for which search condition is TRUE ( discard others ). 4. For each remaining row, calculate the value of each item in the select list to produce a single row of query result.
Page No : 20 of 56

SQL

5. If SELECT DISTINCT is specified, eliminate any duplicate rows of query results that were produced. 6. If the statement is UNION of SELECT statement, merge the query results for the individual statements into a single table of query results. Eliminate duplicate rows unless UNION ALL is specified. 7. If there is an ORDER By clause, sort the result as specified. Outer-Joins In the previous examples of joins, it can be noticed that join is formed by the matching columns. Only those rows for which search condition is TRUE are listed in the query results and the others are dropped. Such a join is called inner join. Consider the following tables. P1 NAME Ravi Kiran Subhash Dev Prakash CITY Banglore Mumbai Kolkata Kolkata Chennai P2 NAME Kumar Robert Tom Clerk Jones CITY Banglore Mumbai Chennai Pune Hyderabad

If we are interested in making the pair of persons living in the same city, statement can be written as SELECT * FROM P1, P2 WHERE P1.CITY = P2.CITY P1.NAME Ravi Kiran Prakash P1.CITY Banglore Mumbai Chennai P2.NAME Kumar Robert Tom P2.CITY Banglore Mumbai Chennai

In the above query results if we want to include the unmatched results, then the resultant join is called outer join. The statement for outer join is written as SELECT * FROM P1, P2 WHERE P1.CITY* = *P2.CITY P1.NAME Ravi Kiran Prakash Suhash Dev NULL NULL P1.CITY Banglore Mumbai Chennai Kolkata Kolkata NULL NULL P2.NAME Kumar Robert Tom NULL NULL Clerk Jones P2.CITY Banglore Mumbai Chennai NULL NULL Pune Hyderabad

In this case unmatched rows from each table are listed. This type of join is referred to as full outer join.
Page No : 21 of 56

SQL

If only unmatched rows from first table are included in the query result then the join is called as left outer join. On the other hand if it includes the unmatched results from second table only then it is called right outer join. The examples of left and right outer joins are given below. SELECT * ( left outer join ) FROM P1, P2 WHERE P1.CITY* = P2.CITY P1.NAME Ravi Kiran Prakash Suhash Dev P1.CITY Banglore Mumbai Chennai Kolkata Kolkata P2.NAME Kumar Robert Tom NULL NULL P2.CITY Banglore Mumbai Chennai NULL NULL

SELECT * ( right outer join ) FROM P1, P2 WHERE P1.CITY = *P2.CITY P1.NAME Ravi Kiran Prakash NULL NULL P1.CITY Banglore Mumbai Chennai NULL NULL P2.NAME Kumar Robert Tom Clerk Jones P2.CITY Banglore Mumbai Chennai Pune Hyderabad

In SQL2 inner joins are represented in alternative way as SELECT * FROM P1 INNER JOIN P2 ON P1.CITY = P2.CITY SELECT * FROM P1 INNER JOIN P2 ON ( P1.CITY = P2.CITY ) AND (P1.AGE = P2.AGE ) SELECT * FROM P1 INNER JOIN P2 USING (CITY, AGE ) SELECT * FROM P1 INNER JOIN P2 ON ( P1.CITY = P2.CITY ) AND (P1.AGE > P2.AGE ) SELECT * FROM P1 NATURAL INNER JOIN P2

( Columns with same name in P1 and P2 are used for matching )

Page No : 22 of 56

SQL

Outer joins in SQL2 SELECT * FROM P1 FULL OUTER JOIN P2 ON P1.CITY = P2.CITY SELECT * FROM P1 NATURAL OUTER JOIN P2 SELECT * FROM P1 FULL OUTER JOIN P2 USING ( CITY ) SELECT * FROM P1 LEFT OUTER JOIN P2 USING ( CITY ) SELECT * FROM P1 RIGHT OUTER JOIN P2 USING ( CITY ) Cross join is nothing but product of tables. SELECT * FROM P1 CROSS JOIN P1 SELECT * FROM P1, P2 Unions can also be joined. SELECT * FROM P1 UNION JOIN P2

Summary Queries SQL provides the following column functions. SUM() Computes the sum of a column AVG() Computes the average value of a column MIN() Finds the smallest value in a column MAX() Finds the maximum value in a column COUNT() Counts the number of values in a column COUNT(*) Counts the rows of a query result

Page No : 23 of 56

SQL

The SUM() column function computes the SUM of a column of data values. The data in the column must be numeric type ( integer, decimal, floating point or money type ) SELECT SUM(QUOTA, SUM(SALES) FROM SALESREPS SUM(QUOTA) 7,00,000.00 SUM(SALES) 8,00,000.00

The AVG() column function computes the average value of a column of data values. As in case of SUM() column function data in the column must be numeric type. SELECT AVG(PRICE) FROM PRODUCTS WHERE MFR-ID = ACI AVG(PRICE) 80,000.00 The MIN() and MAX() column functions find smallest and largest values in a column respectively. The data in the column contain numeric, string or date/time information. SELECT MIN(QUOTA), MAX(QUOTA) FROM SALESREPS MIN(QUOTA) 2,00,000.00 MAX(QUOTA) 5,00,000.00

In case of strings, the comparison is done according to ASCII ( PC/minicomputer ) or EBCDIC. In case of ASCII digits, capital letters and small letters is the sequence. In case of EBCDIC small letters, capital letters and digits is the order. The COUNT() column function cunts the number of data values in a column. The data in the column can be any type. SELECT COUNT(CUST_NO) FROM CUSTOMERS COUNT(CUST_NO) 21 SELECT COUNT(NAME) FROM SALESREPS WHERE SALES > QUOTA SELECT COUNT(AMOUNT) FROM ORDERS WHERE AMOUNT > 25,000.00 COUNT(AMOUNT) 4 COUNT(*) Column function counts rows of query result rather than data values of a column. SELECT COUNT(*) FROM ORDERS WHERE AMOUNT > 25,000.00 COUNT(*) 4

Page No : 24 of 56

SQL

The NULL values in the column are ignored by the column functions. Duplicate values can be eliminated from a column before applying it to a column function. To eliminate duplicate values, the key word DISTINCT is included before the column function argument, immediately after the opening parenthesis. SELECT COUNT(DISTINCT TITLE ) FROM SALESREPS COUNT(DISTINCT TITLE ) 3 Following example demonstrates the use of GROUP BY clause. SELECT REP, AVG(AMOUNT) FROM ORDERS GROUP BY REP REP 101 104 108 106 110 AVG(AMOUNT) 8,00,000.00 4,50.000.00 3,00,000.00 5,00,000.00 2,00,000.00

The first query is a simple summary query, the second query produces several summary rows one row for each group, summarizing the orders taken by a single sales person. SQL can group query results based on the contents of two or more columns. SQL with multiple grouping columns, provides only a single level of grouping. SELECT REP, CUST, SUM(AMOUNT) FROM ORDERS GROUP BY REP,CUST REP 101 101 101 105 105 105 102 102 102 CUST 2102 2108 2105 2110 2105 2110 2105 2115 2116 SUM(AMOUNT) 3,00,000.00 2,00,000.00 1,00,000.00 2,00,000.00 1,00,000.00 3,00,000.00 5,00,000.00 4,00,000.00 5,00,000.00

Many SQL versions provide, the GROUP BY clause which will automatically sort the query results. However, ORDER BY clause can be used to have any order for sorting as given below. SELECT CUST, REP, SUM(AMOUNT) FROM ORDERS GROUP BY CUST, REP ORDER BY CUST, REP

Page No : 25 of 56

SQL

CUST 2101 2102 2103

REP 106 101 105

SUM(AMOUNT) 3,00,000.00 3,50,000.00 4,00,000.00

To get the sub totals, COMPUTE BY clause can be used as given below. SELECT REP, CUST, AMOUNT FROM ORDERS ORDER BY REP, CUST CUMPUTE SUM(AMOUNT) BY REP,CUST COUPUTE SUM(AMOUNT), AVG(AMOUNT) BY REP REP 102 102 REP 103 CUST 2106 2107 SUM CUST 2114 SUM SUM AVG AMOUNT 2,00,000.00 3,00,000.00 5,00,000.00 AMOUNT 52,000.00 52,000.00 5,52,000.00 1,84,000.00

Grouped queries are subjected to some limitations. The grouping columns must be actual columns of tables named in the FROM clause. Grouping cannot be done based on the value of calculated expression. There are also restrictions on the items that can appear in the select list of a grouped query. All the items in select list must have a single value for each group of rows. In general, a select list in a grouped query can be a constant a column function, which produces a single value summarizing the rows in a group a grouping column, which by definition has the same value in every row of the group or an expression involving the combination of above For the purpose of grouping, SQL considers two NULL values to be equal. PEOPLE NAME Kiran Ravi Jones George Prakash Clerk Marie Kevin Harry Paul Sam Robert HAIR Brown NULL NULL NULL NULL Brown Brown Brown Brown Brown Blonde Blonde EYES Blue Blue Blue NULL NULL NULL NULL NULL NULL Brown Blue Blue

Page No : 26 of 56

SQL

SELECT HAIR, EYES, COUNT(*) FROM PEOPLE GROUP BY HAIR, EYES HAIR Brown NULL NULL Brown Brown Blonde EYES COUNT(*) Blue 1 Blue 2 NULL 2 NULL 4 Brown 1 Blue 2

As the search condition can be used with WHERE clause, to select and reject individual rows that participate in query, HAVING clause can be used to select and reject row groups. The format of HAVNG clause is similar to that of the WHERE clause, consisting of HAVING key word followed by search condition. SELECT REP, AVG(AMOUNT) GROUP BY REP HAVING SUM(AMOUNT) > 30,000.00 REP 105 106 107 108 AVG(AMOUNT) 1,00,000.00 2,00,000.00 3,00,000.00 1,50,000.00

The HAVING clause is used to include or exclude row groups from query results, so the search condition specifies, must be one that applies to the group as a whole rather than to individual rows. In general an item appearing within the search condition in a HAVING clause can be a constant a column function, which produces a single value summarizing the rows in a group a grouping column, which by definition has the same value in every row of the group or an expression involving combinations of above. Sub Queries A sub query is a query within a query. The results of the sub query are used by the DBMS to determine the results of higher level query that contains the sub query. Mostly sub query appears within the WHERE or HAVING clause of another SQL statement. Sub queries provide an efficient, natural way to handle query requests, that are themselves expressed in terms of of the results of other queries. SELECT CITY FROM OFFICES WHERE TARGET > ( SELECT SUM(QUOTA) FROM SALESREPS WHERE REPOFF = OFFICE )

Page No : 27 of 56

SQL

For each office, the inner query (sub query) calculates the sum of quotas for the sales people working in that office. The outer query (main query) compares the offices target to the calculated total and decides whether to add the office to the main query results or not. The sub query is enclosed in parenthesis, otherwise it is similar to SELECT statement with a FROM clause and optional WHERE, GROUP BY and HAVING clauses. The form of these clauses in a sub query is identical to that in a SELECT statement and they perform their normal functions when used within a sub query. The differences between a sub query and an actual SELECT statement are, In most common uses, a sub query must produce a single column of data as its query results. This means that a sub query almost always has a single select item in its SELECT statement. The ORDER BY clause cannot be used in a sub query. The sub query results are used internally by the main query and are never visible to the user, so it makes little sense to sort them Column names appearing in sub query may refer to the columns of tables in the main query. The reference is called outer reference. In most of the implementations, a sub query cannot be a UNION of several different SELECT statements. SQL2 relaxes this condition Sub Query Search Conditions A sub query usually appears as a part of search condition in WHERE or HAVING clause. SQL offers the following search condition. Sub query comparison test Compares the value of an expression to a single value produced by a sub query. This test resembles the simple comparison test. The operators used for this test are =, <>, <, <=, >, >= SELECT NAME FROM SALESREPS WHERE QUOTA >= ( SELECT TARGET FROM OFFICES WHERE CITY = Mumbai) NAME Ravi Kiran Sub query set membership test ( IN ) Checks whether the value of an expression matches one of the set of values produced by a sub query. This test also resembles simple set membership test. SELECT NAME FROM SALESREPS WHERE REPOFF IN (SELECT OFFICES FROM OFFICES WHERE SALES > TARGET)

Page No : 28 of 56

SQL

NAME Kiran Ravi Prakash Existence test Tests whether a sub query produces any rows of query results. This test is used only with sub queries. Consider the following example. SELECT DISTINCT DESCRIPTION FROM PRODUCTS WHERE EXISTS (SELECT ORDER_NO FROM ORDERS WHERE PRODUCT = PRODUCT_ID AND MFR = MFR_ID AND AMOUNT >= 25,000.00) DESCRIPTION Left hinge Right hinge SQL processes this query by going through the PRODUCTS table and performing the sub query for each product. The sub query produces a column containing the order numbers of any orders for the current product that are over 25,000.00. If there are any such order, the EXISTS test is TRUE. If the sub query produces no rows, the EXISTS test is FALSE. This test cannot produce NULL results. Quantified Comparison test (ANY or ALL) Compares the value of an expression to each of the set of values produced by a sub query ( modified version of IN ). SQL provides two quantified tests any and all that extend the set membership test to other comparison operators. SELECT NAME FROM SALESREPS WHERE (0.1 * QUOTA) < ANY (SELECT AMOUNT FROM OREDERS WHERE REP = EMPNO) NAME Ravi Kiran Prakash If the sub query produces an empty column of query results, ANY test returns FALSE. Like ANY test, the ALL test is used in conjunction with one of the sis SQL comparison operators to compare a single test value to a column of data values produced by the sub query. To perform the test, SQL uses the specified comparison operator to compare the test value to each data value in the column one at a time. If all of individual comparisons yield a TRUE result, the ALL test returns a TRUE result. SELECT CITY, TARGET FROM OFFICES WHERE ( 0.5 8 TARGET ) < ALL( SELECT SALES FROM SALESREPS WHERE REP_OFF = OFFICE )

Page No : 29 of 56

SQL

CITY Mumbai Delhi Chennai

TARGET 3,00,000.00 5,00,000.00 1,00,000.00

Many sub queries work as joins. Consider the following example. SELECT NAME, AGE FROM SALESREPS WHERE REP_OFF IN (SELECT OFFICE FROM OFFICES WHERE REGION = Western) NAME Kiran Ravi AGE 30 40

Above result can also be obtained from the following statement. SELECT NAME, AGE FROM SALESREPS, OFFICES WHERE REP_OFF = OFFICE AND REGION = Western Sub queries can also be nested as shown in the following example. SELECT COMPANY FROM CUSTOMERS WHERE CUST_REP IN (SELECT EMPNO FROM SALESREPS WHERE REP_OFF IN ( SELECT OFFICE FROM OFFICES WHERE REGION = Western)) Correlated Sub Queries SQL performs a sub query over and over again once for each row of main query. In many sub queries, sub query produces the same results for every row or row groups of main query. SELECT CITY FROM OFFICES WHERE SALES < (SELECT AVG(TARGET) FROM OFFICES ) In the above example it is not necessary to perform the sub query for every row of main query. In fact sub query can be evaluated once and the results can be used in main query. Then the query can be reduced as SELECT CITY FROM OFFICES WHERE SALES < 55,000.00 (55,000.00 is the result of sub query) SQL detects such situation automatically and takes care to avoid the repeated evaluation of sub query. SELECT CITY FROM OFFICES WHERE TARGET > (SELECT SUM(QUOTA) FROM SALESREPS WHERE REP_OFF = OFFICE )

Page No : 30 of 56

SQL

In the above example for each row of the OFFICES table to be tested by WHERE clause of main query, the OFFEICE column has different value ( OFFICE column appears as outer reference in sub query ). Thus, SQL has no choice but to carry out this sub query for each row in OFFICES table. A sub query containing an outer reference is called correlated sub query because its results are correlated with each individual rows of main query. Sub queries can also be written with HAVING clause. SELECT NAME, AVG(AMOUNT) FROM SALESREPS, ORDERS WHERE EMPNO = REP AND MFR = BPL GROUP BY NAME HAVING AVG(AMOUNT) > SELECT AVG(AMOUNT) FROM ORDERS) Data Base Updating INSERT statement is used to add row/rows into a table. The INTO clause specifies the table that receives the new row and VALUES clause specifies the data values that the new row will contain. The column list indicates which data values go into which column of new row. INSERT INTO OFFICES ( OFFICE, CITY, REGION, TARGET, SALES ) VALUES (22, Mumbai, Western, 30,000.00, 50,000.00) 1 row inserted. When SQL inserts a new row of data into a table, it automatically assigns a NULL value to any column whose name is missing from the column list in the insert statement. INSERT INTO OFFICES ( OFFICE, CITY, REGION, SALES ) VALUES (33, Chennai, Southern, 4,00,000.00) Above statement inserts NULL into TARGET column of OFFICES table. When column list is omitted, SQL automatically selects a column list consisting of all columns of the table, in left to right sequence. INSERT INTO OFFICES VALUES ( 11, Delhi, Northern, 3,50,000.00, 2,50,000.00 ) Multiple rows from a table can be substituted into another table as shown in the following example. INSERT INTO OFFICES1 ( NAME,CITY, REGION, TARGET, SALES ) SELECT NAME,CITY, REGION, TARGET, SALES FROM OFFICES

Page No : 31 of 56

SQL

WHERE clause can also be used if only selected rows are to be inserted into target table. Multi row insert statement has following restrictions. The query cannot have an ORDER BY clause. It is useless to sort the query results as they are being inserted into a table in which they will be unordered. The query results must contain the same number of columns as the column list in the INSERT statements, and the data types must be compatible column by column The query cannot be the UNION of several different SELECT statements. Only a single SELECT statement may be specified. The target table of INSERT statement cannot appear in the FROM clause of query or any sub queries that it contains. This prohibits inserting part of a table into itself. To insert the bulk data down loaded from another computer system or collected from other sites and stored in a sequential file, a program can be written, to read a single record from file, and uses a single row INSERT statement to add arrow into table. Looping statements can be used to perform this task till the entire data is written into table. The DELETE statement removes selected rows of data from a single table. The FROM clause specifies a target table containing rows. The WHERE clause specifies which rows of the table are to be deleted. DELETE FROM SALESREPS WHERE NAME = Kiran All rows satisfying the search condition in WHERE clause are deleted. All rows of a table can be deleted if WHERE clause is not included in the DELETE statement. DELETE FROM ORDERS Two or more tables can not be mentioned in the DELETE statement. DELETE statement can also include a sub query. DELETE FROM CUSTOMERS WHERE CUST_REP IN ( SELECT EMPNO FROM SALESREPS WHERE SALES < (0.8 * QUOTA)) DELETE FROM SALESREPS WHERE ( 0.02 * QUOTA ) > (SELECT SUM(AMOUNT) FROM ORDERS WHERE REP = EMPNO) The UPDATE statement modifies the values of one or more columns in selected rows of a single table. UPDATE CUSTOMERS SET CREDIT_LIMIT = 60,000.00, CUST_REP = 109 WHERE COMPANY = BPL

Page No : 32 of 56

SQL

UPDATE SALESREPS SET QUOTA = 10,000.00 WHERE QUOTA IS NULL All rows of a column of a table can be updated as given below. UPDATE SALESREPS SET QUOTA = 1.5 * QUOTA Sub queries can also be used in UPDATE statement UPDATE CUSTOMERS SET CREDIT_LIMIT = CREDIT_LIMIT + 5,000.00 WHERE CUST_N0 IN (SELECT DISTINCT CUST FROM ORDERS WHERE AMOUNT > 2,500.00) Data Integrity The term data integrity refers to the correctness and completeness of data in a database. To preserve the consistency and correctness of its stored data, RDBMS typically imposes one or more data integrity constraints. Following are typical RDBMS constraints. Required Data : Some columns in a database must contain a valid data value in every row, they are not allowed to contain missing or NULL values. Validity Checking : Every column in a database has domain, a set of data values that are legal for that column. A rule can be constructed and used as a constraint at the time of creating the table. CREATE RULE QUOTQ_LIMIT (rule name) AS @VALUE BETWEEN 0.00 AND 5,000.00 Column check constraint can be introduced as given below. CREARE TABLE SALESREPS (EMP_NO INTEGER NOT NULL CHECK (EMP_N0 BETWEEN 101 AND 199), AGE INTEGER CHECK (AGE >20), QUOTA MONEY CHECK QUOTA > 0.00) ) Domain check can also be introduced as. CREATE DOMAIN VALID_ID INTEGER CHECK (VALUE BETWEEN 101 AND 199) The domain name VALID_ID is used at the time of table creation. CREATE TABLE SALESREPS ( EMP_NO VALID_ID)

Page No : 33 of 56

SQL

In case of a primary key, each row of the table should have a unique value; otherwise they will loose their meaning. For this reason, the requirement is that primary keys have unique values is called entity integrity. In case of a foreign key, the reference to which table also needs to be mentioned at the time of table creation. This type reference is called referential integrity. Assertions CREATE ASSERTION QUOTA_VALID CHECK ((OFFICES.QUOTA <= SUM(SALESREPS.QUOTA)) AND SALESREPS.REP_OFF = OFFICES.OFFICE)) Every time an attempt is made to modify the contents of a database through INSERT< UPDATE or DELETE statement, the search condition is checked against the (proposed) modified data base contents. If the search condition is TRUE, the modification is allowed. If the search condition is NOT TRUE, DBMS does not carry out the proposed modification. Trigger For any event that causes a change in the contents of a table, a user can specify an associated action that DBMS should carryout. There are three events that can trigger an action are attempts to INSERT, DELETE, and UPDATE. CREATE TRIGGER NEWORDER ON ORDERS FOR INSERT AS UPDATE SALESREPS SET SALES = SALES + INSERTED.AMOUNT FROM SALESREPS, INSERTED WHERE SALESREPS.EMPNO = INSERTED.REP UPDATE PRODUCTS SET QTY = QTY INSTERTED.QTY FROM PRODUCTS, INSERTED WHERE PRODUCTS.MFR = INSERTED.MFR Creating Database The CREATE TABLE statement , defines a new table in the database and prepares it to accept data. The various clauses of the statement specify the elements of the table definition. When a user executes a CREATE TABLE statement, he becomes the owner of the table. The table name should not conflict with the name of existing table. Following are column definitions of CREATE TABLE statement. A column name : Every column in the table must have a unique name, but the names may duplicate those of columns of other tables The data type of column : This identifies the type of data that the column stores Whether the column contains required data : The NOT NULL clause prevents NULL values from appearing in the column. An optional default value for the column : The DBMS uses this value when an INSERT statement for table does not specify a value for column.

Page No : 34 of 56

SQL

CREATE TABLE OFFICES (OFFICE INTEGER NOT NULL, CITY VARCHAR(15), REGION VARCHAR(10) NOT NULL MGR INTEGER, TARGET MONEY, SALES MONEY NOT NUL) Domain constraint for a column can be introduced as shown in the following example. CREATE DOMAIN VALID_ID INTEGER CHECK (VALUE BETWEEN 11 AND 99) CREATE TABLE OFFICES ( OFFICE VALID_ID NOT NULL, CITY VARCHAR(15), REGION VARCHAR(10) NOT NULL MGR INTEGER, TARGET MONEY, SALES MONEY NOT NUL) In addition to column definition of a table, CREATE TABLE statement identifies the tables primary key and foreign keys and the tables relationships to other tables in database. The PRIMARY KEY clause specifies the column or columns that form primary key for the table. DBMS automatically requires that the primary key value to be unique in every row of the table. In addition the column definition for every column in primary key must be specify the column as NOT NULL. FOREIGN KEY clause specifies a foreign key in the table and relationship that it creates with another table in the database. CREATE TABLE ORDERS ( ORDER_NO INTEGER NOT NULL, ORDER_DATE DATE NOT NULL, CUST INTEGER NOT NULL, REP INTEGER, MFR CHAR(3) NOT NULL, PRODUCT CHAR(5) NOT NULL, QTY INTEGER NOT NULL, AMOUNT MONEY NOT NULL, PRIMSRY KEY ( ORDER_NO), CONSTRAINT PLACEDBY, FOREIGN KEY ( CUST ), REFERENCES CUSTOMERS, ON DELETE CASCADE, CONSTRAINT TAKENBY )

Page No : 35 of 56

SQL

ON DELETE CASCADE ON DELETE SET NULL ON DELETE SET DEFAULT ON DELETE NO ACTION ON DELETE RESTRICT ON DELETE may have the following constraints? The RESTRICT delete rule prevents the deleting a row from the parent table if the row has any children. A DELETE statement that attempts to delete such a row is rejected with error message. The CASCADE delete rule tells the DBMS that when a parent row is deleted, all of its child rows should also automatically deleted from the child table. The SET NULL tells the DBMS that when a parent row is deleted, the foreign key values in all of its child rows should be automatically set to null. The SET DEFAULT delete rule tells the DBMS that when a parent row is deleted, the foreign key values in all of its child rows should automatically be set to default for that particular column. The above four rules are also applicable to UPDATE statement. UNIQUE ( CITY ) Unique clause defines a non primary key column to contain distinct values. DROP TABLE statement is used to remove a table from database. DROP TABLE OFFICES The table definition can be changed for the following operations. Add or drop a column Change the default value for a column Add or drop a primary key Add or drop a foreign key Add or drop uniqueness constraint for a table Add or drop a check constraint

A column is added to a table by ALTER TABLE statement. ALTER TABLE PRODUCTS ASS CONTACT_NAME VARCHAR(20) ALTER TABLE PRODUCTS ADD MIN_QTY INTEGER NOT NULL WITH DEFAULT 0 A column may be dropped from a table as given below. ALTER TABLE SALESREPS DROP AGE

Page No : 36 of 56

SQL

A foreign key can be added as shown below. ALTER TABLE OFFICES ADD CONSTRAINT INREGION FOREIGN KEY (REGION) REFERENCES REGION Before dropping a primary key foreign keys related to that primary key should be dropped. ALTER TABLE SALES REPS DROP CONSTRAINT WORKSIN FOREIGN KEY (REP_OFF ) REFERENCES OFFICES ALTER TABLE OFFICES DROP PRIMARY KEY (CITY ) An assertion can be dropped as given below. DROP ASSERTION CREDIT_LIMIT Similarly domain can also be dropped as given below. DROP DOMAIN QUOTA_LIMIT CASCADE DROP DOMAIN QUOTA_LIMIT RESTRICT Table aliases can be created and dropped as indicated below. CREATE ALIS REPS FOR SALESREPS Alias of tables owned by other users can also be created as shown in the following example. CREATE ALIAS OFFICES FOR OP_ADMIN.OFFICES Aliases can be dropped as shown below. DROP ALIAS REPS DROP ALIAS OFFICES Indexes are created for rapid access to the rows of a table based on values of on or more columns. The index stores data values and pointers to the rows where those data values occur. In the index data values are arranged in ascending or descending order, so that DBMS can quickly search the index to find a particular value. CREATE UNIQUE INDEX ORDER_LIMIT ON ORDERS (MFR, PRODUCT) DROP INDEX ORDER_LIMIT

Page No : 37 of 56

SQL

A view is a virtual table in the database whose contents are defined by a query. To the database user, the view appears just like a real table, with a set of named columns and rows of data. But, unlike a real table a view does not exist in the database as stored set of values. In fact the rows and columns of data visible through the view are query results produced by the query that defines the view. CREATE VIEW EASTREPS AS SELECT * FROM SALESREPS WHERE REP_OFF IN (11, 12, 13 ) CREATE VIEW EASTOFFICES AS SELECT * FROM OFFICES WHERE REGION = Eastern Views provide different advantages as discussed below. Security : Each user can be given permission to access to the database only through a small set of views that contain the specific data the user is authorized. Query Simplicity : A view can draw the data from several different tables and present it as a single table turning multi table queries into a single table queries against view. Structural Simplicity : Views can give a user a personalized view of the database structure, presenting the data base as a set of virtual tables that make sense for that user Insulation from Change : A view can present a consistent, unchanged image of structure of the database, even if the source tables are split, restructured or renamed. Data Integrity : It database is accessed and entered through a view, the DBMS can automatically check the data to ensure that it meets specified integrity constraints. Disadvantages If a view is defined by complicated queries using multi tables, a simple query on view takes longer time. When a user tries to update a view, the DBMS must translate the request into a update on rows of the source tables. This is possible for simple view, but more complex views cannot be updated, they are read only. A view defined by the following example in which all columns of table and only specific rows included is called horizontal view. CREATE VIEW WESTREPS AS SELECT * FROM SALESREPS WHERE REP_OFF IN ( 21, 22)

Page No : 38 of 56

SQL

A view defined by the following example, in which an entire column/columns are included is called vertical view. CREATE VIEW OFFICESIN AS SELECT OFFICE, CITY, REGION FROM OFFICES Views can also be combined to be horizontal and vertical. CREATE VIEW CUST AS SELECT CUSTNO, COMPANY FROM CUSTOMERS WHERE CUST_REP = 105 The query making a view can also contain GROUP BY clause. The query specified in a view definition may include GROUP BY clause. This type of view is called grouped view, because the data visible through the view is the result of a grouped query. Grouped views perform the same function as grouped queries, they group related rows of data and produce one row of query result for each group, summarizing the data in that group. A grouped vies makes these grouped query results into a vertical table and allow further queries on them. CREATE VIEW ORD_BY_REP ( WHO, HOW_MANY, TOATAL, LOW, HIGH, AVERAGES ) AS SELECT REP, COUNT(*), SUM(AMOUNT), MIN(AMOUNT), MAX(AMOUNT), AVG(AMOUNT) FROM ORDERS GROUP BY REP Once this view is formed, it can be used to simplify the queries. SELECT NAME, HOW_MANY, TOTAL, AVERAGE FROM SALESREPS, ORDER_BY_REP WHERE WHO = EMPNO ORDER BY TOTAL The following query produces error because of nested column functions. SELECT REP_OFF, MIN(AVERAGE), MAX(AVERAGE) FROM SALESREPS, ORD_BY_REP WHERE EMPNO = WHO GROUP BY REP_OFF Joined Views One of the most frequent reasons for having views is to simplify multi table queries. By specifying a two table or three table query in view definition, a joined view can be created that draws its data from two or three tables and presents the result as a single virtual table.

Page No : 39 of 56

SQL

CREATE VIEW ORDER_INFO ( ORDER_NO, COMPANY,REP_NAME, AMOUNT ) AS SELECT ORDER_NO,COMPANY, NAME, AMOUNT FROM ORDERS, CUSTOMERS, SALESREPS WHERE CUST = CUST_NO AND REP = EMPNO SELECT REP_NAME, COMPANY, SUM(AMOUNT) FROM ORDER_INFO GROUP BY REP_NAME, COMPANY Query result REP_NAME COMPANY Kiran BPL Kiran HMT Ravi BHEL Ravi BPL Prakash BEL SUM(AMOUNT) 3,00,000.00 2,00,000.00 1,00,000.00 4,00,000.00 2,00,000.00

The updating statements for view can be translated into equivalent operations on source tables. However, it is possible only for horizontal views derived from a single table. If a view is derived from more tables, and one to one correspondence is not existing update statements cannot be executed on view. Consider the view defined by the following example. CREATE VIEW EASTREPS AS SELECT * FROM SALESREPS WHERE REP_OFF IN (11, 12, 13) An INSERT statement may be executed on this view as shown in the following example. INSERT INTO EASTREPS (EMPNO, NAME, REP_OFF, AGE, SALES ) VALUES (113, Sham, 11, 43, 00.00 ) Above statement inserts a row into SALESREPS ( source ) table. Now consider the following example. INSER INTO EASTREPS ( EMPNO, NAME, REP_OFF, AGE, SALES ) VALUES 114, Gopal, 21,40, 0.00) This statement will also get executed and a new row is inserted into SALESREPS table. But, this newly inserted row is not visible through view, as it does not meet the search condition specified in view definition ( WHERE REP_OFF IN (11, 12, 13 ) ). Similarly, an update that does not satisfy the search condition of definition, row disappears from view, example. UPDATE EASTREPS SET REP_OFF = 21 WHERE EMPNO = 104

Page No : 40 of 56

SQL

This kind of updates which are not visible through view can be avoided by CHECK OPTION as given in the following example. CREATE VIEW EASTREPS AS SELECT 8 FROM SALESREPS WHERE REP_OFF IN ( 11, 12, 13 ) WITH CHECK OPTION A view can be dropped as given below. DROP VIEW EASTREPS DROP VIEW EASTREPS CASCADE The CASCADE option tells the DBMS to delete not only named view, but also view that depend on its definition DROP VIEW EASTREPS RESTRICT The RESTRICT option tells the DBMS to remove the view only if no other views depend on it. Many commercial versions of SQL need either of these options. GRANT SELECT, INSERT, DELETE, UPDATE ON ORDERS TO SHAM GRANT SELECT, INSERT ON CUSTOMERS TO PRAKASH GRANT ALL PRIVILEGES ON SALESREPS TO SHAM GRANT SELECT ON OFFICES TO PUBLIC ( all users ) GRANT UPDATE (COMPANY, CUST)REP ) ON CUSTOMERS TO SHAM GRANT SELECT ( EMP_NO, NAME, REP_OFF ) ON SALESREPS TO PRAKASH GRANT SELECT ON WESTREPS TO LARRY WITH GRANT OPTION

Page No : 41 of 56

SQL

REVOKE INSERT, UPDATE ON SALESREPS FROM PRAKASH REVOKE ALL PREVILEGES ON OFFICES FROM PUBLIC System Catalog The system catalog contains the following information. Tables : the catalog describes each table in the database, identifying its table name, its owner, the number of columns it contains, its size and so on. Columns : The catalog describes each column in the database, giving columns name, the table to which it belongs to, its data type, its size, NULLs are allowed and so on. Users : The catalog describes each authorized data base user, including users name an encrypted form of pass word etc. Views : The catalog describes each view defined in the database, including its name, name of its owner, the query that defines the view and so on. Privileges : The system catalog describes each set of privileges granted in the database, including the name of grantor and grantee, the privileges granted, the object on which the privileges have been granted etc. Embedded SQL SQL is a dual mode language which can be used for adhoc queries and data updates, and as a programmatic database language used by application programs for database access. The dual mode nature of SQL has following advantages. It is relatively easy for programmers to learn how to write programs that access the database Capabilities available through interactive query language are also automatically available to application programs The SQL statements to be used in a program can be tried out first using interactive SQL and then coded into program Programs can work with tables of data and query results instead of navigating their way through the database. SQL has no provision for declaring variables, no goto statement, no if statement for testing conditions, no for, do or while statements to construct loops, no block structure and so on. SQL is a database sub language that handles special purpose database management tasks. Commercial SQL offer two basic techniques for using SQL within application program.

Page No : 42 of 56

SQL

Embedded SQL In this approach, SQL statements are embedded directly into the programs source code, inter mixed with other programming language statements. Special embedded SQL statements are used to retrieve data into program. A special SQL precompiler accepts the combined source code and along with other programming tools, converts it into an executable program. Application program Interface. In this approach, the program communicates with the DBMS through a set of function calls, called an application program interface or API. The program passes SQL statements to the DBMS through API calls and uses API calls to retrieve results. This approach does not require special compiler. DBMS statement processing To process SQL statements, the DBMS goes through following steps. The DBMS begins by parsing the SQL statement. It breaks the statement up into individual words, makes sure that the statement has a valid verb, legal clauses, and so on. Syntax errors and misspellings can be detected in this step. The DBMS validates the statement. It checks the statement against system catalog. Do all the tables named in the statement exist in the database or not, do all the columns exist or not, and are the column names unambiguous or not. Does the user has required privileges to execute the statement or not. Sematic errors are detected. DBMS optimizes the statement. It explores various ways to carry out the statement. After exploring alternatives, the DBMS chooses the optimum alternative. The DBMS then generates an application plan for the statement. The application plan is a binary representation of steps that are required to carry out the statement, it is the DBMS equivalent of executable code. Finally, the DBMS carries out the statement by executing the application plan. Embedded SQL concepts The central idea of embedded SQL id to blend the SQL language statements directly into a program written in a host programming language such as C, Pascal, COBOL, FORTRON, PL/I or assembler. Embedded SQL uses the following techniques to embed the SQL statements. SQL statements are intermixed with the statements of host language in the source program. This embedded SQL program is submitted to a SQL pre compiler which processes the SQL statements. Variables of host programming language can be referenced in the embedded SQL statements, allowing values calculated by program to be used by SQL statements. Program language variables also are used by embedded SQL statements to receive the result of SQL queries, allowing the program to use and process the retrieved values.

Page No : 43 of 56

SQL

Special program variables are used to assign NULL values to database columns and to support the retrieval of NULL values from database. Several new SQL statements that are unique to embedded SQL are added to the interactive SQL language, to provide row by row processing of queries. Embedded SQL Program The embedded SQL program is submitted to the SQL pre compiler, a programming tool. The pre compiler scans the program and finds the embedded SQL statements and processes them. A different pre compiler is required for each programming language supported by DBMS. Commercial SQL typically offer pre compiler for C, Pascal, COBOL, FORTRON, Ada, PL/I, RPG and various assembly languages. The pre compiler produces two files as its output. The first file is the source program, stripped of its embedded SQL statements. In their place, the pre compiler substitutes calls to private DBMS routines that provide the run time link between program and DBMS. Typically only the pre compiler and the DBMS know the names and calling sequence of these routines, they are not public interface to the DBMS. The second file is a copy of all the embedded SQL statements used in the program. This file is some times called database request module DBRM. The source file output from pre compiler is submitted to the standard compiler for host programming language. The compiler processes the source code and produces object code as its output. The linker accepts the object modules generated by the compiler, links them with various library routines and produces an executable program. The library routines linked to executable program include private DBMS routines described in step 2. The database request module generated by the pre compiler is submitted to a special BIND program. This program examines the SQL statements, parses, validates and optimizes them and produces an application plan for each statement. The result is a combined application plan for the entire program, representing a DBMS executable version of its embedded SQL statements. The BIND program stores the plan in the database, usually assigning it the name of application program that created it. When a program is sun SQL verifies two user_ids. The user_id of person who developed the program or more specifically, the person who ran the BIND program to create the application plan. The user_id of the person who is now executing the program and the corresponding application plan.

Page No : 44 of 56

SQL

main() { exec sql include sqlca; exec sql begin declaration section; int officenum; char cityname(1); char regionname(10); float targrt_val; float sales_val; exec sql end declaration section; exec sql whenever sqlerror goto querry_error; exec sql whenever not found goto bad_number; printf(\Enter office number : ); scanf( %d, & officenum); exec sql select city, region, target, sales from offices where office = officenum into : cityname, : regionname, : target_val, : sales_val; printf(\n city : %s, cityname); printf(\n Region : %s, regionname); printf(\n Target : %f, target_val); printf(\n Sales : %f, sales_val); exit(); query_error : printf(\n SQL Error : %d , sqlca.sqlcode); exit(); bad_number : printf(\n Invalid office number ); exit(); } The embedded SQL statements appear in the midst of host programming language statements. It usually does not matter whether the SQL statement is written in upper case or lower case; the most common practice is to follow the style of host language. Every embedded SQL statement begins with an introducer that flags it as SQL statement. The most commonly used introducer is EXEC SQL. If an embedded SQL statement extends over multiple lines, the host language strategy for multiple lines is used. For FORTRON programs the second and subsequent lines of statement must have a continuation character in column 6 . Every embedded SQL statement ends with a terminator that signal the end of SQL statement. The terminator varies with the style of the host language. In COBOL, the terminator is string END-exec, in C ; is the terminator. main() { exec sql include sqlca; exec sql declare salesreos table; empl_no integer not null, name varchar(15) not null, age integer,

Page No : 45 of 56

SQL

rep_ff integer, title varchar(10), hire_date date not null, manager integer, quota money, sales money not null ); printf( Deleting salesreps with low quota. \n); exec sql delete from salesreps where sales < 15,000.00; printf(Finished deleting.); exit(); }

Declaring tables The embedded DECLARE TABLE statement declares a table that will be referenced by on or mor SQL statements in the program. This is an optional statements that aids the pre compiler in its task of parsing and validating the embedded SQL statement. By using the DECLARE TABLE statement, the program explicitly specifies its assumptions about the columns in the table and their data type and sizes. The pre compiler checks the table and column references in the program to make sure they confirm to the table declaration. This statement is not executable statement. It is used only for the purpose of documentation and for the use of pre compiler. Error Handling Embedded SQL statements can produce two types of errors. Compile time errors Misplaces commas, misspelled SQL keywords, and similar errors in embedded SQL statements are detected by the SQL precompiler and reported to the programmer. The programmer can correct the errors and recompile the application program. Run time errors An attempt to insert an invalid data or lack of permission to update a table can be detected only at run time. Errors such as these must be detected and handled by the application program. Error handling with SQLCODE Under this scheme, the DBMS communicates status information to the embedded SQL program through an are of program storage called the SQL Communication Area or SQLCA. The SQLCA is a data structure that contains error variables and status indicators. By examining the SQLCA, the application program can determine the success or failure of its embedded SQL statements and act accordingly. SQLCA structure is given below.

Page No : 46 of 56

SQL

struct sqlca { unsigned char sqlcaid[8]; id long sqlcac; length long sqlcode status code short sqlerrml; error message length unsigned char sqlerrmc[70]; name of object unsigned char sqlerrp[8]; diagnostic integer long sqlerrd[6]; counts error codes unsigned char sqlwarn[8]; warning flag unsigned char sqlext[8] extn to sql } #define SQLCODE sqlca.sqlcode / a W in any of the SQLWARN fields signals warning condition otherwise these fields are blank #define SQLWARN0 sqlca.sqlwarn[0] #define SQLWARN1 sqlca.sqlwarn[1] #define SQLWARN2 sqlca.sqlwarn[2] #define SQLWARN7 sqlca.sqlwarn[7] A SQLCODE of zero indicates successful completion of statement A negative SQLCODE value indicates a serious error that prevented the statement from executing correctly. A positive SQLCODE value indicated a warning condition, sqlcode can be used as given below. exec sql delete from salesreps where quota < 15,000.00; if (sqlca.sqlcode < 0 ) goto error_routine; Error handling with SQLSTAE In order to bring the standardization, a new variable called SQLSTATE is introduced to handle errors. A SQLSTATE variable consists of two parts. A two character error class that identifies the general classification of error. (such as connection error, invalid data or a warning ). A three character error subclass that identifies a specific type of error within a general class. Subclass might identify a divide by zero error etc. to support the use of SQLSTATE , GET DIAGNOSTICS statement is used, as given below. exec sql delete from salesreps where quota < 15,000.00; if (strcmp(sqlca.sqlstate, 00000)) goto error_routine;

Page No : 47 of 56

SQL

exec sql get diagnostics :num = ROW_COUNT; printf(\n%ld row deleted , num); error_routine : exeec sql get diagnostics : count = NUMBER; for(i=1; i< count; i++) { exec sql get diagnostics EXCEPTION : I : err = returned_SQLSTATE; : msg = MESSAGE_TEXT; printf (\nSQL Error #%d : code : %s message : %s ,i,err,msg); } The whenever statement The whenever statement is directive to precompiler not a executable statement. It tells the precompiler to automatically generate error handling code following every executable embedded SQL statement. There are three different conditions along with which whenever statement can be used. whenever sqlerror whenever sqlwarning whenever not found whenever statement can take any of the following actions whenever/goto whenever/continue the whenever statement is directive to the precompiler and its effect can be superceded by another whenever statement appearing late in the program. Host Variables and NULL values Most of the host programming languages do not support the NULL values. To support the NULL values host indicator variables are used. An indicator value of zero means that the host variable contains a valid value A negative indicator value means that host variable should be assumed to have NULL value A positive indicator value means that host variable contains a valid value, which may have been rounded off or truncated. exec sql update sales reps set quots= : amount : amount_id, sales = : amt; Data retrieval in embedded SQL Single row queries Using where clause Not found condition

Page No : 48 of 56

SQL

If a single row of query results is successfully retrieved SQLCODE is set to zero and SQLSTATE is set to 00000, the host variables values in the into clause contain retrieved values If the query produces error, SQLCODE is set to negative value, SQLSTATE is set to non zero error class, host variables do not contain retrieved values. It the query produced no rows of query results, a special NOT FOUNT warning value is returned in SQLCODE and SQLSTATE return NO DATA error class If the query produces more than one row of query results, it is treated as error and a negative SQLCODE is returned. Multi Row Queries SQL provides a method to process one row of query at a time. For this purpose a cursor and some statements are provided. The DECLARE CURSOR statement specifies the query to be performed and associated a cursor name with query. The OPEN statement asks the DBMS to start executing the query and generating query results. It positions the cursor before the first row of query results. The FETCH statements advances the cursor to the first row of query results and retrieves its data into host variables for use by the program. Subsequent FETCH statements move the cursor through the query results row by row advancing the cursor to the next row of query results and retrieving its data into the host variables. The CLOSE statement ends access to query results and breaks the association between the cursor and the query results main() { exec sql include sqlca; exec sql begin declare section; char rename[10]; float repquota; float repsales; short repquota_ind /* NULL indicator exec sql end declare section; exec sql declare repur cursor for select name, quota, sales from salesreps where sales > quota order by name; whenever sqlerror goto error; whenever not found goto done; exec sql open repurs; for( ; ; ) {

Page No : 49 of 56

SQL

exec sql fetch repurs into repname, :repquota, repquota_ind, : repsales ; printf(\n Name : %s repname); if (repquota_ind < 0 ) printf(\n Quota is NULL): else printf(\n Quota : %f , repquota); printf(\n Sales : %f, resales: } error : printf(SQL error : %ld, sqlca.sqlcode) exit() done : exec sql close repurs; exit(); } SCROLL cursor FETCH FIRST FETCH LAST FETCH PROR FETCH NEST FETCH ABSOLUTE FETCH RELATIVE For using the scroll cursor the cursor should be declared as given below. exec sql declare repur scroll cursor for select name, quota, sales from salesreps where sales > quota order by name ; cursor can also be used for delete and update purposes. declare cursor cursorname for select statement for update of columnname; update table tablename set columnname = expression where current of cursorname;

Page No : 50 of 56

SQL

DYNAMIC SQL main() { exec sql include sqlca; exec sql begin declare section; char stmtbuf[301]; exec sql end declare section; char tblname[101]; char searchcondition[101]; strcpy(stmtbuf, delete from); printf(|N Enter table name : ); gets(tblname); strcat(stmtbuf, tblname); printf(\n Enter search condition : ); sets searchcondition); if ( strlen(searchcondition) > 0 ) { strcat(stmtbuf, where); strcat(stmtbuf , searchcondition ); } exec sql execute immediate : stmtbuf; if (sqlca.sqlcode < 0 ) printf(\n SQL error : %ld , sqlca.sqlcode); else printf(\n Dele from %s successful , tblname ); exit(); } The program constructs a SQL statement as a string of text in one of its data areas. The statement can be any SQL statement that does not retrieve data. The program passes the SQL statement to the DBMS with EXECUT IMMEDIATE statement. The DBMS executes the statement and sets the SQLCODE/SQLSTATE values to indicate the completion status, exactly as if the statement had bell hard coded using static SQL. main() { exec sql include sqlca; exec sql begin declare section; char stmtbuf[301]; float searchvalue; float newvalue; exec sql end declare section; char tblname[30]; char searchcol[30]; char updatecol[30]; char yesno[30];

Page No : 51 of 56

SQL

printf(\n Enter table name to be updated ; ); gets(tblname); printf(\n Enter name of column to be searched ); gets(searchcol); printf(\n Enter name of column to be updated ); sprintf(stmtbuf, update %s set %s= ? where %s=? tblename,updatecol,searchcol ); exec sql prepare mystmt from : stmtbuf ; if (sqlca.cod ) { printf(\n Prepare error : %ld, sqlca.sqlcode); exit(); } for( ; ; ) { printf(\n Enter the search value for %s, searchcol ); scanf(%f, &searchvale); printf(\n Enter new value for %s,updatecol); scanf(%f, &newvalue); exec sql execute mystmt using : searchvalue, : newvalue; if ( sqlca.sqlcode) { printf(\n Execute error : %ld, sqlca.code); exit(); } printf(\n Another Y/N ); gets(yesno); if (yesno == n ) break ; printf(\n Update complete); exit(); } } Dynamic Queries A dynamic version of declare cursor statement declares a cursor for query, declare cursor statement specifies the statement name that it will be used SELECT statement is constructed in buffer PREPARE DESCRIBE statement gives the description of query results SQLDA is to allocate block of memory to receive each column of query results. Dynamic OPEN FETCH CLOSE

Page No : 52 of 56

SQL

struct sqlda { unsigned char sqldaid[8]; long sqldabc; short sqln; short sqld; struct sqlvar { short sqltype; short sqllen; unsigned char *sqldata; short *sqlind; struct { short length; unsigned char data[33]; }sqlnqme; } sqlvq[1]; }; API Functions dblogin() dbopen() dbuse() dbexit() dbcmd() dbsqlexec() dbresults() dbcancel() dbmsghandle dberrhandle() dbbind() dbnextrow() dbnumcols() dbcolname() dbcoltype(0 dbcollen() dbdata() dbdatalet() dbcanquery() API Concepts An application program communicates with DBMS exclusively through a set of calls that are collectively known as an application program interface or API. The basic operation of a typical DBMS API is given below. login data structure information opens a connection to SQL establishes default database closes a connection to SQL passes SQL statement to dblib executes a statement obtains results of next SQL statement. cancels remainder of statement establishes user written message handler procedure establishes user written error handler procedure binds a query results column to a variable fetches the next row of query results obtains the number of columns of query results obtains name of query results column. obtsind the data type of column obtains max length of query results obtains a pointer to retrieved data value obtains the actual length of retrieved value cancels a query before all rows are fetched

Page No : 53 of 56

SQL

The program begins its database access with one or more API calls that connect the program to the DBMS and often to a specific database. To send a SQL statement to DBMS, the program builds the statement as a text string in a buffer contents to the DBMS. The program makes API calls to check the status of its DBMS request and to handle errors. It the request is a query, the program uses API calls to retrieve the query results into the programs buffers. Typically the calls return data a row at a time or a column at a time. The program ends its database access with an API call that disconnects it from DBMS. The basic interaction between a program and SQL server is illustrated by the following.T he program prepares a login record filling the user name, password and any other information required to connect the DBMS. The program calls dbopen() to establish a connection to DBMS. A connection must exist before the program can send SQL statements to SQL server. The program builds a SQL statement in a buffer and calls dbcmd() to pass the SQL text to dblib. Successive calls to dbcmd() add to the previously passed text, there is no requirement that a complete SQL statement be sent in a single dbcmd() call. The program calls dbsqlexec(), instructing SQL server to execute the statement previously passed with dbcmd(). The program calls dbresults() to determine the success or failure of statement. The program calls dbexit() to close down the connection to SQL server. main() { LIGINREC *loginrec; DBPROCESS * dbproc; char amount_str[31]; int status; loginrec = dblogin(); DBSETUSER(loginrec, Prakash); DBSETPWD(loginrec, Tiger); dbproc = dbopen(loginrec, ); printf(\n Raise/lower by houw much); gets(amount_str); dbcmd(dbproc, update salesreps set QUOTA = QUOTA + ); dbcmd(dbproc, amount_str); dbsqlexec(dbproc); status = dbresults(dbproc); if status != SUCCESS) printf(\n Error durng update); else printf(\n Update successful); dbexit(dbproc); exit(); }

Page No : 54 of 56

SQL

User written error handling functions are called whenever an error occurs during the execution of SQL statement. int errcode; char errmsg[256]; int msg_rtn(dbproc, msgno, msgstate, severity, msttext); DBPROCESS *dbproc; DBINT msgno; int msgstate; int severity; char *msgtext; extern int errcode; estern char *errmsg; { printf(\n Error : %d Message : %s, msgno, msgtext); errcode = msgno; strcpy(errmsg, msgtext); return(0); } main() DBPROCESS *DBPROC; dberrhandle(msg_rtn); dbcmd(dbproc, delete from salesreps where QUOTA < 15,00.00); dbsqlexec(dbproc); dbresults(dbproc); dbproc msgno msgstate severity msgtest connection on which error occurred the SQL error number providing nformation about error context a number indicating seriousness of error an error message corresponding to msgno.

SQL Server Queries The program uses dbcmd() and dbsqlecex() call to pass SELECT statement to SQL server and request its execution. When the program call dbresults() for SELECT statement, dblib returns the completion status for query and also makes the query results available for processing. The program calls dbbinf(0 one for each column of query results, telling dblib where it should return the data for particular column. The arguments to dbbind(0 indicate the column number, the buffer to receive its data, the size of buffer, and espected data type.

Page No : 55 of 56

SQL

The program loops, callind dbnextrow() repeatedly to obtain the query results. The API places returned data into the data areas indicated in previous dbbind() calls. When no more rows of query results are available, the dbnextrow() call returns NO_MORE+ROWS. main() { LOGINREC *loginrec; DBPROCESS *dbproc; char rename[16]; short requota; float repsales; loginrec = dblogin() DBSETUSER(loginrec, xyz); DBSETPWD(loginrec, abc0; dbproc = dbopen(loginrec, ); dbcmd(dbproc, select name, quota, sales, from salesreps); dbcmd(dbproc, where sales > quota order by name); dbsqlexec(dbproc); dbresults(dbproc); dbbind(dbproc, 1, NTBSTRINBIND, 16, &repname); dbbind(dbproc, 2, FLT4BIND, 0, & repquota); dbbind(dbproc, 3, FLT$BIND, 0, &repsales); while( status = dbnextroe(dbproc) = = SUCCESS ) { printf(\n Name : %s repname); printf(\n Quota : %f repquota); printf(\n Sales : %f, repsales); } if ( status = = FAIL ); { printf(\n SQL error :); dbexit(dbproc); } exit(); }

Page No : 56 of 56

Das könnte Ihnen auch gefallen