Ainal Yaqin
Ainal Yaqin

Reputation: 31

Call foreign key in foreign key on PHP

I'm confused, because of this 3 tables, can i call a foreign key in another foreign key?

I got 3 tables, Component, PC, and Processor.

  ALTER TABLE `component` (
      `componentID` int(10) NOT NULL AUTO_INCREMENT,
      `Name_component` varchar(100) DEFAULT NULL,
      `Type` varchar(100) NOT NULL,
      `processorIDFK` int(10) NOT NULL,
       PRIMARY KEY (`componentID`),
       UNIQUE KEY `Processor` (`processorId`),
       CONSTRAINT processor_IDFK FOREIGN KEY (`processorIDFK`) REFERENCES processor(`processorID`)   
       );

 ALTER TABLE `PC` (
      `PCID` int(10) NOT NULL AUTO_INCREMENT,
      `name_PC` varchar(100) DEFAULT NULL,
      `componentIDFK` int(10) NOT NULL,
      PRIMARY KEY (`PCID`),
      UNIQUE KEY `component` (`componentId`),
      CONSTRAINT component_IDFK FOREIGN KEY (`componentIDFK`) REFERENCES component(`componentId`)
      );

 CREATE TABLE `processor` (
     `processorId` int(10) NOT NULL AUTO_INCREMENT,
     `processor_Name` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`processorId`)
      );

Can i call/use SELECT for processor_name in PC table? with this table relationship?

Upvotes: 1

Views: 221

Answers (1)

Hubert Sadecki
Hubert Sadecki

Reputation: 333

Yes, you can.

The thing you need to do is to join your tables.

SELECT processor_Name FROM PC t1 
INNER JOIN component t2 ON t1.componentIDFK=t2.componentID 
INNER JOIN processor t3 ON t3.processorId=t2.processorIDFK

Upvotes: 1

Related Questions