How to create table in database that stores data from 2 different tables in MS access Database in c# WPF

StackOverflow https://stackoverflow.com/questions/22649293

Question

Any tips on how to create new table that stores data from 2 different tables?

For example : Application Online - Library

User Named Jack from table = "Users" wants to book a "book" named Harry Potter from table ="Books".

This should now save into new table ="Bookings" , where we would have info of a user who has booked the book and the "book" name

Later admin could also display the same data.

I'll apprecciate any tips and help.

I will also be helpful of any links and tutorials/guides.

Thank you.

Was it helpful?

Solution

If you're talking about a relational database, then your table structure should look like this (with some sample data included here):

User table:
UserId: 17
FirstName: Jim
Last Name: Smith

Book table:
BookId: 42
Name: Harry Potter

Booking table:
BookingId: 4
UserId: 17
BookId: 42

The booking table is your association between users and their books. When you store data here, you should only store the IDs of the other tables, and those columns in the booking table (UserId, BookId) should be foreign keys that point to the other tables.

When you need to display information, you should join the tables in a query and get back all of the information you need.

If you need to add another association, you simply add another row to the booking table. For example, if you have another user with an ID of 20, and another book with an ID of 75, then you would simply add this row to your booking table:

Booking table:
BookingId: 5
UserId: 20
BookId: 75

Some links to help:
Normalization
SQL Joins
Foreign Keys

OTHER TIPS

If u want to do like Mr. Bob's answer than code is like

CREATE PROCEDURE StoreDataFromDiffTable

as
BEGIN

Declare @UserId int
Declare @BookId int

set @UserId =(select UserId from [User table] where FirstName = 'Jim')
set @BookId = (select BookId from [Book table] where Name ='[Harry Potter]')



insert into Booking table
(
UserId,
BookId 

)
values
(
@UserId,
@BookId 
)

Hope this helps you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top