ตัวอย่างง่ายๆ: มีตารางลูกค้า
create table Customers (
  id integer,
  constraint CustomersPK primary key (id)
)ข้อมูลอื่น ๆ ทั้งหมดในฐานข้อมูลควรเชื่อมโยงไปยัง a Customerดังนั้นเช่นOrdersจะมีลักษณะดังนี้:
create table Orders (
  id integer,
  customer integer,
  constraint OrdersPK primary key (customer, id),
  constraint OrdersFKCustomers foreign key (customer) references Customers (id)
)สมมติว่าตอนนี้มีตารางเชื่อมโยงไปยัง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)
)ฉันควรจะเพิ่มรหัสต่างประเทศแยกต่างหากจากItemsในCustomersหรือไม่
...
constraint ItemsFKCustomers foreign key (customer) references Customers (id)รูปภาพแทน: ฉันควรจะเพิ่มเส้นประ / FK หรือไม่?

แก้ไข:ฉันได้เพิ่มคำจำกัดความของคีย์หลักในตาราง ฉันต้องการซ้ำอีกครั้งในจุดที่ฉันทำไว้ข้างต้น: ฐานข้อมูลนั้นถูกปิดกั้นโดยลูกค้าโดยทั่วไปเป็นมาตรการความถูกต้อง / ความปลอดภัย ดังนั้นคีย์หลักทั้งหมดมีcustomerID


