首先,我想道歉,以防这是一个已知且已回答的问题,但英语不是我的母语,我什至不知道如何搜索这样的问题。所以这里。
我有一个名为电影的表。此表包含有关电影的信息。这部电影的导演和编剧也在我存储的信息中。但导演和作家都是多值属性。由于它们是多值的,它们应该存储在新表中,但是这些表将与电影表有多对多的关系,所以我制作了四个新表。
导演 -> 导演 -> 电影编剧 -> 编剧 -> 电影
[Director] 和 [writer] 持有名字 lastnames 和一个 Id 作为关键属性。[directed], [write] 持有 director 和 writer 表中的 id 键和 movie 表中的键。
当我询问特定电影(有 3 位导演和 2 位编剧)的导演和编剧姓名时,我得到的结果是::
D.Name D.Lastname W.Name W.Lastname
----------------------------------------------
Jan Kounen Stanley Kubric
Albert Hughes Stanley Kubric
Len Wiseman Stanley Kubric
Jan Kounen Alan Moore
Albert Hughes Alan Moore
Len Wiseman Alan Moore
如果我使用联合查询,我只能得到我想要的五个名字,但我无法理解谁是导演谁是作家。
Jan Kounen
Albert Hughes
Len Wiseman
Stanley Kubric
Alan Moore
但都被列为董事。
所以我的问题是,我的设计有缺陷吗?我的查询错了吗?有没有办法让导演和作家只用一个查询而不用多重响应?
先感谢您。
编辑::::
谢谢大家的回复。
我试图为关系上传一张图片,但它不允许我。所以这是相关表格的代码
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`directed_by` (`Direc_id` INT(11) NOT NULL ,
`Db_Title` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`Direc_id`, `Db_Title`) ,
INDEX `fk_Director_has_Movie_Movie1_idx` (`Db_Title` ASC) ,
INDEX `fk_Director_has_Movie_Director1_idx` (`Direc_id` ASC) ,
CONSTRAINT `fk_Director_has_Movie_Director1`
FOREIGN KEY (`Direc_id` )
REFERENCES `Red_Apple_storeDB`.`director` (`D_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Director_has_Movie_Movie1`
FOREIGN KEY (`Db_Title` )
REFERENCES `Red_Apple_storeDB`.`movie` (`MV_Title` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`director` (`D_Name` VARCHAR(50) NOT NULL ,
`D_lastname` VARCHAR(45) NOT NULL ,
`D_id` INT(11) NOT NULL ,
PRIMARY KEY (`D_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`movie` (`MV_Title` VARCHAR(50) NOT NULL ,
`MV_Release_Date` DATE NOT NULL ,
`MV_Cost` BIGINT(20) NOT NULL ,
`St_Name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`MV_Title`) ,
INDEX `fk_Movie_Studio1_idx` (`St_Name` ASC) ,
CONSTRAINT `fk_Movie_Studio1`
FOREIGN KEY (`St_Name` )
REFERENCES `Red_Apple_storeDB`.`studio` (`ST_Name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`writen_by` (`Writ_id` VARCHAR(45) NOT NULL ,
`Wb_Title` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`Writ_id`, `Wb_Title`) ,
INDEX `fk_Writer_has_movie_movie1_idx` (`Wb_Title` ASC) ,
INDEX `fk_Writer_has_movie_Writer1_idx` (`Writ_id` ASC) ,
CONSTRAINT `fk_Writer_has_movie_Writer1`
FOREIGN KEY (`Writ_id` )
REFERENCES `Red_Apple_storeDB`.`writer` (`W_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Writer_has_movie_movie1`
FOREIGN KEY (`Wb_Title` )
REFERENCES `Red_Apple_storeDB`.`movie` (`MV_Title` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`writer` (`W_Name` VARCHAR(50) NOT NULL ,
`W_Lastname` VARCHAR(45) NULL DEFAULT NULL ,
`W_id` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`W_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
我要求的一部电影,有 3 位导演和 2 位编剧。
我的查询::
select d_name, d_lastname,d_id
from director, movie,directed_by
where mv_title = 'from hell'
and mv_title = db_title
and direc_id = d_id
union all
select w_name, w_lastname,w_id
from movie, writer, writen_by
where mv_title = 'from hell'
and mv_title = wb_title
and writ_id = w_id