Content deleted Content added
Move the Alternatives section to the top-level, instead of nesting it in the Overview section |
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
||
(43 intermediate revisions by 21 users not shown) | |||
Line 1:
{{short description|Database model}}
The '''relational model''' ('''RM''') is an approach to managing [[data]] using a [[Structure (mathematical logic)|structure]] and language consistent with [[first-order logic|first-order predicate logic]], first described in 1969 by English computer scientist [[Edgar F. Codd]],<ref>{{Citation | title = Derivability, Redundancy, and Consistency of Relations Stored in Large Data Banks | first = E.F | last = Codd | publisher = IBM | series = Research Report | year = 1969}}.</ref><ref name="codd1970">{{cite journal |last= Codd |first= E.F |year= 1970 |title= A Relational Model of Data for Large Shared Data Banks |series= Classics
The purpose of the relational model is to provide a [[Declarative programming|declarative]] method for specifying data and queries: users directly state what information the database contains and what information they want from it, and let the [[database management system|database management system software]] take care of describing data structures for storing the data and retrieval procedures for answering queries.
Most relational databases use the [[SQL]] data definition and query language; these systems implement what can be regarded as an engineering approximation to the relational model. A ''[[Table (database)|table]]'' in a SQL [[database schema]] corresponds to a predicate variable; the contents of a table to a relation; key constraints, other constraints, and SQL queries correspond to predicates. However, SQL databases [[#SQL and the relational model|deviate from the relational model in many details]], and Codd fiercely argued against deviations that compromise the original principles.<ref>{{Citation | first = E. F | last = Codd | title = The Relational Model for Database Management | publisher = Addison-Wesley | year = 1990 | isbn = 978-0-201-14192-4 | pages=371–388}}.</ref>
==
The relational model was developed by [[Edgar F. Codd]] as a general model of data, and subsequently promoted by [[Christopher J. Date|Chris Date]] and [[Hugh Darwen]] among others. In their 1995 ''The Third Manifesto'', Date and Darwen try to demonstrate how the relational model can accommodate certain "desired" [[Object-oriented programming|object-oriented]] features.<ref>{{Cite web |title=Did Date and Darwen's "Third Manifesto" have a lasting impact? |url=https://cs.stackexchange.com/questions/99350/did-date-and-darwens-third-manifesto-have-a-lasting-impact |access-date=2024-08-03 |website=Computer Science Stack Exchange |language=en}}</ref>▼
=== Extensions ===▼
Some years after publication of his 1970 model, Codd proposed a [[three-valued logic]] (True, False, Missing/[[Null (SQL)|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.<ref>{{cite book| first =Christopher J. | last = Date|title=Date on Database: Writings 2000–2006|date= 2006 |publisher= Apress|isbn= 978-1-59059-746-0|pages=329–41|chapter= 18. Why Three- and Four-Valued Logic Don't Work}}</ref>
== Conceptualization ==
=== Basic concepts ===
[[File:Relational database terms.svg|thumb|A relation with 5 attributes (its degree) and 4 tuples (its cardinality) can be visualized as a table with 5 columns and 4 rows. However, unlike rows and columns in a table, a relation's attributes and tuples are unordered.]]
A ''relation'' consists of a ''heading'' and a ''body''. The heading defines a [[Set (mathematics)|set]] of ''attributes'', each with a ''name'' and ''data type'' (sometimes called a ''___domain''). The number of attributes in this set is the relation's ''degree'' or ''[[arity]]''. The body is a set of ''tuples''. A tuple is a collection of ''n'' ''values'', where ''n'' is the relation's degree, and each value in the tuple corresponds to a unique attribute.<ref>{{Cite web |date=2023-02-12 |title=Tuple in DBMS |url=https://www.geeksforgeeks.org/tuple-in-dbms/ |access-date=2024-08-03 |website=GeeksforGeeks |language=en-US}}</ref> The number of tuples in this set is the relation's ''[[cardinality]]''.<ref name="professionals"/>{{rp|17–22}}
== History ==▼
▲The relational model was developed by [[Edgar F. Codd]] as a general model of data, and subsequently promoted by [[Christopher J. Date|Chris Date]] and [[Hugh Darwen]] among others. In their 1995 ''The Third Manifesto'', Date and Darwen try to demonstrate how the relational model can accommodate certain "desired" [[object-oriented]] features.
▲=== Extensions ===
▲Some years after publication of his 1970 model, Codd proposed a [[three-valued logic]] (True, False, Missing/[[Null (SQL)|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.<ref>{{cite book| first =Christopher J. | last = Date|title=Date on Database: Writings 2000–2006|date= 2006 |publisher= Apress|isbn= 978-1-59059-746-0|pages=329–41|chapter= 18. Why Three- and Four-Valued Logic Don't Work}}</ref>
Relations are represented by ''relational [[Variable (computer science)|variables]]'' or ''relvars'', which can be reassigned.<ref name="professionals"/>{{rp|22–24}} A ''[[database]]'' is a collection of relvars.<ref name="professionals"/>{{rp|112–113}}
In this model, databases follow the ''Information Principle'': At any given time, all [[Information theory|information]] in the database is represented solely by values within tuples, corresponding to attributes, in relations identified by relvars.<ref name="professionals"/>{{rp|111}}
=== Constraints ===
A database may define arbitrary [[boolean expressions]] as [[constraint (database)|constraints]]. If all constraints evaluate as ''true'', the database is ''consistent''; otherwise, it is ''inconsistent''. If a change to a database's relvars would leave the database in an inconsistent state, that change is illegal and must not succeed.<ref name="professionals"/>{{rp|91}}
In general, constraints are expressed using relational comparison operators, of which just one, "is subset of" (⊆), is theoretically sufficient.{{Citation needed|reason=This should be explained, because it is interesting enough to read more about it|date=August 2010}}
Two special cases of constraints are expressed as ''keys'' and ''foreign keys'':
==== Keys ====
A ''candidate key'', or simply a ''key'', is the smallest [[subset]] of attributes guaranteed to uniquely differentiate each tuple in a relation. Since each tuple in a relation must be unique, every relation necessarily has a key, which may be its complete set of attributes. A relation may have multiple keys, as there may be multiple ways to uniquely differentiate each tuple.<ref name="professionals"/>{{rp|31–33}}
An attribute may be unique across tuples without being a key. For example, a relation describing a company's employees may have two attributes: ID and Name. Even if no employees currently share a name, if it is possible to eventually hire a new employee with the same name as a current employee, the attribute subset {Name} is not a key. Conversely, if the subset {ID} is a key, this means not only that no employees ''currently'' share an ID, but that no employees ''will ever'' share an ID.<ref name="professionals"/>{{rp|31–33}}
==== Foreign keys ====
{{main|Foreign key}}
A ''foreign key'' is a subset of attributes ''A'' in a relation ''R<sub>1</sub>'' that corresponds with a key of another relation ''R<sub>2</sub>'', with the property that the [[Projection (relational algebra)|projection]] of ''R<sub>1</sub>'' on ''A'' is a subset of the projection of ''R<sub>2</sub>'' on ''A''. In other words, if a tuple in ''R<sub>1</sub>'' contains values for a foreign key, there must be a corresponding tuple in ''R<sub>2</sub>'' containing the same values for the corresponding key.<ref name="professionals"/>{{rp|34}}
=== Relational operations ===
Line 50 ⟶ 47:
Often, data from multiple tables are combined into one, by doing a [[Join (SQL)|join]]. Conceptually, this is done by taking all possible combinations of rows (the [[Cartesian product]]), and then filtering out everything except the answer.
There are a number of relational operations in addition to join. These include project (the process of eliminating some of the columns), restrict (the process of eliminating some of the rows), union (a way of combining two tables with similar structures), difference (that lists the rows in one table that are not found in the other), intersect (that lists the rows found in both tables), and product (mentioned above, which combines each row of one table with each row of the other). Depending on which other sources you consult, there are a number of other operators – many of which can be defined in terms of those listed above. These include semi-join, outer operators such as outer join and outer union, and various forms of division. Then there are operators to rename columns, and summarizing or aggregating operators, and if you permit [[relation (database)|relation]] values as attributes (relation-valued attribute), then operators such as group and ungroup.
The flexibility of relational databases allows programmers to write queries that were not anticipated by the database designers. As a result, relational databases can be used by multiple applications in ways the original designers did not foresee, which is especially important for databases that might be used for a long time (perhaps several decades). This has made the idea and implementation of relational databases very popular with businesses.
Line 57 ⟶ 54:
{{Main|Database normalization}}
[[Relation (database)|Relation]]s are classified based upon the types of anomalies to which they're vulnerable. A database that is in the [[first normal form]] is vulnerable to all types of anomalies, while a database that is 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.<ref name="Normalization">David M. Kroenke, ''Database Processing: Fundamentals, Design, and Implementation'' (1997), Prentice-Hall, Inc., pages 130–144</ref>
== Logical interpretation ==
The relational model is a [[formal system]]. A relation's attributes define a set of [[logic]]al [[propositions]]. Each proposition can be expressed as a tuple. The body of a relation is a subset of these tuples, representing which propositions are true. Constraints represent additional propositions which must also be true. [[Relational algebra]] is a set of logical rules that can [[Validity (logic)|validly]] [[inference|infer]] conclusions from these propositions.<ref name="professionals"/>{{rp|95–101}}
The definition of a ''tuple'' allows for a unique empty tuple with no values, corresponding to the [[empty set]] of attributes. If a relation has a degree of 0 (i.e. its heading contains no attributes), it may have either a cardinality of 0 (a body containing no tuples) or a cardinality of 1 (a body containing the single empty tuple). These relations represent [[Boolean ___domain|Boolean]] [[truth values]]. The relation with degree 0 and cardinality 0 is ''False'', while the relation with degree 0 and cardinality 1 is ''True''.<ref name="professionals"/>{{rp|221–223}}
=== Example ===
If a relation of Employees contains the attributes ''{Name, ID}'', then the tuple ''{Alice, 1}'' represents the proposition: "There exists an employee named ''Alice'' with ID ''1''". This proposition may be true or false. If this tuple exists in the relation's body, the proposition is true (there is such an employee). If this tuple is not in the relation's body, the proposition is false (there is no such employee).<ref name="professionals"/>{{rp|96–97}}
Furthermore, if ''{ID}'' is a key, then a relation containing the tuples ''{Alice, 1}'' and ''{Bob, 1}'' would represent the following [[contradiction]]:
# There exists an employee with the name ''Alice'' and the ID ''1''.
# There exists an employee with the name ''Bob'' and the ID ''1''.
# There do not exist multiple employees with the same ID.
Under the [[principle of explosion]], this contradiction would allow the system to prove that any arbitrary proposition is true. The database must enforce the key constraint to prevent this.<ref name="professionals"/>{{rp|104}}
== Examples ==
Line 94 ⟶ 108:
Now, the Order relvar has a ''[[One-to-many (data model)|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 '''<u>Order ID</u>''' in the Order relation equals the '''<u>Order ID</u>''' in OrderInvoice, and where '''<u>Invoice ID</u>''' in OrderInvoice equals the '''<u>Invoice ID</u>''' in Invoice.
== Application to relational databases ==
A '''[[data ___domain|data type]]'''
'''Attributes''' are commonly represented as '''[[Column (database)|
A database '''[[relvar]]''' (relation variable) is commonly known as a '''base table'''. The heading of its assigned value at any time is as specified in the table declaration and its body is that most recently assigned to it by an '''update operator''' (typically, INSERT, UPDATE, or DELETE). The heading and body of the table resulting from evaluating a query are determined by the definitions of the operators used in that query.
=== SQL and the relational model ===
SQL, initially pushed as the [[standardization|standard]] language for [[relational database]]s, deviates from the relational model in several places. The current [[International Organization for Standardization|ISO]] SQL standard doesn't mention the relational model or use relational terms or concepts.{{
According to the relational model, a Relation's attributes and tuples are [[Set (mathematics)|mathematical sets]], meaning they are unordered and unique. In a SQL table, neither rows nor columns are proper sets. A table may contain both duplicate rows and duplicate columns, and a table's columns are explicitly ordered. SQL uses a [[Null (SQL)|Null]] value to indicate missing data, which has no analog in the relational model. Because a row can represent unknown information, SQL does not adhere to the relational model's ''Information Principle''.<ref name="professionals">{{cite book |last1=Date |first1=Chris J. |title=Relational Theory for Computer Professionals: What Relational Databases are Really All About |date=2013 |publisher=O'Reilly Media |___location=Sebastopol, Calif |isbn=978-1-449-36943-9 |edition=1.}}</ref>{{rp|
== Set-theoretic formulation ==
Line 201 ⟶ 213:
== Alternatives ==
Other [[Database model|models]] include the [[Hierarchical database model|hierarchical model]] and [[network model]]. Some [[system]]s using these older architectures are still in use today in [[data center]]s with high data volume needs, or where existing systems are so complex and abstract that it would be cost-prohibitive to migrate to systems employing the relational model. Also of note are newer [[object database|object-oriented databases]].
''Datalog'' is a database definition language, which combines a relational view of data, as in the relational model, with a logical view, as in [[logic programming]]. Whereas relational databases use a relational calculus or relational algebra, with [[Relational database#Relational operations|relational operations]], such as ''union'', ''intersection'', ''set difference'' and ''cartesian product'' to specify queries, Datalog uses logical connectives, such as ''if'', ''or'', ''and'' and ''not'' to define relations as part of the database itself.
In contrast with the relational model, which cannot express recursive queries without introducing a least-fixed-point operator,<ref>Aho, A.V. and Ullman, J.D., 1979, January. Universality of data retrieval languages. In Proceedings of the 6th ACM SIGACT-SIGPLAN symposium on Principles of programming languages (pp. 110-119).</ref> recursive relations can be defined in Datalog, without introducing any new logical connectives or operators.
== See also ==
Line 215 ⟶ 231:
* [[Tuple-versioning]]
{{div col end}}
{{reflist|group=nb}}
== References ==
Line 227 ⟶ 246:
* {{Citation | publisher = Handle | title = Feasibility of a set-theoretic data structure: a general structure based on a reconstituted definition of relation | last = Childs | year = 1968 | type = research| hdl = 2027.42/4164 }} cited in Codd's 1970 paper.
* {{Citation | last = Darwen | first = Hugh | url = http://www.thethirdmanifesto.com/ | title = The Third Manifesto (TTM)}}.
* {{Citation | contribution-url = http://c2.com/cgi/wiki?RelationalModel | contribution = Relational Model | title = C2}}.
* {{Citation | url = http://blogs.sun.com/bblfish/entry/why_binary_relations_beat_tuples | title = Binary relations and tuples compared with respect to the semantic web | publisher = Sun | type = [[World Wide Web]] log}}.
Line 239 ⟶ 257:
[[Category:Articles with example pseudocode]]
[[Category:Programming paradigms]]
[[Category:Database management systems]]
|