Sobes.tech
Junior — Middle

What types of keys are used in databases and what are they for?

sobes.tech AI

Answer from AI

Databases use several types of keys, each serving its own function:

  • Primary Key — uniquely identifies each record in a table. Primary key values cannot be repeated and must not be NULL.

  • Foreign Key — a reference to the primary key of another table, used to establish relationships between tables and ensure data integrity.

  • Unique Key — guarantees the uniqueness of values in a column or set of columns, but unlike the primary key, it can allow NULLs (depending on the DBMS).

  • Surrogate Key — an artificial key, usually a numeric identifier, that does not carry business meaning but is used for unique record identification.

  • Composite Key — a key consisting of multiple columns that together uniquely identify a record.

Each of these keys helps organize data, ensure its integrity, and optimize search and relationships between tables.