Composite key

Summary

In database design, a composite key is a candidate key that consists of two or more attributes,[1][2][3] (table columns) that together uniquely identify an entity occurrence (table row).

A compound key is a composite key for which each attribute that makes up the key is a foreign key in its own right.[4][5]

Advantages

edit

Composite keys have advantages similar to that of a natural key as it is often composed of multiple natural key attributes.

Storage

edit

Composite keys use less disk space as compared to defining a surrogate key column, this is because the composite key already exists as attributes in the table and does not need to be defined in the table just for the purpose of unique identification. This simplifies the table and also saves space.

Easier to implement and use

edit

Composite keys are easy to implement in a database schema as their component parts are already named items in the database. When they are also natural keys, they are often intuitive for real world scenarios. They are often used when a non-composite key does not always uniquely identify a record. For example, a personal name may often, but not always, be unique in a given database, and some other field such as date of birth may be added to make uniqueness much more probable.

Disadvantages

edit

Requirement changes

edit

The business requirements and rules can change which can change the format of certain real world entities. Composite keys are formed of multiple natural keys which are related to the real world and with the change of their format in the real world, their format in the database will also be changed. This is inconvenient as the number of attributes of composite key will change and all the foreign keys would need to be updated.

Complexity and storage

edit

A composite key consists of multiple attributes and the composite key will be referenced in multiple tables as the foreign key, this uses a lot of disk space as multiple columns are being stored as the foreign key instead of just possibly one. This makes the schema complex and the queries become more CPU expensive as for every join the DBMS will need to compare three attributes instead of just possibly one in case of a single natural key.

Example

edit

An example is an entity that represents the modules each student is attending at University. The entity has a studentID and a moduleCode as its primary key. Each of the attributes that makes up the primary key is a simple key because each represents a unique reference when identifying a student in one instance and a module in the other, so this key is a compound key.

In contrast, using the same example, imagine we identified a student by their firstName + lastName (assuming that people must have different names). In a table representing students our primary key would now be firstName + lastName. Because students can have the same firstName or the same lastName these attributes are not simple keys. The primary key firstName + lastName for students is a composite key.

See also

edit

References

edit
  1. ^ Connolly, Thomas M.; Begg, Carolyn E. (2015). "12.3.4 Keys". Database systems: a practical approach to design, implementation, and management (6., global ed.). Boston Columbus Indianapolis: Pearson. p. 416. ISBN 978-1-292-06118-4.
  2. ^ Elmasri, Ramez; Navathe, Sham (2017). "17.4 Indexes on Multiple Keys". Fundamentals of database systems (Seventh, global ed.). Boston Columbus Indianapolis New York San Francisco Hoboken Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo: Pearson. p. 661. ISBN 978-1-292-09761-9.
  3. ^ Coronel, Carlos; Morris, Steven (2015). "Glossary". Database systems: design, implementation, and management (12e ed.). Cengage Learning. p. 770. ISBN 978-1-305-62748-2.
  4. ^ Duncan, Joyce; Rackley, Lesley; Walker, Alexandria (1995), Duncan, Joyce; Rackley, Lesley; Walker, Alexandria (eds.), "Step 340 — Enhance Required Logical Data Model", SSADM in Practice: A Version 4 Text, London: Macmillan Education UK, pp. 61–70, doi:10.1007/978-1-349-10341-6_6, ISBN 978-1-349-10341-6, retrieved 2024-10-25
  5. ^ Sikora, Z. M. (1997), Sikora, Z. M. (ed.), "Implementing a Design", Oracle Database Principles, London: Macmillan Education UK, pp. 74–84, doi:10.1007/978-1-349-14693-2_7, ISBN 978-1-349-14693-2, retrieved 2024-10-25
edit
  • Composite Inverse Functional Properties: for an equivalent notion in the Semantic Web
  • Relation Database terms of reference, Keys: An overview of the different types of keys in an RDBMS
  • Different types of keys in a database: An overview of all the types of keys that are used in an RDBMS