Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Relational algebra
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Operations for domain computations === There is nothing in relational algebra introduced so far that would allow computations on the data domains (other than evaluation of propositional expressions involving equality). For example, it is not possible using only the algebra introduced so far to write an expression that would multiply the numbers from two columns, e.g. a unit price with a quantity to obtain a total price. Practical query languages have such facilities, e.g. the SQL SELECT allows arithmetic operations to define new columns in the result <syntaxhighlight lang="sql" inline>SELECT unit_price * quantity AS total_price FROM t</syntaxhighlight>, and a similar facility is provided more explicitly by Tutorial D's <code>EXTEND</code> keyword.<ref name="Date2011">{{cite book|author=C. J. Date|title=SQL and Relational Theory: How to Write Accurate SQL Code|url=https://books.google.com/books?id=WuZGD5tBfMwC&pg=PA133|year=2011|publisher=O'Reilly Media, Inc.|isbn=978-1-4493-1974-8|pages=133β135}}</ref> In database theory, this is called '''extended projection'''.<ref name="Garcia-MolinaUllman2009"/>{{rp|213}} ==== Aggregation ==== Furthermore, computing various functions on a column, like the summing up of its elements, is also not possible using the relational algebra introduced so far. There are five [[aggregate function]]s that are included with most relational database systems. These operations are Sum, Count, Average, Maximum and Minimum. In relational algebra the aggregation operation over a schema (''A''<sub>1</sub>, ''A''<sub>2</sub>, ... ''A''<sub>''n''</sub>) is written as follows: :<math>G_1, G_2, \ldots, G_m\ g_{f_1({A_1}'), f_2({A_2}'), \ldots, f_k({A_k}')}\ (r)</math> where each ''A''<sub>''j''</sub>', 1 β€ ''j'' β€ ''k'', is one of the original attributes ''A''<sub>''i''</sub>, 1 β€ ''i'' β€ ''n''. The attributes preceding the ''g'' are grouping attributes, which function like a "group by" clause in SQL. Then there are an arbitrary number of aggregation functions applied to individual attributes. The operation is applied to an arbitrary relation ''r''. The grouping attributes are optional, and if they are not supplied, the aggregation functions are applied across the entire relation to which the operation is applied. Let's assume that we have a table named {{mono|Account}} with three columns, namely {{mono|Account_Number, Branch_Name}} and {{mono|Balance}}. We wish to find the maximum balance of each branch. This is accomplished by <sub>{{mono|Branch_Name}}</sub>''G''<sub>Max({{mono|Balance}})</sub>({{mono|Account}}). To find the highest balance of all accounts regardless of branch, we could simply write ''G''<sub>Max({{mono|Balance}})</sub>({{mono|Account}}). Grouping is often written as <sub>{{mono|Branch_Name}}</sub>''Ι£''<sub>Max({{mono|Balance}})</sub>({{mono|Account}}) instead.<ref name="Garcia-MolinaUllman2009" />
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)