本文介绍了Sql表设计问题。一个包含两个表键的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有两张桌子:老师和课程 每位老师都可以有一个或多个课程我创建一个新桌子并将其命名为TeacherLesson:I have two tables : Teacher and LessonEvery teacher may have one or more lessons i create a new table and named it TeacherLesson :CREATE TABLE [dbo].[TeacherLesson] ([Id] INT IDENTITY (1, 1) NOT NULL,[teacherID] INT NULL,[LessonID] INT NULL,PRIMARY KEY CLUSTERED ([Id] ASC)); 当用户尝试添加新教师时,他/她可以选择l新老师的essons所以我有一个用户可以选择它们的课程列表。这意味着我已经拥有了课程ID,但教师用的内容是什么?教师表中的 teacherID是一个身份规范(auto int)字段,所以我不知道下一个id是什么!我如何使用teacherID和LessonID在TeacherLesson表中插入新记录 请注意我使用linq to SQL和WPF C#! 我可以使用foreign-key和make teacherID外键并做这样的事情? :when user try to add a new teacher he/she can choose lessons for new teacher so i have a list of lessons that user can select them. that means i already have the lesson id but what about TeacherIDteacherID in teacher table is an identity specification (auto int) field so i don't know what is next id ! how i can insert new record in TeacherLesson Table with teacherID and LessonIDnote that i use linq to SQL and WPF C#!can i use foreign-key an make teacherID foreign-key and do something like this ? :Teachers.TeacherLesson.add(newTeacherLesson); 技术上还可以吗? 谢谢! 我的尝试: SQL表格设计问题。一个表包含两个表键is it technically OK ?thank you!What I have tried:SQL Table design Issue. one table that contains two table Keys推荐答案Int32 newId = (Int32) myCommand.ExecuteScalar(); b $ b 查看 检索身份或自动编号值 [ ^ ]CREATE TABLE [dbo].[TeacherLesson] ([Id] INT IDENTITY (1, 1) NOT NULL,[teacherID] INT NULL,[LessonID] INT NULL,PRIMARY KEY CLUSTERED ([Id] ASC)); 并且对于获取最后插入的标识值,您可以使用and for get last-inserted identity value you may use@@IDENTITY 更多详情参考: 这里 希望它可以帮到你。 谢谢For more details Reference : HereHope it helps you .Thankspublic static void InsertOrUpdateTeacher( string Name,string Family,string Licence , byte[] Bytes,List<lesson> Lessons) { DataClassesDataContext dc = new DataClassesDataContext(); Table<teacher> TeacherTable = dc.GetTable<teacher>(); Teacher teacher = new Teacher(); teacher.Name = Name; teacher.Family = Family; teacher.Licence = Licence; teacher.Image = Bytes; foreach (Lesson item in Lessons) { TeacherLesson tl = new TeacherLesson(); tl.LessonID = item.Id; Teacher.TeacherLessons.Add(tl); } TeacherTable.InsertOnSubmit(Teacher); TeacherTable.Context.SubmitChanges(); }</teacher></teacher></lesson> 这是带有外键的TeacherLesson表and this is TeacherLesson table with Foreign-keyCREATE TABLE [dbo].[TeacherLesson] ( [Id] INT IDENTITY (1, 1) NOT NULL, [teacherID] INT NULL, [LessonID] INT NULL, PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_TeacherLesson_ToTable] FOREIGN KEY ([teacherID]) REFERENCES [dbo].[Teacher] ([Id])); 这篇关于Sql表设计问题。一个包含两个表键的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 02:17