Problem when creating constraint in Microsoft SQL

0

Hi, I'm trying to create these tables in SQL

create table Clientes(
                      Tdoc varchar (10),
                      Ndoc int,
                      Nombre varchar(30),
                      Apellido varchar(30),
                      constraint p_key primary key(Tdoc,Ndoc)
                      )


create table Vendedor(
                      Tdoc varchar (10),
                      Ndoc int,
                      Nombre varchar(30),
                      Apellido varchar(30),
                      constraint p_key_ve primary key(Tdoc,Ndoc)
                      )

create table Venta( tdoc_vend varchar (10),
                    ndoc_vend int,
                    tdoc_cli varchar (10),
                    ndoc_cli int,
                    nro_fac int,
                    fecha date,
                    tipo_pago int,
                    constraint venta_key primary key (tdoc_vend, ndoc_vend, tdoc_cli, ndoc_cli, nro_fac),
                    constraint t_doc foreign key (tdoc_vend, ndoc_vend) references Vendedor (Tdoc, Ndoc),
                    constraint cli foreign key (tdoc_cli, ndoc_cli) references Clientes (Tdoc, Ndoc)
                    )

create table Producto ( id int primary key,
                        descripcion varchar (20),
                        precio float)

create table TPago ( id int primary key,
                     descripcion varchar (20))

create table Detalle_venta (nro_fact int,
                            id_prod int,
                            cantidad int,
                            constraint keyDv primary key (nro_fact, id_prod),
                            constraint nrofac foreign key (nro_fact) references Venta(nro_fac),
                            constraint idpro foreign key (id_prod) references Producto(id)
                            )

The problem is when I try to create the last table, where I get the following error

Mens. 1776, Nivel 16, Estado 0, Línea 880
There are no primary or candidate keys in the referenced table 'Venta' that match the referencing column list in the foreign key 'nrofac'.
Mens. 1750, Nivel 16, Estado 1, Línea 880
Could not create constraint or index. See previous errors.
    
asked by Sebastian Ceru 12.07.2018 в 01:35
source

1 answer

0

what happens is that in the table Ventas you are assigning as primary key nro_fac

constraint venta_key primary key (tdoc_vend, ndoc_vend, tdoc_cli, ndoc_cli, nro_fac),

, then in the table Detalle_venta you are assigned a primary key key that is nro_fac and then you try to put a foreign key with that field

 constraint nrofac foreign key (nro_fact) references Venta(nro_fac),

so a foreign key only gives from a primary key to a field of the same type.

I hope it has helped you. By srJJ postd: if I help you mark it as resolved

    
answered by 12.07.2018 в 04:24