• No se han encontrado resultados

Cosmopolitismo subalterno

In document DE SOUSA SANTOS y ME > (página 39-41)

If HTTP and HTML/XML are the building blocks of the Web, SQL and relational databases are the cornerstones of information systems. Because relational data- bases are the leading products for building information systems, and over 80% of the pages found on the Web are actually built from database content, relational technology can be legitimately considered a key ingredient of data-intensive Web applications.

The main principle of the relational technology is extremely simple: data are formatted in tables (called relations) consisting of rows and columns. Each table stores the “facts” about a distinct concept of the application domain, like products or customers, organized as rows of elementary values. For example, the database in Figure 1.15 includes two tables: PRODUCTandCUSTOMER. The prod- uct table has three columns: CODE,NAME, and PRICE, which respectively store the code, name, and price of each distinct product.

To uniquely identify the facts stored in a table, it is a good practice to de- fine that a group of columns form a keyof the table. This definition implies that two rows cannot coexist in the table if they have coincident values of all the key columns. Therefore, the key of a table gives a unique “identity” to each one of the table rows. In the example of Figure 1.15, the key of table productis the CODE

column, and the key of table customeris the IDcolumn. These columns are un- derlined in Figure 1.15 to denote that they are the key of the respective table.

The notion of keyis central in relational databases, because it permits the expression of semantic relationships between the objects of the application domain. For example, the relationship between the customers and the products they have purchased, along with the information of the total quantity of each product purchased by each customer, can be represented using the table illus- trated in Figure 1.16. Each row in the table specifies the product (PROD-CODE), the

1.4 SQL: The Structured Query Language for Relational Databases 33

CUSTOMER

ID NAME ADDRESS

1 Acme Inc. 1 First St. Memphis TN

2 Gizmo Inc. 2 Second Av. New York NY

3 Widget Inc. 5 Third Av. S. Francisco CA

4 Supply Inc. 8 Fifth Rd. London UK

Figure 1.15 Tables in a relational database.

PRODUCT

CODE NAME PRICE

100 Aladdin 35

101 Blue moon 67

102 Casper 40

104 Dundee 21

Figure 1.16 Table storing purchases of products performed by customers.

PURCHASE

PROD-CODE CUST-ID QUANTITY

100 1 30

102 1 15

104 4 43

101 3 30

customer (CUST-ID), and the total purchased quantity (QUANTITY). The columns PROD-CODE andCUST-IDconstitute the key of the PURCHASEtable, because it does not make sense to allow multiple rows recording the total purchased quan- tityfor the same pair (product, customer).

The three tables shown in Figure 1.15 and Figure 1.16 constitute a small database. By combining the data stored in the three tables it is possible to extract complex information; for instance, which products were bought by “Acme Inc.” or how many “Aladdin” lamps were bought by all the customers over time. The combination of information occurs by comparing values in the columns that con- stitute the keys of the three relations; such a “navigation” from one table to an- other one is expressed by means of query languages.

TheStructured Query Language (SQL) is the most popular language for ex- tracting and manipulating the information stored into relational tables. SQL offers the SELECT statement for querying data, and the INSERT, DELETE, and UPDATEstatements for changing the database content.

A SELECTstatement permits you to extract the desired information from one or more tables, formatted as a table of result rows. The basic form of a SE- LECTstatement is:

SELECT column-list FROM table-list WHERE condition

The meaning of the statement is to build a table composed of the columns specified in the column list (SELECTclause), assembled from the values of the rows of the tables mentioned in the table list (FROMclause) that satisfy some con- dition (WHEREclause).

For example, to retrieve the name and price of all products costing less than $50, the following query can be formulated:

SELECT NAME, PRICE FROM PRODUCT WHERE PRICE < 50

Returning as result the table in Figure 1.17.

A more complex example is the query that retrieves the names of all the cus- tomers who have purchased products costing less than $50, together with the name of the purchased products. To fulfill this query, it is necessary to consider three tables: the PRODUCT,CUSTOMER, and PURCHASEtables. In the relational ter- minology, the PRODUCTtable must be “joined” to the PURCHASEtable to obtain a table that includes for every product the IDs of customers who have purchased

it, and then this intermediate table must be in turn joined to the CUSTOMERtable, to add the information about the customer’s name and address. The following query does the job:

SELECT PRODUCT.NAME, CUSTOMER.NAME FROM PRODUCT,PURCHASE, CUSTOMER

WHERE PRODUCT.CODE = PURCHASE.PROD-CODE AND CUSTOMER.ID = PURCHASE.CUST-ID AND PRODUCT.PRICE < 50

TheWHEREcondition includes both “join conditions” (PRODUCT.CODE = PUR- CHASE.PROD-CODEandCUSTOMER.ID = PURCHASE.CUST-ID) and row selection condition (PRODUCT.PRICE < 50). The result of the query applied to the three tables shown in Figure 1.15 and Figure 1.16 is the table shown in Figure 1.18.

The SQL SELECTstatement has more options than table joins and row se- lection; it includes primitives for applying aggregate functions to sets of rows, sorting and grouping query results, and building complex nested queries.

Figure 1.19 shows the various ways in which SQL queries can be submitted to the database. As a first option SQL queries can be posed directly by the user, using a dedicated graphical user interface. However, such an interface is rarely used, and queries are instead normally embedded inside programs, which inter- act with the database to extract or update its content according to the business logic of the application. The interaction between an external program and the database query processor takes place through the Application Programming Interface (API) of the database, which offers procedures for shipping queries to the database and for getting results back.

To ensure the portability of programs across databases of different vendors, which may have different APIs, special-purpose libraries are available that offer a standard set of functions masking the peculiarities of the specific database prod- uct. The most diffused interoperability libraries are the Open Database Connectiv- ity(ODBC) library by Microsoft, and the Java Database Connectivity(JDBC) library,

1.4 SQL: The Structured Query Language for Relational Databases 35

Figure 1.17 Result of a SQL query.

NAME PRICE

Aladdin 35

Casper 40

Dundee 21

Figure 1.18 Result of a SQL query.

PRODUCT.NAME CUSTOMER.NAME

Aladdin Acme Inc.

Casper Acme Inc.

by Javasoft, which is part of the Java 2 Enterprise Edition platform. These libraries expose a set of utility objects (for example, the “database connection” object, the “statement” object, the “result set” object), which hide the details of database in- teraction and facilitate the programming of database-aware applications. Pro- grams that build dynamic Web pages from database content typically use the ODBC or JDBC libraries.

In document DE SOUSA SANTOS y ME > (página 39-41)