简体   繁体   English

SQL代替触发器和更新

[英]SQL instead of trigger and update

I have these tables: 我有这些表:

  • Movie ( mID, title, year, director ) 电影(mID,标题,年份,导演)
  • Reviewer ( rID, name ) 审稿人(rID,姓名)
  • Rating ( rID, mID, stars, ratingDate ) 评分(rID,mID,星级,ratingDate)

and some views: 和一些意见:

  • View LateRating contains movie ratings after January 20, 2011. The view contains the movie ID, movie title, number of stars, and rating date. 视图LateRating包含2011年1月20日之后的电影分级。该视图包含电影ID,电影标题,星星数量和分级日期。

     create view LateRating as select distinct R.mID, title, stars, ratingDate from Rating R, Movie M where R.mID = M.mID and ratingDate > '2011-01-20' 
  • View HighlyRated contains movies with at least one rating above 3 stars. View HighlyRated包含至少3个星以上的评级的电影。 The view contains the movie ID and movie title. 该视图包含电影ID和电影标题。

     create view HighlyRated as select mID, title from Movie where mID in (select mID from Rating where stars > 3) 
  • View NoRating contains movies with no ratings in the database. View NoRating包含数据库中没有评级的电影。 The view contains the movie ID and movie title. 该视图包含电影ID和电影标题。

     create view NoRating as select mID, title from Movie where mID not in (select mID from Rating) 

Here's my data set : https://prod-c2g.s3.amazonaws.com/db/Winter2013/files/viewmovie.sql 这是我的数据集: https : //prod-c2g.s3.amazonaws.com/db/Winter2013/files/viewmovie.sql

I'm asked to write an instead-of trigger that enables updates to the stars attribute of view LateRating.Here's my approach. 我被要求编写一个代替触发器,以启用对LateRating视图的stars属性的更新。这是我的方法。

CREATE trigger update_LateRating_title INSTEAD OF 
UPDATE OF stars ON LateRating
BEGIN
  UPDATE Rating SET stars = stars - 2 
  WHERE Rating.mID = old.mID 
  AND Rating.mID IN (SELECT stars FROM Rating WHERE stars > 2);
END;

It gives almost right answer but there is only one wrong row that being 201 101 4 2011-01-27.4 should be 2.What is wrong? 它给出的答案几乎是正确的,但是只有一排错误的行是201 101 4 2011-01-27.4应该是2.什么地方出了错?

Let's look at your WHERE condition: 让我们看看您的WHERE条件:

...AND Rating.mID IN (SELECT stars FROM Rating WHERE stars > 2)

It states that Rating.mID should be among values of stars selected from rating. 它指出Rating.mID应该在从评级中选择的stars值中。 I'm unsure what was intended here. 我不确定这里打算做什么。 Let's assume you want to decrease the number of stars by 2 for any attempt to modify stars (that's weird: you're turning queries which would increase stars into some unrelated queries) if the original number of stars in that line of Rating table was greater than 2. 假设您想将任何修改星号的尝试都减少2个星号(这很奇怪:您正在将可能将增加的星号转换为一些不相关的查询的查询),如果那一行“ Rating表中的原始星数更大比2。

CREATE trigger update_LateRating_stars INSTEAD OF 
UPDATE OF stars ON LateRating
BEGIN
  UPDATE Rating SET stars = stars - 2 
  WHERE Rating.mID = old.mID 
  AND Rating.stars > 2;
END;

Again, there are many possible interpretations of what you want, and they all are more or less weird : mID field in LateRating is not enough to identify the original record in Rating (maybe rID and mID together would be enough, if you don't allow the same reviewer to hold different opinions on the same movie simultaneously). 同样,也有你想要的东西很多可能的解释,他们都或多或少怪异mID现场LateRating不足以识别原始记录Rating (也许rIDmID在一起就足够了,如果你不允许同一位审稿人同时对同一部电影持有不同的意见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM