Middle
Explain the mechanism of connection through primary and foreign keys between the entity and contract tables.
sobes.tech AI
Answer from AI
In relational databases, the relationship between the "legal entity" and "contract" tables is usually implemented through primary and foreign keys.
- The "legal entity" table has a primary key (for example,
id) that uniquely identifies each legal entity. - The "contract" table has a foreign key (for example,
juridical_person_id) that references the primary key of the "legal entity" table.
This ensures data integrity: each contract is linked to an existing legal entity. When retrieving data, you can easily get all contracts of a specific legal entity through a JOIN on these keys.
Example:
-- Legal entity table
CREATE TABLE juridical_person (
id INT PRIMARY KEY,
name VARCHAR(255)
);
-- Contract table
CREATE TABLE contract (
id INT PRIMARY KEY,
juridical_person_id INT,
contract_date DATE,
FOREIGN KEY (juridical_person_id) REFERENCES juridical_person(id)
);