Sobes.tech
Junior — Middle

What types of keys have you used in SQL tables and what are their features?

sobes.tech AI

Answer from AI

In SQL tables, the following key types are commonly used:

  • Primary Key (PRIMARY KEY) — uniquely identifies each record in the table. Cannot contain NULL or duplicate values.
  • Foreign Key (FOREIGN KEY) — references the primary key of another table, ensuring data integrity between tables.
  • Unique Key (UNIQUE KEY) — guarantees the uniqueness of values in a column or set of columns, allows NULLs (depending on the DBMS).
  • Composite Key (Composite Key) — a key consisting of multiple columns, used for unique identification of a record.

Features:

  • Primary key ensures uniqueness and does not allow NULL.
  • Foreign key maintains the relationship between tables and ensures referential integrity.
  • Unique key is similar to primary but can allow NULLs and is not necessarily the main identifier.

Example of creating a table with keys:

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_number VARCHAR(50) UNIQUE,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
What types of keys have you used in SQL tables and… - sobes.tech