|
The SQL LanguageAs is the case with most modern relational languages, SQL is based on the tuple relational calculus. As a result every query that can be formulated using the tuple relational calculus (or equivalently, relational algebra) can also be formulated using SQL. There are, however, capabilities beyond the scope of relational algebra or calculus. Here is a list of some additional features provided by SQL that are not part of relational algebra or calculus:
SelectThe most often used command in SQL is the SELECT statement, used to retrieve data. The syntax is: SELECT [ALL|DISTINCT] { * | expr_1 [AS c_alias_1] [, ... [, expr_k [AS c_alias_k]]]} FROM table_name_1 [t_alias_1] [, ... [, table_name_n [t_alias_n]]] [WHERE condition] [GROUP BY name_of_attr_i [,... [, name_of_attr_j]] [HAVING condition]] [{UNION [ALL] | INTERSECT | EXCEPT} SELECT ...] [ORDER BY name_of_attr_i [ASC|DESC] [, ... [, name_of_attr_j [ASC|DESC]]]]; Now we will illustrate the complex syntax of the SELECT statement with various examples. The tables used for the examples are defined in The Suppliers and Parts Database. Simple SelectsHere are some simple examples using a SELECT statement: Example 2-4. Simple Query with Qualification To retrieve all tuples from table PART where the attribute PRICE is greater than 10 we formulate the following query: SELECT * FROM PART WHERE PRICE > 10;and get the table: PNO | PNAME | PRICE -----+---------+-------- 3 | Bolt | 15 4 | Cam | 25 Using "*" in the SELECT statement will deliver all attributes from the table. If we want to retrieve only the attributes PNAME and PRICE from table PART we use the statement: SELECT PNAME, PRICE FROM PART WHERE PRICE > 10;In this case the result is: PNAME | PRICE --------+-------- Bolt | 15 Cam | 25Note that the SQL SELECT corresponds to the "projection" in relational algebra not to the "selection" (see Relational Algebra for more details). The qualifications in the WHERE clause can also be logically connected using the keywords OR, AND, and NOT: SELECT PNAME, PRICE FROM PART WHERE PNAME = 'Bolt' AND (PRICE = 0 OR PRICE < 15);will lead to the result: PNAME | PRICE --------+-------- Bolt | 15 Arithmetic operations may be used in the target list and in the WHERE clause. For example if we want to know how much it would cost if we take two pieces of a part we could use the following query: SELECT PNAME, PRICE * 2 AS DOUBLE FROM PART WHERE PRICE * 2 < 50;and we get: PNAME | DOUBLE --------+--------- Screw | 20 Nut | 16 Bolt | 30Note that the word DOUBLE after the keyword AS is the new title of the second column. This technique can be used for every element of the target list to assign a new title to the resulting column. This new title is often referred to as alias. The alias cannot be used throughout the rest of the query. JoinsThe following example shows how joins are realized in SQL. To join the three tables SUPPLIER, PART and SELLS over their common attributes we formulate the following statement: SELECT S.SNAME, P.PNAME FROM SUPPLIER S, PART P, SELLS SE WHERE S.SNO = SE.SNO AND P.PNO = SE.PNO;and get the following table as a result: SNAME | PNAME -------+------- Smith | Screw Smith | Nut Jones | Cam Adams | Screw Adams | Bolt Blake | Nut Blake | Bolt Blake | Cam In the FROM clause we introduced an alias name for every relation because there are common named attributes (SNO and PNO) among the relations. Now we can distinguish between the common named attributes by simply prefixing the attribute name with the alias name followed by a dot. The join is calculated in the same way as shown in An Inner Join. First the Cartesian product SUPPLIER × PART × SELLS is derived. Now only those tuples satisfying the conditions given in the WHERE clause are selected (i.e. the common named attributes have to be equal). Finally we project out all columns but S.SNAME and P.PNAME. Aggregate OperatorsSQL provides aggregate operators (e.g. AVG, COUNT, SUM, MIN, MAX) that take the name of an attribute as an argument. The value of the aggregate operator is calculated over all values of the specified attribute (column) of the whole table. If groups are specified in the query the calculation is done only over the values of a group (see next section). Example 2-5. Aggregates If we want to know the average cost of all parts in table PART we use the following query: SELECT AVG(PRICE) AS AVG_PRICE FROM PART; The result is: AVG_PRICE ----------- 14.5 If we want to know how many parts are stored in table PART we use the statement: SELECT COUNT(PNO) FROM PART;and get: COUNT ------- 4 Aggregation by GroupsSQL allows one to partition the tuples of a table into groups. Then the aggregate operators described above can be applied to the groups (i.e. the value of the aggregate operator is no longer calculated over all the values of the specified column but over all values of a group. Thus the aggregate operator is evaluated individually for every group.) The partitioning of the tuples into groups is done by using the keywords GROUP BY followed by a list of attributes that define the groups. If we have GROUP BY A1, ⃛, Ak we partition the relation into groups, such that two tuples are in the same group if and only if they agree on all the attributes A1, ⃛, Ak. Example 2-6. Aggregates If we want to know how many parts are sold by every supplier we formulate the query: SELECT S.SNO, S.SNAME, COUNT(SE.PNO) FROM SUPPLIER S, SELLS SE WHERE S.SNO = SE.SNO GROUP BY S.SNO, S.SNAME;and get: SNO | SNAME | COUNT -----+-------+------- 1 | Smith | 2 2 | Jones | 1 3 | Adams | 2 4 | Blake | 3 Now let's have a look of what is happening here. First the join of the tables SUPPLIER and SELLS is derived: S.SNO | S.SNAME | SE.PNO -------+---------+-------- 1 | Smith | 1 1 | Smith | 2 2 | Jones | 4 3 | Adams | 1 3 | Adams | 3 4 | Blake | 2 4 | Blake | 3 4 | Blake | 4 Next we partition the tuples into groups by putting all tuples together that agree on both attributes S.SNO and S.SNAME: S.SNO | S.SNAME | SE.PNO -------+---------+-------- 1 | Smith | 1 | 2 -------------------------- 2 | Jones | 4 -------------------------- 3 | Adams | 1 | 3 -------------------------- 4 | Blake | 2 | 3 | 4 In our example we got four groups and now we can apply the aggregate operator COUNT to every group leading to the total result of the query given above. Note that for the result of a query using GROUP BY and aggregate operators to make sense the attributes grouped by must also appear in the target list. All further attributes not appearing in the GROUP BY clause can only be selected by using an aggregate function. On the other hand you can not use aggregate functions on attributes appearing in the GROUP BY clause. HavingThe HAVING clause works much like the WHERE clause and is used to consider only those groups satisfying the qualification given in the HAVING clause. The expressions allowed in the HAVING clause must involve aggregate functions. Every expression using only plain attributes belongs to the WHERE clause. On the other hand every expression involving an aggregate function must be put to the HAVING clause. Example 2-7. Having If we want only those suppliers selling more than one part we use the query: SELECT S.SNO, S.SNAME, COUNT(SE.PNO) FROM SUPPLIER S, SELLS SE WHERE S.SNO = SE.SNO GROUP BY S.SNO, S.SNAME HAVING COUNT(SE.PNO) > 1;and get: SNO | SNAME | COUNT -----+-------+------- 1 | Smith | 2 3 | Adams | 2 4 | Blake | 3 SubqueriesIn the WHERE and HAVING clauses the use of subqueries (subselects) is allowed in every place where a value is expected. In this case the value must be derived by evaluating the subquery first. The usage of subqueries extends the expressive power of SQL. Example 2-8. Subselect If we want to know all parts having a greater price than the part named 'Screw' we use the query: SELECT * FROM PART WHERE PRICE > (SELECT PRICE FROM PART WHERE PNAME='Screw'); The result is: PNO | PNAME | PRICE -----+---------+-------- 3 | Bolt | 15 4 | Cam | 25 When we look at the above query we can see the keyword SELECT two times. The first one at the beginning of the query - we will refer to it as outer SELECT - and the one in the WHERE clause which begins a nested query - we will refer to it as inner SELECT. For every tuple of the outer SELECT the inner SELECT has to be evaluated. After every evaluation we know the price of the tuple named 'Screw' and we can check if the price of the actual tuple is greater. If we want to know all suppliers that do not sell any part (e.g. to be able to remove these suppliers from the database) we use: SELECT * FROM SUPPLIER S WHERE NOT EXISTS (SELECT * FROM SELLS SE WHERE SE.SNO = S.SNO); In our example the result will be empty because every supplier sells at least one part. Note that we use S.SNO from the outer SELECT within the WHERE clause of the inner SELECT. As described above the subquery is evaluated for every tuple from the outer query i.e. the value for S.SNO is always taken from the actual tuple of the outer SELECT. Union, Intersect, ExceptThese operations calculate the union, intersect and set theoretic difference of the tuples derived by two subqueries. Example 2-9. Union, Intersect, Except The following query is an example for UNION: SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Jones' UNION SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Adams';gives the result: SNO | SNAME | CITY -----+-------+-------- 2 | Jones | Paris 3 | Adams | Vienna Here an example for INTERSECT: SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 INTERSECT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 2;gives the result: SNO | SNAME | CITY -----+-------+-------- 2 | Jones | Paris The only tuple returned by both parts of the query is the one having $SNO=2$. Finally an example for EXCEPT: SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 EXCEPT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 3;gives the result: SNO | SNAME | CITY -----+-------+-------- 2 | Jones | Paris 3 | Adams | Vienna Data DefinitionThere is a set of commands used for data definition included in the SQL language. Create TableThe most fundamental command for data definition is the one that creates a new relation (a new table). The syntax of the CREATE TABLE command is: CREATE TABLE table_name (name_of_attr_1 type_of_attr_1 [, name_of_attr_2 type_of_attr_2 [, ...]]); Example 2-10. Table Creation To create the tables defined in The Suppliers and Parts Database the following SQL statements are used: CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20), CITY VARCHAR(20)); CREATE TABLE PART (PNO INTEGER, PNAME VARCHAR(20), PRICE DECIMAL(4 , 2)); CREATE TABLE SELLS (SNO INTEGER, PNO INTEGER); Data Types in SQLThe following is a list of some data types that are supported by SQL:
Create IndexIndices are used to speed up access to a relation. If a relation R has an index on attribute A then we can retrieve all tuples t having t(A) = a in time roughly proportional to the number of such tuples t rather than in time proportional to the size of R. To create an index in SQL the CREATE INDEX command is used. The syntax is: CREATE INDEX index_name ON table_name ( name_of_attribute );
Example 2-11. Create Index To create an index named I on attribute SNAME of relation SUPPLIER we use the following statement: CREATE INDEX I ON SUPPLIER (SNAME); The created index is maintained automatically, i.e. whenever a new tuple is inserted into the relation SUPPLIER the index I is adapted. Note that the only changes a user can percept when an index is present are an increased speed. Create ViewA view may be regarded as a virtual table, i.e. a table that does not physically exist in the database but looks to the user as if it does. By contrast, when we talk of a base table there is really a physically stored counterpart of each row of the table somewhere in the physical storage. Views do not have their own, physically separate, distinguishable stored data. Instead, the system stores the definition of the view (i.e. the rules about how to access physically stored base tables in order to materialize the view) somewhere in the system catalogs (see System Catalogs). For a discussion on different techniques to implement views refer to SIM98. In SQL the CREATE VIEW command is used to define a view. The syntax is: CREATE VIEW view_name AS select_stmtwhere select_stmt is a valid select statement as defined in Select. Note that select_stmt is not executed when the view is created. It is just stored in the system catalogs and is executed whenever a query against the view is made. Let the following view definition be given (we use the tables from The Suppliers and Parts Database again): CREATE VIEW London_Suppliers AS SELECT S.SNAME, P.PNAME FROM SUPPLIER S, PART P, SELLS SE WHERE S.SNO = SE.SNO AND P.PNO = SE.PNO AND S.CITY = 'London'; Now we can use this virtual relation London_Suppliers as if it were another base table: SELECT * FROM London_Suppliers WHERE P.PNAME = 'Screw';which will return the following table: SNAME | PNAME -------+------- Smith | Screw To calculate this result the database system has to do a hidden access to the base tables SUPPLIER, SELLS and PART first. It does so by executing the query given in the view definition against those base tables. After that the additional qualifications (given in the query against the view) can be applied to obtain the resulting table. Drop Table, Drop Index, Drop ViewTo destroy a table (including all tuples stored in that table) the DROP TABLE command is used: DROP TABLE table_name; To destroy the SUPPLIER table use the following statement: DROP TABLE SUPPLIER; The DROP INDEX command is used to destroy an index: DROP INDEX index_name; Finally to destroy a given view use the command DROP VIEW: DROP VIEW view_name; Data ManipulationInsert IntoOnce a table is created (see Create Table), it can be filled with tuples using the command INSERT INTO. The syntax is: INSERT INTO table_name (name_of_attr_1 [, name_of_attr_2 [,...]]) VALUES (val_attr_1 [, val_attr_2 [, ...]]); To insert the first tuple into the relation SUPPLIER (from The Suppliers and Parts Database) we use the following statement: INSERT INTO SUPPLIER (SNO, SNAME, CITY) VALUES (1, 'Smith', 'London'); To insert the first tuple into the relation SELLS we use: INSERT INTO SELLS (SNO, PNO) VALUES (1, 1); UpdateTo change one or more attribute values of tuples in a relation the UPDATE command is used. The syntax is: UPDATE table_name SET name_of_attr_1 = value_1 [, ... [, name_of_attr_k = value_k]] WHERE condition; To change the value of attribute PRICE of the part 'Screw' in the relation PART we use: UPDATE PART SET PRICE = 15 WHERE PNAME = 'Screw'; The new value of attribute PRICE of the tuple whose name is 'Screw' is now 15. DeleteTo delete a tuple from a particular table use the command DELETE FROM. The syntax is: DELETE FROM table_name WHERE condition; To delete the supplier called 'Smith' of the table SUPPLIER the following statement is used: DELETE FROM SUPPLIER WHERE SNAME = 'Smith'; System CatalogsIn every SQL database system system catalogs are used to keep track of which tables, views indexes etc. are defined in the database. These system catalogs can be queried as if they were normal relations. For example there is one catalog used for the definition of views. This catalog stores the query from the view definition. Whenever a query against a view is made, the system first gets the view definition query out of the catalog and materializes the view before proceeding with the user query (see Simkovics, 1998 for a more detailed description). For more information about system catalogs refer to Date, 1994. Embedded SQLIn this section we will sketch how SQL can be embedded into a host language (e.g. C). There are two main reasons why we want to use SQL from a host language:
A program using embedded SQL in a host language consists of statements of the host language and of embedded SQL (ESQL) statements. Every ESQL statement begins with the keywords EXEC SQL. The ESQL statements are transformed to statements of the host language by a precompiler (which usually inserts calls to library routines that perform the various SQL commands). When we look at the examples throughout Select we realize that the result of the queries is very often a set of tuples. Most host languages are not designed to operate on sets so we need a mechanism to access every single tuple of the set of tuples returned by a SELECT statement. This mechanism can be provided by declaring a cursor. After that we can use the FETCH command to retrieve a tuple and set the cursor to the next tuple. For a detailed discussion on embedded SQL refer to Date and Darwen, 1997, Date, 1994, or Ullman, 1988. |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |