官术网_书友最值得收藏!

The select and project operations

SELECT is used to restrict tuples from the relation. SELECT always returns a unique set of tuples this is inherited form entity integrity constraint. For example, the query give me the customer information where the customer_id equals to 2 is written as follows:

σcustomer_id =2 customer

The selection, as mentioned earlier, is commutative; the query give me all customers where the customer mail is known, and the customer first name is kim is written in three different ways, as follows:

σemail is not nullfirst_name =kim customer)

σfirst_name =kimemail is not null customer)

σfirst_name =kim and email is not null (customer)

The selection predicates are certainly determined by the data types. For numeric data types, the comparison operator might be ≠, =, <, >, ≥, or ≤. The predicate expression can also contain complex expressions and functions. The equivalent SQL statement for the SELECT operator is the SELECT * statement, and the predicate is defined in the WHERE clause.

The symbol * means all the relation attributes; note that in the production environment, it is not recommended to use *. Instead, one should list all the relation attributes explicitly.

The following SELECT statement is equivalent for the relational algebra expression σ</span>customer_id =2 customer:

SELECT * FROM customer WHERE customer_id = 2;

The project operation could be visualized as vertical slicing of the table. The query, give me the customer names, is written in relational algebra as follows:

π first_name, last_name customer

The following is the result of projection expression:

Duplicate tuples are not allowed in the formal relational model; the number of returned tuples from the PROJECT operator is always equal to or less than the number of total tuples in the relation. If a PROJECT operator's attribute list contains a primary key, then the resulting relation has the same number of tuples as the projected relation.

The projection operator also can be optimized, for example, cascading projections could be optimized as the following expression:

πaa,πb(R)) = πa(R)

The SQL equivalent for the PROJECT operator is SELECT DISTINCT. The DISTINCT keyword is used to eliminate duplicates. To get the result shown in the preceding expression, one could execute the following SQL statement:

SELECT DISTINCT first_name, last_name FROM customers;

The sequence of the execution of the PROJECT and SELECT operations can be interchangeable in some cases. The query give me the name of the customer with customer_id equal to 2 could be written as follows:

σcustomer_id =2 (π first_name, last_name customer)

π first_name, last_name(σcustomer_id =2 customer)

In other cases, the PROJECT and SELECT operators must have an explicit order as shown in the following example; otherwise, it will lead to an incorrect expression. The query, give me the last name of the customers where the first name is kim, could be written in the following way:

π last_name(σfirst_name=kim customer)

主站蜘蛛池模板: 惠安县| 武穴市| 石泉县| 日喀则市| 昆山市| 柳江县| 陕西省| 和硕县| 西和县| 蓝田县| 壤塘县| 宁陕县| 荔波县| 邯郸市| 嫩江县| 南岸区| 罗定市| 礼泉县| 韩城市| 尼勒克县| 翼城县| 前郭尔| 威宁| 宁波市| 韶山市| 临沂市| 永吉县| 永泰县| 崇仁县| 张家港市| 方山县| 石阡县| 桐庐县| 屏边| 平乐县| 沧源| 社旗县| 海晏县| 黔西县| 繁昌县| 平武县|