0

So I've got Table ActorInMovies, which has 3 foreign keys.

CREATE TABLE ActorInMovie(
    ID_ROLE bigserial REFERENCES Role(ID_ROLE) ON DELETE CASCADE,
    ID_ACTOR bigserial REFERENCES Actor(ID_Actor) ON DELETE CASCADE,
    ID_MOVIE bigserial REFERENCES Movie(ID_Movie) ON DELETE CASCADE,
    CONSTRAINT ActorInMovie_pk PRIMARY KEY (ID_ROLE));

I assumed that when I try to insert something like:

INSERT INTO ActorInMovie (ID_ROLE, ID_ACTOR) values (1,1);

that it would result in an error as ID_MOVIE was not specified (null I supposed).. but it automatically starts assigning indexes staring from 1.

What am I doing wrong? As written here, I thought that "PostgreSQL automatically creates indexes on primary keys and unique constraints, but not on the referencing side of foreign key relationships."

4

2 回答 2

5

我很难想象一个serial(或bigserial列引用另一列的用例。通常情况正好相反:serial列应该位于外键约束的另一端。

我同样很难想象一个movie_id需要abigint而不是int. 这个星球上几乎没有足够的电影。

此外,很有可能,在名为movie_id的表中调用的列actor_in_movie应定义为NOT NULL.

简而言之:我怀疑你的设计根本不靠谱。也许是这样的:

CREATE TABLE actor (actor_id serial PRIMARY KEY, actor text, ...);
CREATE TABLE movie (movie_id serial PRIMARY KEY, movie text, ...);
CREATE TABLE actor_in_movie(
   role_id  serial PRIMARY KEY
  ,actor_id int NOT NULL REFERENCES actor(actor_id) ON DELETE CASCADE
  ,movie_id int NOT NULL REFERENCES movie(movie_id) ON DELETE CASCADE
);

约束是多余的NOT NULL,而列包含在主键中。

您可能需要索引 onactor_id和on movie_idactor_in_movie

更多细节:

于 2013-11-04T21:05:30.610 回答
3

bigserial与宣传的完全一样。它与外键约束或索引无关。

于 2013-11-04T20:05:09.177 回答