Let's say i have a table named borrows like this one:
CREATE TABLE IF NOT EXISTS `borrows`
( `memberID` int(11) NOT NULL ,
`ISBN` int(11) NOT NULL ,
`CopyNr` int(11) NOT NULL ,
`date_of_borrowing` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_of_reminder` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`memberID`,`ISBN`,`CopyNr`,`date_of_borrowing`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1
I want this table to have (ISBN,CopyNr) as a foreign key to an other table named copies.What i tried is this:
ALTER TABLE `borrows` ADD FOREIGN KEY (`ISBN`,`copyNr`)
REFERENCES `copies`(`copyNr`) ON DELETE RESTRICT ON UPDATE RESTRICT;
This however gives me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(`ISBN`),(`copyNr`)) REFERENCES `copies`(`copyNr`) ON DELETE RESTRICT ON UPDATE ' at line 1
Should i create another column that would contain tuples of(ISBN,CopyNr) first?If yes,how can this happen?If not,how can i solve this?
UPDATE:This is the code for copies:
CREATE TABLE IF NOT EXISTS `copies`
( `copyNr` int(11) NOT NULL AUTO_INCREMENT,
`shelf` text NOT NULL,
`ISBN` int(11) NOT NULL,
PRIMARY KEY (`copyNr`,`ISBN`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1