Sql-server – I can’t create relationships in MSSQL

sql server

This is what I get every time I try to create a new relationship and I don't know what to do.

The columns in table 'Cartea' do not match an existing primary key or
UNIQUE constraint.

enter image description here

That's the data from the tables.

enter image description here

enter image description here

Best Answer

It seems you're adding the foreign key from the wrong table. Since you're using the wizard to create it, you should start from Cartea table. It should look like this:

wizard configuration

And here is the equivalent T-SQL command:

ALTER TABLE [dbo].[Cartea]  WITH CHECK ADD  CONSTRAINT [FK_Cartea_Librarie] FOREIGN KEY([Id_librarie])
REFERENCES [dbo].[Librarie] ([Id_librarie])
GO

ALTER TABLE [dbo].[Cartea] CHECK CONSTRAINT [FK_Cartea_Librarie]
GO