0

I'm trying to represent inheritance (I know) in creating a database. I have it figured out, but I'm not sure if I need to represent the PK that my tables inherit when creating tables.

I have a FoodClass relation with the PK FoodClassID from which DonorFood and CharityFood inherit. Do I need to do anything other than just name the PK FoodClassID in each table?

Dub
  • 27
  • 6

1 Answers1

1

I expect you'll want a combination of PRIMARY KEY and FOREIGN KEY. Example:

CREATE TABLE DonorFood
(FoodClassID INT NOT NULL
 FOREIGN KEY REFERENCES FoodClass (FoodClassID)
 PRIMARY KEY,
... other columns    );
nvogel
  • 24,981
  • 1
  • 44
  • 82
  • Thank you. I wasn't sure what the semantics of it were. This will still make it a unique number in DonorFood, right? – Dub Nov 26 '17 at 14:14
  • Also, would I need to give the constraint a name in this context? – Dub Nov 26 '17 at 15:12
  • 1
    The PRIMARY KEY constraint means that FoodClassId must be unique. Best practice is to give names to all your constraints but I left the names out just for the sake of brevity. – nvogel Nov 26 '17 at 20:50