Edvinas Liutvaitis
Edvinas Liutvaitis

Reputation: 575

foreign key not working properly

I am trying to establish a relationship between two tables using phpMyAdmin. But I am having some trouble of doing this.

I have the foreign key set up but for some reason it dos not add the id of the other table. I am very new to MySQL and can't figure this one out.

Here is my table structure:

tblanswers

CREATE TABLE IF NOT EXISTS `tblanswers` (
  `answerid` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL,
  `questionid` int(11) NOT NULL,
  `answerA` varchar(255) NOT NULL,
  `answerB` varchar(255) NOT NULL,
  `answerC` varchar(255) NOT NULL,
  `comment` varchar(255) NOT NULL,
  PRIMARY KEY (`answerid`),
  UNIQUE KEY `cid` (`cid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=210 ;

credentials

CREATE TABLE IF NOT EXISTS `credentials` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `second_name` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `cid` (`cid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=106 ;

I am very unclear how to set foreign keys and how they are connect the tables I looked up couple of tutorials but they did not really explain much in detail.

Upvotes: 0

Views: 91

Answers (1)

John Woo
John Woo

Reputation: 263693

add this on table tblanswers

CONSTRAINT tb_FK FOREIGN KEY (cid) REFERENCES credentials (cid)

Upvotes: 1

Related Questions