开发者

php my admin one to many relationship

I'm using mySQL and PHPMyAdmin.

I have the floowing three tables and fields in my DB;

T_users;
F_uID - varchar - (PK)
F_Forename - text
F_Surname - text

T_candidates;
F_bookingRef - int - auto increment - (PK)
F_sessionID - int
F_uID - varchar

T_TrainingSessions;
F_sessionID - int - auto increment - (PK)
F_sessionDesc - text
F_sessionDate - date

I would like to create a one-to-many relationship between T_Users.F_uID and T_candidates.F_Uid.

I would also like to create a one-to-many relationship between T_Tra开发者_JS百科iningSessions.F_sessionID and T_candidates.F_sessionID.

However, when I click on 'relation view' in any of the table structure sections in PHPMyAdmin I can only select fields that are primary keys from the drop down lists!

From my reading, i think this may have something to do with indexes, however I have no knowledge or experience of setting indexes.

Any help would be very greatly appreciated.

:)

Tim


You can make foreign keys from fields that are primary keys or fields that have unique values.

If you can't create the relations

T_Users.F_uID<---->>T_candidates.F_Uid
T_TrainingSessions.F_sessionID<---->>T_candidates.F_sessionID 
(one<---->>many)

maybe you are trying to create the inverse relations.

try this:

CREATE TABLE T_users (
  F_uID VARCHAR(SIZE) NOT NULL,
  F_Forename TEXT NOT NULL,
  F_Surname TEXT NOT NULL,
  PRIMARY KEY(F_uID)
)
TYPE=InnoDB;


CREATE TABLE T_TrainingSessions (
  F_sessionID INT AUTO_INCREMENT NOT NULL,
  F_sessionDesc TEXT NOT NULL,
  F_sessionDate DATE NOT NULL,
  PRIMARY KEY(F_sessionID)
)
TYPE=InnoDB;

CREATE TABLE T_candidates (
  F_bookingRef INT AUTO_INCREMENT NOT NULL,
  F_sessionID INT NOT NULL,
  F_uID VARCHAR(SIZE) NOT NULL,
  PRIMARY KEY(F_bookingRef)
  INDEX FKIndex1(F_sessionID),
  INDEX FKIndex2(F_uID),
  FOREIGN KEY(F_uID)
    REFERENCES T_users(F_uID)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION
  FOREIGN KEY(F_sessionID)
    REFERENCES T_TrainingSessions(F_sessionID)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION
)
TYPE=InnoDB;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜