Compound key
   HOME

TheInfoList



OR:

In
database design Database design is the organization of data according to a database model. The designer determines what data must be stored and how the data elements interrelate. With this information, they can begin to fit the data to the database model.Teorey, T ...
, a composite key is a candidate key that consists of two or more attributes, (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 Foreign may refer to: Government * Foreign policy, how a country interacts with other countries * Ministry of Foreign Affairs, in many countries ** Foreign Office, a department of the UK government ** Foreign office and foreign minister * United S ...
key in its own right.


Advantages

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


Storage

Composite keys use less disk space as compared to defining a
surrogate key A surrogate key (or synthetic key, pseudokey, entity identifier, factless key, or technical key) in a database is a unique identifier for either an ''entity'' in the modeled world or an ''object'' in the database. The surrogate key is ''not'' deri ...
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

Composite keys are easy to implement in a
database schema The database schema is the structure of a database described in a formal language supported typically by a relational database management system (RDBMS). The term "wikt:schema, schema" refers to the organization of data as a blueprint of how the ...
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


Requirement changes

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

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

An example is an entity that represents the modules each student is attending at University. The entity has a and a as its
primary key In the relational model of databases, a primary key is a designated attribute (column) that can reliably identify and distinguish between each individual record in a table. The database creator can choose an existing unique attribute or combinati ...
. 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 + (assuming that people must have different names). In a table representing students our primary key would now be + . Because students can have the same or the same these attributes are not simple keys. The primary key + for students is a composite key.


See also

*
Relational database A relational database (RDB) is a database based on the relational model of data, as proposed by E. F. Codd in 1970. A Relational Database Management System (RDBMS) is a type of database management system that stores data in a structured for ...
* Alternate key * Candidate key *
Foreign key A foreign key is a set of attributes in a table that refers to the primary key of another table, linking these two tables. In the context of relational databases, a foreign key is subject to an inclusion dependency constraint that the tuples ...
*
Primary key In the relational model of databases, a primary key is a designated attribute (column) that can reliably identify and distinguish between each individual record in a table. The database creator can choose an existing unique attribute or combinati ...
* Unique key * Superkey


References

{{reflist


External links


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 Data modeling ru:Составной ключ