增强SQL体系结构

时间:2016-04-08 18:07:52

标签: sql database-design relational-database erd

我需要设计一个没有重复组的表格,我认为我已经这样做了,但表格看起来是正确的,如何链接等等,还是有什么我可以改进的?我跑了所有的查询,我有一些重复的组播放器&经理,因为我把他们都放在一个班组中,所以我将他们分成了team_player& team_manager,这样一个或多个经理也可以管理团队,反之亦然,体育场等。

drop table film_director;
drop table film_actor;
drop table film;
drop table studio;
drop table actor;
drop table director;

CREATE TABLE studio(
  studio_ID NUMBER NOT NULL,
  studio_Name VARCHAR2(30),
  PRIMARY KEY(studio_ID));

CREATE TABLE film(
  film_ID NUMBER NOT NULL,
  studio_ID NUMBER NOT NULL,
  genre VARCHAR2(30),
  genre_ID NUMBER(1),
  film_Len NUMBER(3),
  film_Title VARCHAR2(30) NOT NULL,
  year_Released NUMBER NOT NULL,
  PRIMARY KEY(film_ID),
  FOREIGN KEY (studio_ID) REFERENCES studio);

CREATE TABLE director(
  director_ID NUMBER NOT NULL,
  director_fname VARCHAR2(30),
  director_lname VARCHAR2(30),
  PRIMARY KEY(director_ID));

CREATE TABLE actor(
  actor_ID NUMBER NOT NULL,
  actor_fname VARCHAR2(15),
  actor_lname VARCHAR2(15),
  PRIMARY KEY(actor_ID));

CREATE TABLE film_actor(
  film_ID NUMBER NOT NULL,
  actor_ID NUMBER NOT NULL,
  PRIMARY KEY(film_ID, actor_ID),
  FOREIGN KEY(film_ID) REFERENCES film(film_ID),
  FOREIGN KEY(actor_ID) REFERENCES actor(actor_ID));

CREATE TABLE film_director(
  film_ID NUMBER NOT NULL,
  director_ID NUMBER NOT NULL,
  PRIMARY KEY(film_ID, director_ID),
  FOREIGN KEY(film_ID) REFERENCES film(film_ID),
  FOREIGN KEY(director_ID) REFERENCES director(director_ID));

INSERT INTO studio (studio_ID, studio_Name) VALUES (1, 'Paramount');
INSERT INTO studio (studio_ID, studio_Name) VALUES (2, 'Warner Bros');
INSERT INTO studio (studio_ID, studio_Name) VALUES (3, 'Film4');
INSERT INTO studio (studio_ID, studio_Name) VALUES (4, 'Working Title Films');

INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (1, 1, 'Comedy', 1, 180, 'The Wolf Of Wall Street', 2013);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (2, 2, 'Romance', 2, 143, 'The Great Gatsby', 2013);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (3, 3, 'Science Fiction', 3, 103, 'Never Let Me Go', 2008);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (4, 4, 'Romance', 4, 127, 'Pride and Prejudice', 2005);

INSERT INTO director (director_ID, director_fname, director_lname) VALUES (1, 'Martin', 'Scorcese');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (2, 'Baz', 'Luhrmann');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (3, 'Mark', 'Romanek');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (4, 'Joe', 'Wright');

INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (1, 'Matthew', 'McConnaughy');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (2, 'Leonardo', 'DiCaprio');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (3, 'Margot', 'Robbie');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (4, 'Joanna', 'Lumley');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (5, 'Carey', 'Mulligan');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (6, 'Tobey', 'Maguire');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (7, 'Joel', 'Edgerton');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (8, 'Keira', 'Knightly');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (9, 'Andrew', 'Garfield');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (10, 'Sally', 'Hawkins');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (11, 'Judi', 'Dench');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (12, 'Matthew', 'Macfadyen');

INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 1);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 2);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 3);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 4);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 2);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 6);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 7);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 8);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 9);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 10);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 8);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 11);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 12);

INSERT INTO film_director (film_ID, director_ID) VALUES (1,1);
INSERT INTO film_director (film_ID, director_ID) VALUES (2,2);
INSERT INTO film_director (film_ID, director_ID) VALUES (3,3);
INSERT INTO film_director (film_ID, director_ID) VALUES (4,4);

2 个答案:

答案 0 :(得分:1)

我只会为用户(导演/演员)提供一张桌子,因为如果导演也像Quentin Tarantino这样的演员出现在他的大部分电影中会发生什么。你必须重复数据。

https://en.wikipedia.org/wiki/Database_normalization

尝试将你的模型带到BCNF或4NF,你会好的

答案 1 :(得分:1)

我同意sdmon - 因为导演和演员具有相同的属性,所以将它们全部保存在一个表中是有意义的,并在“事实”表中为它们分配角色。

一部电影可以有一个以上的导演吗?如果没有,那么你不需要film_director表,你只需要一个电影表中的“director”列。否则你需要film_director表。

一部电影可以与多个工作室联系吗?在您的模型中,您在电影表中有一个studio_id列,但也有一个film_studio表。你不需要两者。如果电影总是由单个工作室制作,则需要列(但不是表格)。如果你需要为一部电影展示多个工作室,那么你需要额外的表格,而不是列。

其他一切看起来都不错。祝你好运!