简体   繁体   English

我有现有的表,并希望使自动编号成为一个表中的主键,而使另一个表中的外键。 有什么好办法?

[英]I have existing tables and want to make Autonumber the Primary key in one table and Foreign key in the other table. What is a good way?

In Access, I have one table called courses and another table called lessons 在Access中,我有一个表称为“课程”,另一个表称为“课程”

the table courses 餐桌课程

id  name    code
1   english LEN
2   german  LDE
3   math    SMA

the table lessons 桌上课

id  name        coursename  class_id
1   shakespare  english     ?
2   hesse       german      ?
3   trigonometrymath        ?
4   algebra     math        ?
5   whitman     english     ?

Previously I used course name as a primary key in courses and a foreign key in classes. 以前,我使用课程名称作为课程的主键,而在课程中使用外键。 Now I want to use the autonumber field course_id instead. 现在,我想改用自动编号字段course_id。 What is a good way to make this transition? 进行此过渡的好方法是什么?

Your question is a little messed up compared to the tables you are presenting. 与您要呈现的表格相比,您的问题有些混乱。 What I understand is you have an autonumber field in courses named id . 据我了解,您在名为id courses有一个自动编号字段。 You want to store that in lessons in a field called course_id . 您希望将其存储在lessons中的一个名为course_id的字段中。 If this is true then follow the following: 如果是这样,请按照下列步骤操作:

The easiest way would be to open the lessons table in design view. 最简单的方法是在设计视图中打开lessons表。 Add the course_id field to the table as a long integer and save. course_id字段作为长整数添加到表中并保存。 Then execute the following query: 然后执行以下查询:

UPDATE lessons
INNER JOIN courses ON courses.name = lessons.coursename
SET lessons.course_id = courses.id

Then open the lessons table in design view again and delete the coursename field. 然后再次在设计视图中打开lessons表,并删除coursename字段。

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

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