Records in one table should all be connected to records in another table - you shouldn't have diary entries for a company that isn't listed in the companies table.
Define a FOREIGN KEY on the diary table that REFERENCES the primary key in companies ref.
Or use ALTER TABLE ... ADD CONSTRAINT after the event ref.
In the definition of table diary we have
dy_company field REFERENCES companies(co_id) |
Field dy_company is known as a "foreign key" since it refers to the key field (co_id) in a foreign table (companies).
You can do this after the event using:
ALTER TABLE diary ADD CONSTRAINT diary_company_ref FOREIGN KEY (dy_company) REFERENCES companies(co_id); |
If there are values of dy_company that cannot be found in companies an error will be given.