Heera
Heera

Reputation: 1

SQL71501 :: Foreign Key: [dbo].[FK_AddressBook_Country] has an unresolved reference to Column [dbo].[AddressBook].[CountryID]

hope someone can help me. I have an error " SQL71501 :: Foreign Key: [dbo].[FK_AddressBook_Country] has an unresolved reference to Column [dbo].[AddressBook].[CountryID]. " It red line the ([CountryID)]. I can't find where its fault. I actually followed this site http://demo.dotnetawesome.com/mvc/mycontactbook/part2 , I just changed the tables. Hopefully, someone can help me with this. Thank you so much!

CREATE TABLE [dbo].[AddressBook] (
    [Id]           INT           NOT NULL,
    [Name]         VARCHAR (50)  NOT NULL,
    [Surname]      VARCHAR (50)  NOT NULL,
    [Address1]     VARCHAR (200) NOT NULL,
    [Address2]     VARCHAR (100) NULL,
    [Postcode]     VARCHAR (6)   NOT NULL,
    [Town]         VARCHAR (20)  NOT NULL,
    [Country]      VARCHAR (50)  NOT NULL,
    [Email]        VARCHAR (50)  NOT NULL,
    [MobileNumber] VARCHAR (20)  NOT NULL,
    [PictureUser]  VARCHAR (200) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC), 
    CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([CountryID]) REFERENCES [Country]([CountryID])
);
CREATE TABLE [dbo].[Country] (
    [CountryID]   INT IDENTITY (1, 1) NOT NULL,
    [CountryName] VARCHAR (100) NOT NULL,
    PRIMARY KEY CLUSTERED ([CountryID] ASC)
);

Upvotes: 0

Views: 2011

Answers (2)

Hamni
Hamni

Reputation: 1

The same error happened to me because I added a space after the country and did not write it in SQL

Upvotes: 0

mhido art
mhido art

Reputation: 1

this should work fine how ever there are two mistakes first in "FOREIGN KEY ([CountryID])" you have no field in the AdresseBook named "CountryId" i think you wanted the field "Country" as a foreign key if this is the case the the Country field in adressBook must have the same type with CountryID of the table Country

 CREATE TABLE [dbo].[AddressBook] (
        [Id]           INT           NOT NULL,
        [Name]         VARCHAR (50)  NOT NULL,
        [Surname]      VARCHAR (50)  NOT NULL,
        [Address1]     VARCHAR (200) NOT NULL,
        [Address2]     VARCHAR (100) NULL,
        [Postcode]     VARCHAR (6)   NOT NULL,
        [Town]         VARCHAR (20)  NOT NULL,
        [Country]      INT  NOT NULL,
        [Email]        VARCHAR (50)  NOT NULL,
        [MobileNumber] VARCHAR (20)  NOT NULL,
        [PictureUser]  VARCHAR (200) NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC), 
        CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([Country]) REFERENCES [Country]([CountryID])
    );
    CREATE TABLE [dbo].[Country] (
        [CountryID]   INT IDENTITY (1, 1) NOT NULL,
        [CountryName] VARCHAR (100) NOT NULL,
        PRIMARY KEY CLUSTERED ([CountryID] ASC)
    );

Upvotes: 0

Related Questions