SQL SMS error creating two FOREIGN KEY on one table

3465 views sql
-1

create table TiposPeliculas
(
    id_tipoPelicula int identity (1, 1), 
    descipcion varchar (30)
    constraint pk_tipoPelicula primary key(id_tipoPelicula)
)

create table Generos
(
     id_genero int identity (1,1), 
     descripcion varchar(20)
     constraint pk_generos primary key(id_genero)
)

create table Peliculas 
(
     id_pelicula int not null, 
     id_tipoPelicula int, 
     id_genero int, 
     titulo varchar (30),
     duracion time,
     idioma varchar (30),
     sub bit
     constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
     constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
     constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TipoPeliculas (id_tipoPelicula)
)

The FOREIGN KEY for id_tipoPelicula throwns an error:

foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"

PLEASE HELP MOST DELIVER TO LORD PROFESSOR TOMORROW

answered question

2 Answers

1

create table Peliculas 
(
      id_pelicula int not null, 
      id_tipoPelicula int, 
      id_genero int, 
      titulo varchar (30),
      duracion time,
      idioma varchar (30),
      sub bit 
  constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)  
  constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero), 
  constraint fk_tiposPeliculasDePeli foreign key (id_tiposPelicula) references TiposPeliculas (id_tiposPelicula)
)

This will work the error in spelling mistake in relation name TiposPeliculas.

posted this
13

The error:

foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"

It says, the foreign key is reference to invalid Table "TipoPeliculas", Which means table "TipoPeliculas" doesn't exists.

In your question the table name is "TiposPeliculas". Hence it seems you need to correct table name in reference.

create table Peliculas 
(
     id_pelicula int not null, 
     id_tipoPelicula int, 
     id_genero int, 
     titulo varchar (30),
     duracion time,
     idioma varchar (30),
     sub bit
     constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
     constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
     constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TiposPeliculas (id_tipoPelicula)
)

posted this

Have an answer?

JD

Please login first before posting an answer.