Skip to content
Advertisement

References invalid table in create table script with foreign keys

I’m trying to create a table in SQL Server with two foreign keys. But the problem is it’s not recognizing one of the reference tables.

Here is table creation script.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EmpGroup]
(
    [Id] INT IDENTITY(1,1) PRIMARY KEY ,    
    [GroupId] [nvarchar](100) NOT NULL,     
    CONSTRAINT [GroupId_FK] 
       FOREIGN KEY([GroupId]) REFERENCES [dbo].[Group](Id),  --Getting error on [dbo].[Group]
    [EmployeeId] [nvarchar](100) NOT NULL,      
    CONSTRAINT [EmployeeId_FK] 
       FOREIGN KEY([EmployeeId]) REFERENCES [dbo].[Employee](Id)
)
GO

SQL Server is throwing the error on [dbo].[Group]. Here is the error message.

enter image description here

I have created both of the tables [dbo].[Group] and [dbo].[Employee]. And the confusion is, why it’s not throwing the same error for [dbo].[Employee]?

Edit

Script of Group and Employee Table

CREATE TABLE [dbo].[Employee](
    [Id] [nvarchar](100) NOT NULL PRIMARY KEY,  -- we allow custom id addition, so no need of identity at all
    [Name] [nvarchar](100) NOT NULL,
    [RightThumb] [varbinary](max) NULL,
    [RightIndex] [varbinary](max) NULL,
    [LeftThumb] [varbinary](max) NULL,
    [LeftIndex] [varbinary](max) NULL,
    [CardNo] [nvarchar](100) NULL,
    [MayFairNo] [nvarchar](100) NULL,
    [UHFNo] [nvarchar](100) NULL,
    [Face] [varbinary](max) NULL,
    -- face headers column? 
)
CREATE TABLE [dbo].[Group](
    [Id] INT IDENTITY(1,1) PRIMARY KEY ,    -- we allow custom id addition, so no need of identity at all
    [Name] [nvarchar](100) NOT NULL,    
)

Advertisement

Answer

If you’re only concerned about the red squiggle under [dbo].[Group] it could just be that the IntelliSense cache in SSMS hasn’t sensed a newly created table yet – press Ctrl+Shift+R to refresh it then wait a few seconds to see if it goes away.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement