我正在关注一个 PHP 和 MySQL 教程。此时,我必须创建一个如下所示的数据库:
我手动创建了数据库,但我不明白滚动表的意义。例如,假设我想添加一部电影:既然不允许,我该怎么做?示例错误:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`test`.`movie`, CONSTRAINT `movie_ibfk_1` FOREIGN KEY (`movieCode`) REFERENCES `roll` (`movieCode`) ON DELETE CASCADE)
我个人会做类似的事情:
表艺术家:artistId , firstname, lastname, nationality, dateOfBirth, otherInfo 表电影:movieCode, title, image, category, description, artistId
作为相关外键的粗体。但是,我不明白在那里使用卷表的概念。有人可以为我澄清这一点,因为我想像教程假装教的那样做吗?
到目前为止我拥有的数据库架构:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `artist` (
`artistId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`firstName` char(30) NOT NULL,
`lastName` char(30) NOT NULL,
`dateOfBirth` date NOT NULL,
`nationality` char(30) NOT NULL,
`otherInfo` text NOT NULL,
PRIMARY KEY (`artistId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `movie` (
`movieCode` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` char(30) NOT NULL,
`image` varchar(50) NOT NULL,
`category` char(50) NOT NULL,
`movieDesc` text NOT NULL,
PRIMARY KEY (`movieCode`),
UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `roll` (
`movieCode` int(10) unsigned NOT NULL,
`artistId` int(10) unsigned NOT NULL,
PRIMARY KEY (`movieCode`,`artistId`),
KEY `artistId` (`artistId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `user` (
`userId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`firstName` char(30) NOT NULL,
`lastName` char(30) NOT NULL,
`username` char(30) NOT NULL,
`password` char(20) NOT NULL,
`usertype` int(1) unsigned NOT NULL,
PRIMARY KEY (`userId`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `movie`
ADD CONSTRAINT `movie_ibfk_1` FOREIGN KEY (`movieCode`) REFERENCES `roll` (`movieCode`) ON DELETE CASCADE;
ALTER TABLE `roll`
ADD CONSTRAINT `roll_ibfk_1` FOREIGN KEY (`artistId`) REFERENCES `artist` (`artistId`) ON DELETE CASCADE;