开发者

how do we make reference out of two tables? mysql

this is my tables

CREATE TABLE IF NOT EXISTS `carslibrary` (
  `CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `CarName` varchar(255) NOT NULL,
  PRIMARY KEY (`CarID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

CREATE TABLE IF NOT EXISTS `colorslibrary` (
  `ColorID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ColorName` varchar(255) NOT NULL,
  PRIMARY KEY (`ColorID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

i want to create another table with reference to CarID and ColorID

i made a table named facerecord

CREATE TABLE IF NOT EXISTS `facerecord` (
  `carslibrary_ID` int(10) NOT NULL,
  `colorslibrary_ID` int(11) NOT NULL,
  KEY `carslibrary_ID` (`carslibrary_ID`),
  KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

i am getting this error

Error creating foreign key on carslibrary_ID (check data types)

Error creating foreign key on colorslibrary_ID (check data types)

Error
ALTER TABLE  `facerecord` ADD FOREIGN KEY (  `carslibrary_ID` ) REFERENCES  `cars2`.`carslibrary` 开发者_JAVA百科(
`CarID`
) ON DELETE SET NULL ON UPDATE SET NULL ;

ALTER TABLE  `facerecord` ADD FOREIGN KEY (  `colorslibrary_ID` ) REFERENCES  `cars2`.`colorslibrary` (
`ColorID`
) ON DELETE SET NULL ON UPDATE SET NULL ;


You have mismatched data types in that your main tables use unsigned integer types, but your facerecord table uses plain signed INT(10), INT(11) as the data types.

Change facerecord to:

CREATE TABLE IF NOT EXISTS `facerecord` (
  `carslibrary_ID` int(10) unsigned NOT NULL,
  `colorslibrary_ID` int(11) unsigned NOT NULL,
  KEY `carslibrary_ID` (`carslibrary_ID`),
  KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CarId and ColorId are unsigned integers, so you should use:

CREATE TABLE IF NOT EXISTS `facerecord` (
  `carslibrary_ID` int(10) unsigned NOT NULL,
  `colorslibrary_ID` int(11) unsigned NOT NULL,
  KEY `carslibrary_ID` (`carslibrary_ID`),
  KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜