Prosty przykład: istnieje tabela klientów.
create table Customers (
id integer,
constraint CustomersPK primary key (id)
)
Wszystkie inne dane w bazie danych powinny zawierać linki do Customer, więc np. OrdersWygląda to tak:
create table Orders (
id integer,
customer integer,
constraint OrdersPK primary key (customer, id),
constraint OrdersFKCustomers foreign key (customer) references Customers (id)
)
Załóżmy, że teraz istnieje tabela prowadząca do Orders:
create table Items (
id integer,
customer integer,
order integer,
constraint ItemsPK primary key (customer, id),
constraint ItemsFKOrders foreign key (customer, order) references Orders (customer, id)
)
Czy powinienem dodać osobny klucz obcy od Itemsdo Customers?
...
constraint ItemsFKCustomers foreign key (customer) references Customers (id)
Zamiast tego obraz: czy powinienem dodać linię przerywaną / FK?

Edycja: Dodałem definicje klucza podstawowego do tabel. Chciałbym powtórzyć powyższy punkt: baza danych jest zasadniczo wyciszana przez klientów, jako środek poprawności / bezpieczeństwa. Dlatego wszystkie klucze podstawowe zawierają customeridentyfikator.


