简体   繁体   中英

Relationship to an Identity Column that is not a primary key

I have a Customers table where ID ( INT ) is an Identity column (1,1) and CustomerID ( NVARCHAR ) is the primary key. I have a second table with CustID ( INT ) and I would like to create a relationship to Customers table's ID column.

I have been notified by SQL Server Management Studio that there is no matching primary key or unique constraint - so I added the constraint on the ID column of Customers to make it unique. Same message.

主键或唯一约束错误

EDIT Adding my SQL scripts for tables, constraints and relationship

CREATE TABLE [dbo].[Customers]
(
    [CustomerID] [nvarchar](5) NOT NULL,
    [CompanyName] [nvarchar](40) NOT NULL,
    [ContactName] [nvarchar](30) NULL,
    [ContactTitle] [nvarchar](30) NULL,
    [Address] [nvarchar](60) NULL,
    [Address2] [nvarchar](60) NULL,
    [City] [nvarchar](30) NULL,
    [State] [char](2) NULL,
    [Region] [nvarchar](15) NULL,
    [PostalCode] [nvarchar](10) NULL,
    [Country] [nvarchar](15) NULL,
    [Phone] [nvarchar](24) NULL,
    [Fax] [nvarchar](24) NULL,
    [ID] [int] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_Customers] 
        PRIMARY KEY CLUSTERED ([CustomerID] ASC),
    CONSTRAINT [ID_Unique] 
        UNIQUE NONCLUSTERED ([ID] ASC)
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Customers] 
    ADD CONSTRAINT [DF_Customers_State]  DEFAULT ('VA') FOR [State]
GO

ALTER TABLE [dbo].[Customers]  WITH CHECK 
    ADD CONSTRAINT [FK_CustomersStates] 
        FOREIGN KEY([State]) REFERENCES [dbo].[States] ([Abbrev])
GO

ALTER TABLE [dbo].[Customers] CHECK CONSTRAINT [FK_CustomersStates]
GO


-- =======================================================================

CREATE TABLE [dbo].[Quote]
(
    [Id] [int] NOT NULL,
    [Number] [nvarchar](15) NOT NULL,
    [SalesPersonId] [int] NULL,
    [CustId] [int] NOT NULL,
    [Description] [nvarchar](max) NULL,
    [Status] [int] NULL,
    [QuoteDate] [datetime] NULL,

    CONSTRAINT [PK_Quote] 
        PRIMARY KEY NONCLUSTERED ([Id] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[Quote]  WITH CHECK 
    ADD CONSTRAINT [FK_tbl_Quote_tbl_SalesPerson] 
       FOREIGN KEY([SalesPersonId]) REFERENCES [dbo].[SalesPerson] ([Id])
GO

ALTER TABLE [dbo].[Quote] CHECK CONSTRAINT [FK_tbl_Quote_tbl_SalesPerson]
GO

ALTER TABLE [dbo].[Quote]  WITH CHECK 
    ADD CONSTRAINT [FK_tbl_Quote_tbl_Status] 
        FOREIGN KEY([Status]) REFERENCES [dbo].[Status] ([Id])
GO

ALTER TABLE [dbo].[Quote] CHECK CONSTRAINT [FK_tbl_Quote_tbl_Status]
GO

-- ================================================================

CREATE TABLE [dbo].[States]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Abbrev] [char](2) NOT NULL,

    CONSTRAINT [PK_States_1] 
        PRIMARY KEY CLUSTERED ([Abbrev] ASC)
) ON [PRIMARY]
GO

-- ==================================================================

CREATE TABLE [dbo].[Status]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](50) NOT NULL,

    CONSTRAINT [PK_Status_Id] 
        PRIMARY KEY NONCLUSTERED ([Id] ASC)
) ON [PRIMARY]
GO

--------- ==========================================================

ALTER TABLE CUSTOMERS
Add  Constraint ID_Unique UNIQUE (ID)

-- =========================================================================

ALTER TABLE QUOTE

 ADD CONSTRAINT  FK_QuoteCustId_CustomerId
    FOREIGN KEY (CustId)
    REFERENCES dbo.CUSTOMERS (ID)
    ON DELETE  NO ACTION 
    ON UPDATE  NO ACTION 

Your error is unreproducible.

I took the code from your question, changed the order of the CREATE TABLE statements so that the Status and State table were created before Customer . And I also took out the creation of the duplicate Constraint:

--this already exists
 CONSTRAINT [ID_Unique] UNIQUE NONCLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

--so don't create this
 ALTER TABLE CUSTOMERS
Add  Constraint ID_Unique UNIQUE (ID)

And I was able to run your code without encountering the error you describe. The only error I got was creating the FK to SalesPerson because you didn't include the DDL for that table.

Therefore there is some difference in the code you are executing and the code you are showing us that is causing your error.

This works for me.

create table tmp.Customer(ID  INT identity(1,1) , CustID varchar(8) primary key);
GO
create table tmp.Second(blah int, RefCustID INT);
GO
Create unique index UX1 on tmp.Customer(ID)
GO
ALTER TABLE tmp.Second
ADD CONSTRAINT whatever
FOREIGN KEY (RefCustID) 
REFERENCES tmp.Customer(ID)
GO

I suspect that you have swapped the positions of the Customer and Second table names in the ALTER TABLE command that creates the Foreign Key. (this is more common than you would think)

In response to a question in the comments, I changed the unique index to a unique constraint and retried it, still works.

create table tmp.Customer(ID  INT identity(1,1) , CustID varchar(8) primary key);
GO
create table tmp.Second(blah int, RefCustID INT);
GO
Alter table Tmp.Customer  ADD Constraint UX1 Unique (ID)
GO
ALTER TABLE tmp.Second
ADD CONSTRAINT whatever
FOREIGN KEY (RefCustID) 
REFERENCES tmp.Customer(ID)
GO

The other possibility here is that your datatypes do not match. You would have to provide the table definitions for us to check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM