Search code examples
sql-serverrelational-databasedatabase-scripts

Do inherited keys need some kind of creation in SQL creation scripts?


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?


Solution

  • 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    );