Database Reference
In-Depth Information
Student Number and Class ID in Attendance translate into foreign keys on the Attend-
ance RDBMS table because of the constraints from Student to Attendance and from
Class to Attendance . This means that a Student Number cannot exist in Attendance
without having this Student Number first exist in Student . A Class ID cannot exist in At-
tendance without having this Class ID exist in Class .
However, in MongoDB, if Attendance is a collection with a Student Number reference
to Student and a Class reference to Class , there are no checks to ensure the Attendance
Student Numbers and Class IDs point back to valid values in the Student and Class col-
lections.
SECONDARY KEY
Sometimes there is a need to retrieve data rapidly from a table to answer a business query
or meet a certain response time. A secondary key is one or more data elements (if there is
more than one data element, it is called a composite secondary key) that are accessed fre-
quently and need to be retrieved quickly. A secondary key is also known as a non-unique
index or inversion entry (IE for short). A secondary key does not have to be unique, stable,
nor always contain a value. For example, we can add an IE to Student Last Name in Stu-
dent to allow for quick retrieval whenever any queries require Student Last Name :
Student Last Name is not unique, as there can be two Jones ; it is not stable and can change
over time; and sometimes we may not know someone's last name, so it can be empty.
MongoDB Non-Unique Index = RDBMS Secondary Key
The concept of a secondary key in relational databases is equivalent to the concept of a
non-unique index in MongoDB. Indexes are added to improve retrieval performance. There
are always tradeoffs with indexes, though. Balance retrieval speed with space (each index
requires at least 8 kilobytes) and volatility (if the underlying data changes, the index has to
change, too).
A query that does not use an index is called a “table scan” in an RDBMS and a “collection
scan” in MongoDB. This means that the server has to look through all of the data, starting
at the beginning, until all of the query results are found. This process is basically what
Search WWH ::




Custom Search