Error creating foreign key

0

I'm creating two foreign key references pointing to two different tables, but I'm getting an error creating one.

  

There are no primary or candidate keys in the referenced table   'dbo.TB_PODERES' that match the referencing column list in the foreign   key 'FK_TB_GRUPOS_PODERES_PODERES_TB_PODERES'.

The reference table is as follows

    CREATE TABLE [dbo].[TB_PODERES](
    [ID_PDR] [int] IDENTITY(1,1) NOT NULL,
    [CD_PODER] [char](10) NOT NULL,
    [NM_PODER] [varchar](100) NOT NULL,
    [IC_ATIVO] [bit] NOT NULL,
    [DT_ULT_ATUALIZACAO] [datetime] NOT NULL,
 CONSTRAINT [PK_TB_PODERES_PODER] PRIMARY KEY NONCLUSTERED 
(
    [ID_PDR] ASC,
    [CD_PODER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

and the table I'm creating is FK.

    CREATE TABLE [dbo].[TB_GRUPOS_PODERES_PODERES](
    [ID_GPP] BIGINT IDENTITY(1,1) NOT NULL,
    [ID_GRP_PDR] [bigint] NOT NULL,
    [CD_PODER] [char](10) NOT NULL, 
    [IC_ATIVO] [bit] NOT NULL,
    [DT_ULT_ATUALIZACAO] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [ID_GPP],[ID_GRP_PDR],[CD_PODER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

    ALTER TABLE [dbo].TB_GRUPOS_PODERES_PODERES  WITH CHECK ADD  CONSTRAINT FK_TB_GRUPOS_PODERES_PODERES_TB_PODERES FOREIGN KEY([CD_PODER])
    REFERENCES [dbo].TB_PODERES ([CD_PODER])
GO
ALTER TABLE [dbo].TB_GRUPOS_PODERES_PODERES CHECK CONSTRAINT FK_TB_GRUPOS_PODERES_PODERES_TB_PODERES
GO
    
asked by anonymous 22.01.2018 / 14:39

1 answer

1

You need to use% w% of the same number of columns as FK . In this case, you have PK made up of PK your [ID_PDR],[CD_PODER] also needs 2 columns of the same data type.

I hope that helps you.

I tried your script and added this column FK to table ID_PDR

CREATE TABLE [dbo].[TB_PODERES](
    [ID_PDR] [int] IDENTITY(1,1) NOT NULL,
    [CD_PODER] [char](10) NOT NULL,
    [NM_PODER] [varchar](100) NOT NULL,
    [IC_ATIVO] [bit] NOT NULL,
    [DT_ULT_ATUALIZACAO] [datetime] NOT NULL,
 CONSTRAINT [PK_TB_PODERES_PODER] PRIMARY KEY NONCLUSTERED 
(
    [ID_PDR] ASC,
    [CD_PODER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[TB_GRUPOS_PODERES_PODERES](
    [ID_GPP] BIGINT IDENTITY(1,1) NOT NULL,
    [ID_GRP_PDR] [bigint] NOT NULL,
    [CD_PODER] [char](10) NOT NULL, 
    [IC_ATIVO] [bit] NOT NULL,
    [DT_ULT_ATUALIZACAO] [datetime] NOT NULL,
    [ID_PDR] [int],
PRIMARY KEY CLUSTERED 
(
    [ID_GPP],[ID_GRP_PDR],[CD_PODER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

    ALTER TABLE [dbo].TB_GRUPOS_PODERES_PODERES  WITH CHECK ADD  CONSTRAINT FK_TB_GRUPOS_PODERES_PODERES_TB_PODERES FOREIGN KEY([ID_PDR],[CD_PODER])
    REFERENCES [dbo].TB_PODERES ([ID_PDR],[CD_PODER])
GO

ALTER TABLE [dbo].TB_GRUPOS_PODERES_PODERES CHECK CONSTRAINT FK_TB_GRUPOS_PODERES_PODERES_TB_PODERES
GO
    
22.01.2018 / 17:31