Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inserting related data in two tables with same primary key  RSS feed

 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have a database named Hotel Management System and in that database i have two tables CustomerInfo and CustomerStayInfo. CustomerInfo table is used to hold personal details of customers like first name, last name etc, and CustomerStayInfo table is used to hold the stay details of the customers like check-in date, check-out date etc.

Whenever i insert any customer details in to CustomerInfo table, that customer's stay information is entered in to CustomerStayInfo table. Now obviously both records should have same id.

Question is how can i insert data in both tables that has same id as primary key?
 
Knute Snortum
Sheriff
Posts: 3612
86
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CustomerStayInfo should have its own ID, because a customer may return and you will want to use the same CustomerInfo record for them.  CustomerStayInfo should have only the ID to the CustomerInfo table.  Therefore, if you have a new customer staying at the hotel, you create a new CustomerInfo record and then a new CustomerStayInfo record.  But if you have a returning customer, use the same CustomerInfo record as before.

Note: you should probably drop the "Info" part of the table names, since it is redundant.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!