У меня есть таблица Customers
, где ID
(INT
) - это столбец Identity
(1,1), а CustomerID
(NVARCHAR
) - первичный ключ. У меня есть вторая таблица с CustID
(INT
), и я хотел бы создать отношение к Customers
столбцу ID
таблицы.
Студия управления SQL Server уведомила меня, что нет соответствующего первичного ключа или уникального ограничения - поэтому я добавил ограничение на столбец идентификатора Customers
, чтобы сделать его уникальным. То же сообщение.
РЕДАКТИРОВАТЬ Добавление моих сценариев SQL для таблиц, ограничений и отношений
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