Composite key as foreign key (sql)

后端 未结 2 606
长情又很酷
长情又很酷 2020-11-30 09:01

here are my two tables of concern:

CREATE TABLE IF NOT EXISTS `tutorial` (
  `beggingTime` time NOT NULL,
  `day` varchar(8) NOT NULL,
  `tutorId` int(3) NOT         


        
2条回答
  •  青春惊慌失措
    2020-11-30 09:26

    Per the mySQL documentation you should be able to set up a foreign key mapping to composites, which will require you to create the multiple columns.

    Add the columns and put this in your group table

    FOREIGN KEY (`beggingTime`,`day`,`tutorId`) 
        REFERENCES tutorial(`beggingTime`,`day`,`tutorId`)
    

    As Steven has alluded to in the below comments, you SHOULD try to re-architect this so that the tutorial table uses an actual primary key (even if it is just an identity surrogate key). This will allow for greater performance as SQL was built for this type of relationship, not composite.

提交回复
热议问题