我在連接MSSQL Management studio的兩個表時遇到問題。 我的目標是通過外鍵連接表,如果我刪除用戶,我想第二個表條目將被自動刪除。我打算使用DELETE Cascade方法。SQL主鍵的外鍵問題
User:
CREATE TABLE [dbo].[Users](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Email] [nvarchar](89) NOT NULL,
[Name] [nvarchar](25) NOT NULL,
[Midname] [nvarchar](25) NOT NULL,
[Surname] [nvarchar](25) NOT NULL,
[Phone] [varchar](15) NOT NULL,
[Country] [smallint] NOT NULL,
[Manager] [nvarchar](89) NOT NULL,
[Referrer] [nvarchar](89) NOT NULL,
[Rank] [tinyint] NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
電子郵件是主鍵
Payments:
CREATE TABLE [dbo].[Investments](
[ID] [bigint] NOT NULL,
[Investor] [nvarchar](89) NOT NULL,
[Sum] [decimal](19, 4) NOT NULL,
[Currency] [smallint] NOT NULL,
[Credit] [decimal](19, 4) NOT NULL,
[CreditRate] [decimal](19, 4) NOT NULL,
[Rate] [tinyint] IDENTITY(1,1) NOT NULL,
[Date] [smalldatetime] NOT NULL,
[Comment] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Investments] PRIMARY KEY CLUSTERED
(
[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]
GO
ID是主鍵
我FK應該像USER->付款或付款 - >用戶?
當我試圖連接的用戶 - 使用foregn鍵通過電子郵件>付款 - >投資者,它告訴我,這樣的錯誤:
The columns in table 'Payments' do not match an existing primary key or UNIQUE constraint.
能否請你解釋一下我在哪裏的問題是什麼?我做錯了什麼?
Users.ID
和你在第二個表設置電子郵件喜歡外國的?在這裏添加聲明。 – Sajmon1)發佈你的DDL的表。 2)使用電子郵件地址作爲主鍵是一個非常糟糕的主意。 – Perception
你標記了你的問題[sql]但是哪個? Oracle,MS SQL Server,MySQL ...等,併發布報告錯誤的查詢,這將有助於人們解決您的問題。 – Tony