I have two tables in a database, Library and Book. I'm trying to introduce a foreign key to Book's table in reference to the Library's name. Two tables with sample data
In essence, the "library" key under book should be a foreign key referencing the 'Library' Table's name attribute. I've tried doing this both during table creation and as an ALTER command with no success. Please take a look and let me know if you spot anything wrong.
TABLE CREATION COMMANDS USED: PlainText Code
CREATE TABLE`Library` (
`name` VARCHAR(20) NOT NULL,
`street` VARCHAR(50) NULL,
`zip_Code` CHAR(5) NULL,
`state` CHAR(2) NULL,
PRIMARY KEY (`name`))
ENGINE = InnoDB;
CREATE TABLE `Book` (
`isbn` CHAR(13) NOT NULL,
`copy_number` TINYINT NOT NULL,
`library` VARCHAR(20) NOT NULL,
PRIMARY KEY (`isbn`, `copy_number`, `library`),
INDEX `library_idx` (`library` ASC) VISIBLE,
CONSTRAINT `Book`
FOREIGN KEY (`library`)
REFERENCES `Library` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
ERROR 1064 (42000): 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 '
CONSTRAINT `library`
FOREIGN KEY (`library`)
REFERENCES `Library` ...' at line 6
ALTER USED: ALTER TABLE Book ADD CONSTRAINT FOREIGN KEY (library) REFERENCES Library(name);