Null in Relational Algebra

老子叫甜甜 提交于 2020-03-19 06:11:59

问题


I want to query the id of all apartments that were never rented

I tried something like this:

(π a_id
(apartments))
-
(π a_id
σ from_date Exists ∧ end_date Exists 
(rental) ⨝ rental.a_id on apartment.a_id (apartment))

But I think I cannot use Exist in relational algebra or null or anything.

How could I do it?

Thanks

I attach the schema here


回答1:


For the most straightforward relational algebra, where a relation has an attribute set as heading & tuple set as body:

Presumably apartment ids in Apartment are for apartments & apartment ids in Rental are for rented apartments. Then the unrented apartments are the ones in Apartment but not in Rental. Their ids are the ones in a relational difference between projections of those relations.

Guessing at the legend/key for your ERD, there is a FK (foreign key) in Rental referencing Apartment. That confirms that an apartment in Rental is also in Apartment. So Apartment ⨝ Rental has the same apartments as Rental. That confirms that you don't need to join; you can just use Rental for rented apartments.

Here is how we can query & reason more generally based on what rows in tables mean.

You mention NULL & EXISTS. Maybe you are talking about SQL NULL & EXISTS and/or you are trying to find a relational algebra version of an SQL query and/or you are reasoning in SQL. And/or maybe you are talking about logic EXISTS & whether values exist in columns or tuples.

From common sense about renting & from you not saying otherwise, Rental might be rows where occupant O rented apartment A from date F to date T. But you mention NULL. From common sense & guessing T can be NULL, Rental seems to be rows where occupant O rented apartment A from date F to date T OR occupant O rented apartment A from date F ongoing & T is null. A tuple membership condition like these is a (characteristic) predicate. We cannot update or query about the business situation without being told each base relation's predicate.

NULL is a value that is treated specially by SQL operators & syntax. We don't know how your algebra & language treat NULL. In mathematics EXISTS X [p] & FOR SOME X [p] say that a value exists that we can name X that satisfies condition p. SQL EXISTS (R) says whether rows exist in table R. That is whether EXISTS t [t IN R]. When R is (X,...) rows where r, that is whether EXISTS X,... [r].

When R is rows where r, π x (R) is by definition rows where EXISTS non-x attributes of R [r]. So π A (Rental) is rows where EXISTS O,F,T [occupant O rented apartment A from date F to date T OR occupant O rented apartment A from date F ongoing & T is null].

When R is rows where r, σ p (R) is by definition rows where r & p. Rows where occupant O rented apartment A from date F ongoing & T is null is rows where (occupant O rented apartment A from date F to date T OR occupant O rented apartment A from date F ongoing & T is null) & T is null. That's σ T is null (Rental).

When R is rows where r & S is rows where s, R - S is by definition rows where r & NOT s. Suppose Apartment is rows where apartment A has S square feet .... You want rows where EXISTS S,... [apartment A has S square feet ...] & NOT EXISTS O,F,T [occupant O rented apartment A from date F to date T OR occupant O rented apartment A from date F ongoing & T is null]. That's π A (Apartment) - π A (Rental). That's the relation difference at the start of this answer.

PS

Every query expression has an associated (characteristic) predicate--statement template parameterized by attributes. The tuples that make the predicate into a true proposition--statement--are in the relation.

We are given the predicates for expressions that are relation names.

Let query expression E have predicate e. Then:

  • R ⨝ S has predicate / is rows satisfying r and s

  • R ∪ S has predicate / is rows satisfying r or s

  • R - S has predicate / is rows satisfying r and not s

  • σ p (R) has predicate / is rows satisfying r and p

  • π A (R) has predicate / is rows satisfying exists non-A attributes of R [r]

When we want the tuples satisfying a certain predicate we find a way to express that predicate in terms of relation operator transformations of given relation predicates. The corresponding query returns/calculates the tuples.

Re relational algebra querying.



来源:https://stackoverflow.com/questions/55663848/null-in-relational-algebra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!