简体   繁体   中英

Multiple tables for a single foreign key

I'm currently making a database for my java web app for Tourism.

Only register user can book a tour but can take along several people. For this, I separate into User table and Guest table, each with its own primary key.

Everything is set so far, but when it comes to making my BookRoomDetail table, I have to fill in which person for which slot in the room. the problem arises when both register user and guest can fill this slot, and they're from 2 different tables.

截图

How do I set the foreign key(or anything else) for this?

As I got your problem, you can add one more field for MainUser in your tblBookRoom Detail as a foreign key. And whenever any user books room you can add his/her primary key and guest primary key in forwhom field.

 **tblBookRoomDetail**
      ID (Primary key Of Table)
      RoomId 
      For Whom (For MainUser or Guest)
      MainUser (MainUser Primary Key Who is doing this reservation)
      Slot
      FromDate
      ToDate
      BookId

This is not possible to insert two different primary key value in a column of other table, if you have relation in them.

You can do this without the making the relation in them. But it's not a good process because if the both key value same in this case you can't identify the actual.

If you want to achieve this goal then you should have a table tblAllUsers which primary key is also primary key of the tblGuest and tblUser and then you can make relation to tblBookRoomDetail table direct to tblAllUsers. In this case you can differentiate the Guest user or Registered Users.

or

Can Create the two different columns in tblBookRoomDetail, one for Guest user and second for Registered user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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