[英]Failure to make Foreign keys with two tables
I am in the process of building the website for the Linq, and the way that I need is to use Foreign keys to precisely set the same with my users table.¨ 我正在为Linq构建网站,我需要的方法是使用外键来精确地设置我的用户表。
I have assured me that my Tabler has a primary key because it must be unique content that use grab. 我向我保证我的Tabler有一个主键,因为它必须是使用grab的唯一内容。
Its a brugere table 这是一张古怪的桌子
CREATE TABLE [dbo].[brugere] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[username] NVARCHAR (255) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_brugere] PRIMARY KEY ([Id]),
CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([brugerid]),
CONSTRAINT [FK_brugere_ToKunde] FOREIGN KEY ([Id]) REFERENCES [KundeData]([brugerid])
);
Poinantal its here Poinantal在这里
CREATE TABLE [dbo].[pointantal] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[point] INT NOT NULL,
[omrade] NVARCHAR (255) NOT NULL,
[datotid] DATETIME DEFAULT (getdate()) NOT NULL,
[brugerid] INT NOT NULL,
CONSTRAINT [PK_pointantal] PRIMARY KEY ([Id])
);
and KundeData table here 和KundeData表在这里
CREATE TABLE [dbo].[KundeData] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Adresse] NVARCHAR (255) NOT NULL,
[Postnr] INT NOT NULL,
[Mobil] INT NOT NULL,
[Byen] NVARCHAR (255) NOT NULL,
[abonnementsId] INT NOT NULL,
[BuyDate] DATETIME DEFAULT (getdate()) NOT NULL,
[prisid] INT NOT NULL,
[HaevedeId] NVARCHAR (255) NULL,
[brugerid] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
The error message I receive when I try to updater content is here 我尝试更新内容时收到的错误消息在这里
Update cannot proceed due to validation errors. 由于验证错误,无法继续更新。
Please correct the following errors and try again. 请更正以下错误,然后重试。SQL71516 :: The referenced table '[dbo].[pointantal]' contains no primary or candidate keys that match the referencing column list in the foreign key. SQL71516 ::引用的表'[dbo]。[pointantal]'不包含与外键中的引用列列表匹配的主键或候选键。 If the referenced column is a computed column, it should be persisted. 如果引用的列是计算列,则应该保留它。 SQL71516 :: The referenced table '[dbo].[KundeData]' contains no primary or candidate keys that match the referencing column list in the foreign key. SQL71516 ::引用的表'[dbo]。[KundeData]'不包含与外键中的引用列列表匹配的主键或候选键。 If the referenced column is a computed column, it should be persisted. 如果引用的列是计算列,则应该保留它。
A foreign key can only reference a primary key or unique column. 外键只能引用主键或唯一列。 You can either add a unique constraint to the columns that you are referencing: 您可以向要引用的列添加唯一约束:
CREATE TABLE [dbo].[pointantal] (
...
CONSTRAINT AK_BrugerID UNIQUE(brugerid)
Or you can change your constraint to actually reference the primary key in your tables: 或者,您可以更改约束以实际引用表中的主键:
CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([Id])
However, it seems like you really want the brugerid
column of the pointantal
and KundeData
tables to access an Id
(which is a unique column) in the brugere
table. 但是,好像你真的想brugerid
的列pointantal
和KundeData
表来访问Id
的(这是一个独特的列) brugere
表。 In this case, you put the foreign key on those tables and have it access the primary key of the bruger
table. 在这种情况下,您将外键放在这些表上,并让它访问bruger
表的主键。 The following code runs sucessfully on my system: 以下代码在我的系统上成功运行:
CREATE TABLE [dbo].[brugere] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[username] NVARCHAR (255) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_brugere] PRIMARY KEY ([Id])
);
CREATE TABLE [dbo].[pointantal] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[point] INT NOT NULL,
[omrade] NVARCHAR (255) NOT NULL,
[datotid] DATETIME DEFAULT (getdate()) NOT NULL,
[brugerid] INT NOT NULL,
CONSTRAINT [PK_pointantal] PRIMARY KEY ([Id]),
CONSTRAINT [FK_point_ToBrugere] FOREIGN KEY ([brugerid]) REFERENCES [brugere]([Id])
);
a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table. 外键是一个表中的字段(或字段集合),用于唯一标识另一个表的行。 In simpler words, the foreign key is defined in a second table, but it refers to the primary key in the first table. 简单来说,外键在第二个表中定义,但它引用第一个表中的主键。
Try: 尝试:
1>Change the Reference column 1>更改参考列
CREATE TABLE [dbo].[pointantal] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[point] INT NOT NULL,
[omrade] NVARCHAR (255) NOT NULL,
[datotid] DATETIME DEFAULT (getdate()) NOT NULL,
[brugerid] INT NOT NULL,
CONSTRAINT [PK_pointantal] PRIMARY KEY ([Id])
);
CREATE TABLE [dbo].[KundeData] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Adresse] NVARCHAR (255) NOT NULL,
[Postnr] INT NOT NULL,
[Mobil] INT NOT NULL,
[Byen] NVARCHAR (255) NOT NULL,
[abonnementsId] INT NOT NULL,
[BuyDate] DATETIME DEFAULT (getdate()) NOT NULL,
[prisid] INT NOT NULL,
[HaevedeId] NVARCHAR (255) NULL,
[brugerid] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id])
);
CREATE TABLE [dbo].[brugere] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[username] NVARCHAR (255) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_brugere] PRIMARY KEY ([Id]),
CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([Id]),
CONSTRAINT [FK_brugere_ToKunde] FOREIGN KEY ([Id]) REFERENCES [KundeData]([Id])
);
2>Change The Primary-Key 2>更改主键
CREATE TABLE [dbo].[pointantal] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[point] INT NOT NULL,
[omrade] NVARCHAR (255) NOT NULL,
[datotid] DATETIME DEFAULT (getdate()) NOT NULL,
[brugerid] INT NOT NULL,
CONSTRAINT [PK_pointantal] PRIMARY KEY ([brugerid])
);
CREATE TABLE [dbo].[KundeData] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Adresse] NVARCHAR (255) NOT NULL,
[Postnr] INT NOT NULL,
[Mobil] INT NOT NULL,
[Byen] NVARCHAR (255) NOT NULL,
[abonnementsId] INT NOT NULL,
[BuyDate] DATETIME DEFAULT (getdate()) NOT NULL,
[prisid] INT NOT NULL,
[HaevedeId] NVARCHAR (255) NULL,
[brugerid] INT NOT NULL,
PRIMARY KEY CLUSTERED ([brugerid])
);
CREATE TABLE [dbo].[brugere] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[username] NVARCHAR (255) NOT NULL,
[password] NVARCHAR (255) NOT NULL,
CONSTRAINT [PK_brugere] PRIMARY KEY ([Id]),
CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([brugerid]),
CONSTRAINT [FK_brugere_ToKunde] FOREIGN KEY ([Id]) REFERENCES [KundeData]([brugerid])
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.