Relational Model
Relational Model
Overview
The relational model's central idea is to describe a database as a collection of
predicates over a finite set of predicate variables, describing constraints on the
possible values and combinations of values. The content of the database at any
given time is a finite (logical) model of the database, i.e. a set of relations, one per
predicate variable, such that all predicates are satisfied. A request for information
from the database (a database query) is also a predicate.
Alternatives
Other models are the hierarchical model and network model. Some systems using
these older architectures are still
in use today in data centers with
high data volume needs, or where
existing systems are so complex
and abstract it would be
cost-prohibitive to migrate to
systems employing the relational
model; also of note are newer
object-oriented databases.
The relational model was the first database model to be described in formal
mathematical terms. Hierarchical and network databases existed before relational
databases, but their specifications were relatively informal. After the relational
model was defined, there were many attempts to compare and contrast the
different models, and this led to the emergence of more rigorous descriptions of
the earlier models; though the procedural nature of the data manipulation
interfaces for hierarchical and network databases limited the scope for
formalization.[5]
History
The relational model was invented by E.F. (Ted) Codd as a general model of data,
and subsequently maintained and developed by Chris Date and Hugh Darwen
among others. In The Third Manifesto (first published in 1995) Date and Darwen
show how the relational model can accommodate certain desired object-oriented
features.
Controversies
Codd himself, some years after publication of his 1970 model, proposed a three-
valued logic (True, False, Missing/NULL) version of it to deal with missing
information, and in his The Relational Model for Database Management Version 2
(1990) he went a step further with a four-valued logic (True, False, Missing but
Applicable, Missing but Inapplicable) version.[6] But these have never been
implemented, presumably because of attending complexity. SQL's NULL construct
was intended to be part of a three-valued logic system, but fell short of that due to
logical errors in the standard and in its implementations.[7]
Topics
The fundamental assumption of the relational model is that all data is represented
as mathematical n-ary relations, an n-ary relation being a subset of the Cartesian
product of n domains. In the mathematical model, reasoning about such data is
done in two-valued predicate logic, meaning there are two possible evaluations for
each proposition: either true or false (and in particular no third value such as
unknown, or not applicable, either of which are often associated with the concept
of NULL). Data are operated upon by means of a relational calculus or relational
algebra, these being equivalent in expressive power.
The relational model of data permits the database designer to create a consistent,
logical representation of information. Consistency is achieved by including
declared constraints in the database design, which is usually referred to as the
logical schema. The theory includes a process of database normalization whereby
a design with certain desirable properties can be selected from a set of logically
equivalent alternatives. The access plans and other implementation and operation
details are handled by the DBMS engine, and are not reflected in the logical
model. This contrasts with common practice for SQL DBMSs in which
performance tuning often requires changes to the logical model.
The basic relational building block is the domain or data type, usually abbreviated
nowadays to type. A tuple is an ordered set of attribute values. An attribute is an
ordered pair of attribute name and type name. An attribute value is a specific
valid value for the type of the attribute. This can be either a scalar value or a more
complex type.
The basic principle of the relational model is the Information Principle: all
information is represented by data values in relations. In accordance with this
Principle, a relational database is a set of relvars and the result of every query is
presented as a relation.
The consistency of a relational database is enforced, not by rules built into the
applications that use it, but rather by constraints, declared as part of the logical
schema and enforced by the DBMS for all applications. In general, constraints are
expressed using relational comparison operators, of which just one, "is subset of"
(⊆), is theoretically sufficient. In practice, several useful shorthands are expected
to be available, of which the most important are candidate key (really, superkey)
and foreign key constraints.
Interpretation
For a formal exposition of these ideas, see the section Set-theoretic Formulation,
below.
Application to databases
A data type as used in a typical relational database might be the set of integers,
the set of character strings, the set of dates, or the two boolean values true and
false, and so on. The corresponding type names for these types might be the
strings "int", "char", "date", "boolean", etc. It is important to understand, though,
that relational theory does not dictate what types are to be supported; indeed,
nowadays provisions are expected to be available for user-defined types in
addition to the built-in ones provided by the system.
Attribute is the term used in the theory for what is commonly referred to as a
column. Similarly, table is commonly used in place of the theoretical term
relation (though in SQL the term is by no means synonymous with relation). A
table data structure is specified as a list of column definitions, each of which
specifies a unique column name and the type of the values that are permitted for
that column. An attribute value is the entry in a specific column and row, such as
"John Doe" or "35".
A tuple is basically the same thing as a row, except in an SQL DBMS, where the
column values in a row are ordered. (Tuples are not ordered; instead, each
attribute value is identified solely by the attribute name and never by its ordinal
position within the tuple.) An attribute name might be "name" or "age".
SQL, initially pushed as the standard language for relational databases, deviates
from the relational model in several places. The current ISO SQL standard doesn't
mention the relational model or use relational terms or concepts. However, it is
possible to create a database conforming to the relational model using SQL if one
does not use certain SQL features.
The following deviations from the relational model have been noted in SQL. Note
that few database servers implement the entire SQL standard and in particular do
not allow some of these deviations. Whereas NULL is ubiquitous, for example,
allowing duplicate column names within a table or anonymous columns is
uncommon.
Duplicate rows
The same row can appear more than once in an SQL table. The same tuple
cannot appear more than once in a relation.
Anonymous columns
A column in an SQL table can be unnamed and thus unable to be referenced
in expressions. The relational model requires every attribute to be named and
referenceable.
Duplicate column names
Two or more columns of the same SQL table can have the same name and
therefore cannot be referenced, on account of the obvious ambiguity. The
relational model requires every attribute to be referenceable.
Column order significance
The order of columns in an SQL table is defined and significant, one
consequence being that SQL's implementations of Cartesian product and
union are both noncommutative. The relational model requires there to be no
significance to any ordering of the attributes of a relation.
Views without CHECK OPTION
Updates to a view defined without CHECK OPTION can be accepted but the
resulting update to the database does not necessarily have the expressed
effect on its target. For example, an invocation of INSERT can be accepted
but the inserted rows might not all appear in the view, or an invocation of
UPDATE can result in rows disappearing from the view. The relational model
requires updates to a view to have the same effect as if the view were a base
relvar.
Columnless tables unrecognized
SQL requires every table to have at least one column, but there are two
relations of degree zero (of cardinality one and zero) and they are needed to
represent extensions of predicates that contain no free variables.
NULL
This special mark can appear instead of a value wherever a value can appear
in SQL, in particular in place of a column value in some row. The deviation
from the relational model arises from the fact that the implementation of this
ad hoc concept in SQL involves the use of three-valued logic, under which the
comparison of NULL with itself does not yield true but instead yields the
third truth value, unknown; similarly the comparison NULL with something
other than itself does not yield false but instead yields unknown. It is because
of this behaviour in comparisons that NULL is described as a mark rather
than a value. The relational model depends on the law of excluded middle
under which anything that is not true is false and anything that is not false is
true; it also requires every tuple in a relation body to have a value for every
attribute of that relation. This particular deviation is disputed by some if only
because E. F. Codd himself eventually advocated the use of special marks and
a 4-valued logic, but this was based on his observation that there are two
distinct reasons why one might want to use a special mark in place of a value,
which led opponents of the use of such logics to discover more distinct
reasons and at least as many as 19 have been noted, which would require a
21-valued logic. SQL itself uses NULL for several purposes other than to
represent "value unknown". For example, the sum of the empty set is NULL,
meaning zero, the average of the empty set is NULL, meaning undefined, and
NULL appearing in the result of a LEFT JOIN can mean "no value because
there is no matching row in the right-hand operand".
Relational operations
Users (or programs) request data from a relational database by sending it a query
that is written in a special language, usually a dialect of SQL. Although SQL was
originally intended for end-users, it is much more common for SQL queries to be
embedded into software that provides an easier user interface. Many Web sites,
such as Wikipedia, perform SQL queries when generating pages.
In response to a query, the database returns a result set, which is just a list of
rows containing the answers. The simplest query is just to return all the rows from
a table, but more often, the rows are filtered in some way to return just the
answer wanted.
Often, data from multiple tables are combined into one, by doing a join.
Conceptually, this is done by taking all possible combinations of rows (the
Cartesian product), and then filtering out everything except the answer. In
practice, relational database management systems rewrite ("optimize") queries to
perform faster, using a variety of techniques.
Relations are classified based upon the types of anomalies to which they're
vulnerable. A database that's in the first normal form is vulnerable to all types of
anomalies, while a database that's in the domain/key normal form has no
modification anomalies. Normal forms are hierarchical in nature. That is, the
lowest level is the first normal form, and the database cannot meet the
requirements for higher level normal forms without first having met all the
requirements of the lesser normal forms.[8]
Examples
Database
Customer (Customer ID, Tax ID, Name, Address, City, State, Zip, Phone,
Email,Sex)
Order (Order No, Customer ID, Invoice No, Date Placed, Date Promised,
Terms, Status)
Order Line (Order No, Order Line No, Product Code, Qty)
Invoice (Invoice No, Customer ID, Order No, Date, Status)
Invoice Line (Invoice No, Invoice Line No, Product Code, Qty Shipped)
Product (Product Code, Product Description)
In this design we have six relvars: Customer, Order, Order Line, Invoice, Invoice
Line and Product. The bold, underlined attributes are candidate keys. The
non-bold, underlined attributes are foreign keys.
Usually one candidate key is chosen to be called the primary key and used in
preference over the other candidate keys, which are then called alternate keys.
Customer relation
Customer ID Tax ID Name Address [More fields…]
1234567890 555-5512222 Munmun 323 Broadway …
2223344556 555-5523232 Wile E. 1200 Main Street …
3334445563 555-5533323 Ekta 871 1st Street …
4232342432 555-5325523 E. F. Codd 123 It Way …
Foreign keys are integrity constraints enforcing that the value of the attribute set
is drawn from a candidate key in another relation. For example in the Order
relation the attribute Customer ID is a foreign key. A join is the operation that
draws on information from several relations at once. By joining relvars from the
example above we could query the database for all of the Customers, Orders, and
Invoices. If we only wanted the tuples for a specific customer, we would specify
this using a restriction condition.
There is a flaw in our database design above. The Invoice relvar contains an Order
No attribute. So, each tuple in the Invoice relvar will have one Order No, which
implies that there is precisely one Order for each Invoice. But in reality an invoice
can be created against many orders, or indeed for no particular order. Additionally
the Order relvar contains an Invoice No attribute, implying that each Order has a
corresponding Invoice. But again this is not always true in the real world. An
order is sometimes paid through several invoices, and sometimes paid without an
invoice. In other words there can be many Invoices per Order and many Orders
per Invoice. This is a many-to-many relationship between Order and Invoice (also
called a non-specific relationship). To represent this relationship in the database a
new relvar should be introduced whose role is to specify the correspondence
between Orders and Invoices:
Now, the Order relvar has a one-to-many relationship to the OrderInvoice table, as
does the Invoice relvar. If we want to retrieve every Invoice for a particular Order,
we can query for all orders where Order No in the Order relation equals the
Order No in OrderInvoice, and where Invoice No in OrderInvoice equals the
Invoice No in Invoice.
Set-theoretic formulation
Basic notions in the relational model are relation names and attribute names. We
will represent these as strings such as "Person" and "name" and we will usually
use the variables and to range over them. Another basic notion is
the set of atomic values that contains values such as numbers and strings.
Our first definition concerns the notion of tuple, which formalizes the notion of
row or record in a table:
Tuple
A tuple is a partial function from attribute names to atomic values.
Header
A header is a finite set of attribute names.
Projection
The projection of a tuple on a finite set of attributes is
.
The next definition defines relation that formalizes the contents of a table as it is
defined in the relational model.
Relation
A relation is a tuple with , the header, and , the body, a set of
tuples that all have the domain .
Relation universe
A relation universe over a header is a non-empty set of relations with
header .
Relation schema
A relation schema consists of a header and a predicate that is
defined for all relations with header . A relation satisfies a relation
schema if it has header and satisfies .
One of the simplest and most important types of relation constraints is the key
constraint. It tells us that in every instance of a certain relational schema the
tuples can be identified by their values for certain attributes.
Superkey
A superkey is written as a finite set of attribute names.
A superkey holds in a relation if:
and
there exist no two distinct tuples such that .
A superkey holds in a relation universe if it holds in all relations in .
Theorem: A superkey holds in a relation universe over if and only if
and holds in .
Candidate key
A superkey holds as a candidate key for a relation universe if it holds as
a superkey for and there is no proper subset of that also holds as a
superkey for .
Functional dependency
A functional dependency (FD for short) is written as for finite
sets of attribute names.
A functional dependency holds in a relation if:
and
tuples ,
A functional dependency holds in a relation universe if it holds in
all relations in .
Trivial functional dependency
A functional dependency is trivial under a header if it holds in all relation
universes over .
Theorem: An FD is trivial under a header if and only if
.
Closure
Armstrong's axioms: The closure of a set of FDs under a header , written
as , is the smallest superset of such that:
(reflexivity)
(transitivity) and
(augmentation)
Theorem: Armstrong's axioms are sound and complete; given a header
and a set of FDs that only contain subsets of , if and only if
holds in all relation universes over in which all FDs in hold.
Completion
The completion of a finite set of attributes under a finite set of FDs ,
written as , is the smallest superset of such that:
See also