开发者

MS Access Relationship between 2 tables

I have a Access DB and 2 tables

I need to set the relationships but need some h开发者_如何学运维elp.

table 1

id name address postcode room-name

table 2

room-names

Then table 2 contains data:

eg:

kitchen, bedroom etc

How do I do this join so that table1 room-name field contains table 2 room-names list?


The best way to do this is to create a field in table one called "room_name_id" and set it as a Number. It will take up less space in the mdb file over time. (Sidenote: You can't use "name" as a field name since "name" is a reserved word in Access.)

Next create table two so you have an "id" field and a "room_name" field. So if "Spare room" contains id 1 then in table one, room_name_id could be 1.

Here's the SQL that you could use on a query.

    SELECT tblInfo.username, tblInfo.address, tblRoomNames.room_name
FROM tblRoomNames INNER JOIN tblInfo ON tblRoomNames.id = tblInfo.room_name_id;

This will show something like "Peter", "Narnia", "Spare room" even though table one has "Peter", "Narnia", "1".

P.S. I use this all the time at work to populate dropdown boxes with options for the user to choose from. This way I can update multiple dropdown boxes just by editing one table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜