简体   繁体   English

尝试在FK T-SQL上将记录插入表错误

[英]Trying to Insert records into table Error on FK T-SQL

I'm trying to insert new text book records into aa database. 我正在尝试将新的教科书记录插入数据库中。 I have a Course table with columns ID (PK), CourseID , CourseTitle . 我有一个带有列ID (PK), CourseIDCourseTitleCourse表。

textBook table (all columns, ID (PK)) is a many to many relationship so multiple courses can have the same book and courses can also have multiple different books. textBook表(所有列, ID (PK))是多对多关系,因此多个课程可以具有同一本书,而课程也可以具有多个不同的书籍。

When I try to insert a new text book into my database using C# I get an error on my foreign key. 当我尝试使用C#将新的教科书插入数据库时​​,我的外键出现错误。 The Course table is parent, Textbook table is child. Course表是父级, Textbook表是子级。 The ID column in both tables is set to identity and auto increments. 两个表中的ID列均设置为标识和自动递增。 ID is my foreign key in my textBook table referencing the Course table. ID是我的textBook表中引用Course表的外键。

here is my intermediate table. 这是我的中间表。

CREATE TABLE [dbo].[BookCourse]
(
[cID] INT NOT NULL Unique, 
[BookID] INT NOT NULL Unique, 
[BookCourseID] INT NOT NULL, 
CONSTRAINT [PK_BookCourse] PRIMARY KEY ([BookCourseID]) 
)

here is my textBook table 这是我的教科书表

CREATE TABLE [dbo].[textBooks] (
[thirteenISBN]   VARCHAR (255) NOT NULL,
[CourseID]       VARCHAR (50)  NOT NULL,
[BookTitle]      VARCHAR (255) NULL,
[Ancillary]      VARCHAR (255) NULL,
[BookActive]     VARCHAR (20)  NULL,
[ActiveDate]     VARCHAR (50)  NULL,
[InactiveDate]   VARCHAR (50)  NULL,
[Author]         VARCHAR (255) NULL,
[Imprint]        VARCHAR (100) NULL,
[Publisher]      VARCHAR (100) NULL,
[EditionAndDate] VARCHAR (120) NULL,
[tenISBN]        VARCHAR (255) NULL,
[VendorISBN]     INT           NULL,
[ebookAvailable] VARCHAR (50)  NULL,
[eISBN]          VARCHAR (255) NULL,
[Notes]          VARCHAR (255) NULL,
[BookID]             INT           IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_textBooks] PRIMARY KEY CLUSTERED ([BookID] ASC), 
CONSTRAINT [FK_textBooks_ToTable] FOREIGN KEY ([BookID]) REFERENCES [BookCourse]([BookID])
);

Here is my Course Table 这是我的课程表

CREATE TABLE [dbo].[Course] (
[CourseID]    VARCHAR (50)  NOT NULL,
[CourseTitle] VARCHAR (255) NULL,
[cID]          INT           IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([cID] ASC), 
CONSTRAINT [FK_Course_ToTable] FOREIGN KEY ([cID]) REFERENCES [BookCourse]([cID]) 
);

Table Adapters with Insert: 带插入的表适配器:

JUTDMSTableAdapters.textBooksTableAdapter bookTableAdapter;
bookTableAdapter = new JUTDMSTableAdapters.textBooksTableAdapter();

JUTDMSTableAdapters.CourseTableAdapter courseTableAdapter;
courseTableAdapter = new JUTDMSTableAdapters.CourseTableAdapter();

courseTableAdapter.Insert( CourseID: txtCourseID.Text, CourseTitle: txtCourseTitle.Text);

bookTableAdapter.Insert( thirteenISBN: txt13ISBN.Text, CourseID: txtCourseID.Text, BookTitle: txtBookTitle.Text, Ancillary: txtAncillary.Text,
            BookActive: txtBookActive.Text, ActiveDate: txtActiveDate.Text, InactiveDate: txtInactiveDate.Text, Author: txtAuthor.Text,
            Imprint: txtImprint.Text, Publisher: txtPublisher.Text, EditionAndDate: txtEditionDate.Text,
            VendorISBN: vendISBN, tenISBN: txt10ISBN.Text, ebookAvailable: txtEBookAvailable.Text, eISBN: txtEISBN.Text, Notes: txtNotes.Text);

I figured in my Course table adapter insert I wouldn't have to add the cID column seeing as it is auto increment but I get this error: 我在Course表适配器插入中发现,我不必添加cID列,因为它是自动递增的,但出现此错误:

Additional information: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Course_ToTable". 附加信息:INSERT语句与FOREIGN KEY约束“ FK_Course_ToTable”冲突。 The conflict occurred in database "F:\\HUTDMS V-2.0\\HUTDMS V-2.0\\APP_DATA\\HUTDMS.MDF", table "dbo.BookCourse", column 'cID'. 在数据库“ F:\\ HUTDMS V-2.0 \\ HUTDMS V-2.0 \\ APP_DATA \\ HUTDMS.MDF”的表“ dbo.BookCourse”的列“ cID”中发生了冲突。

BookID in the textbook table is autoincrement cID in the Course table is autoincrement BookCourseID in the BookCourse table is autoincrement. 教科书表中的BookID是自动增量。课程表中的BookID是自动增量。BookCourse表中的BookCourseID是自动增量。

For many-to-many relationship you need to use three tables, Book table, Course table and then intermediate table - BookCourse table. 对于多对多关系,您需要使用三个表:Book表,Course表和中间表-BookCourse表。 Book table refers BookCourse, Course Table refers BookCourse. 书表指的是BookCourse,课程表指的是BookCourse。 There are NOT direct references between Book and Course tables 书和课程表之间没有直接引用

You first add book into Book table, then Course into Course table, finally you add the pair (CourseID, BookID) into BookCourse table. 首先,将书添加到Book表中,然后将Course添加到Course表中,最后将对(CourseID,BookID)添加到BookCourse表中。

As for BookCourse table, you may add composite primary key (CourseID, BookID), or add identity key BookCourseID, but if latter then you need to make sure there are no duplicates - you can create an unique constraint. 对于BookCourse表,您可以添加复合主键(CourseID,BookID)或添加标识键BookCourseID,但是如果是后者,则需要确保没有重复-您可以创建唯一约束。

https://en.wikipedia.org/wiki/Many-to-many_(data_model) https://zh.wikipedia.org/wiki/多对多(数据模型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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