Sobes.tech
Junior — Middle

Is it possible for a foreign key to point to a record in another table?

sobes.tech AI

Answer from AI

Yes, a foreign key in a database is specifically designed to point to a record in another table. It is a mechanism to ensure referential integrity of the data. For example, if you have an orders table with a customer_id foreign key, it will reference a record in the customers table.

Example in SQL:

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Thus, a foreign key links records between tables, ensuring data correctness and integrity.