r/DatabaseHelp • • Nov 18 '15

Managing data with multiple relations

Hi!

I'm new to MySQL and databases in general, and I'm currently setting up a sample database where you have (atm) a table with customers, and a table with orders. I hvae a primary key called customer_id in the customers table, and a primary key called order_id in the orders table. I also have a foreign key in the orders table that refers to the customer_id in the customers table.

Here's an image of my diagram

But what I don't understand is how to manage the data in these tables.

I suppose you'd do a INSERT INTO customers VALUES (1, "test", "test")

But how do you "connect" these to the other tables? How can a key in the customers table refer to another table in the query?

Thanks! Erik

UPDATE

I've changed my model to include a new table to allow for multiple products per order.

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/1tom235 Nov 19 '15

To add to this, after you use the correct customer_id (1) and create an order connecting it to the 'test' customer you created previously:

INSERT INTO orders VALUES (1, 1, 'whatever')

You can then query both tables by linking them with a JOIN operator:

SELECT * FROM customers JOIN orders ON customers.id = orders.customer_id;